博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
[POJ 1273]Drainage Ditches
阅读量:4676 次
发布时间:2019-06-09

本文共 5371 字,大约阅读时间需要 17 分钟。

【问题描述】

  Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This means that the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John has built a set of drainage ditches so that Bessie's clover patch is never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John has also installed regulators at the beginning of each ditch, so he can control at what rate water flows into that ditch. 

  Farmer John knows not only how many gallons of water each ditch can transport per minute but also the exact layout of the ditches, which feed out of the pond and into each other and stream in a potentially complex network. 
  Given all this information, determine the maximum rate at which water can be transported out of the pond and into the stream. For any given ditch, water flows in only one direction, but there might be a way that water can flow in a circle. 

【输入】

  The input includes several cases. For each case, the first line contains two space-separated integers, N (0 <= N <= 200) and M (2 <= M <= 200). N is the number of ditches that Farmer John has dug. M is the number of intersections points for those ditches. Intersection 1 is the pond. Intersection point M is the stream. Each of the following N lines contains three integers, Si, Ei, and Ci. Si and Ei (1 <= Si, Ei <= M) designate the intersections between which this ditch flows. Water will flow through this ditch from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum rate at which water will flow through the ditch.

【输出】

  For each case, output a single integer, the maximum rate at which water may emptied from the pond.

【算法分析】

  裸的最大流,采用最短增广路算法的Dinic算法可以通过所有测试数据。

【程序代码】

1 #include
2 #include
3 #include
4 #define maxn 10000 5 #define inf 1000000 6 using namespace std; 7 int n,m,x,y,z; 8 struct graph{ 9 int start,end,pointsize,tot;10 int c[maxn<<1],to[maxn<<1],next[maxn<<1],head[maxn],dis[maxn],nowhead[maxn];11 void clear(){12 tot=1;13 memset(c,0,sizeof(c));14 memset(to,0,sizeof(to));15 memset(next,0,sizeof(next));16 memset(head,0,sizeof(head));17 memset(dis,0,sizeof(dis));18 memset(nowhead,0,sizeof(nowhead));19 }20 void addedge(int a,int b,int l){21 c[++tot]=l;to[tot]=b;next[tot]=head[a];head[a]=tot;22 c[++tot]=0;to[tot]=a;next[tot]=head[b];head[b]=tot;23 }24 int q[maxn],ql,qr;25 bool BFS(){26 for (int i=1;i<=pointsize;++i) nowhead[i]=head[i],dis[i]=0;27 ql=1; qr=0; q[++qr]=end;28 while (ql<=qr){29 for (int k=q[ql++],p=head[k];p;p=next[p])30 if(c[p^1]&&!dis[to[p]]&&to[p]!=end) dis[q[++qr]=to[p]]=dis[k]+1;31 }32 return dis[start];33 }34 int DFS(int k,int maxflow){35 if (k==end) return maxflow;36 int flow=0,tflow;37 for (int&p=nowhead[k];p&&maxflow;p=next[p])38 if(c[p]&&dis[to[p]]+1==dis[k]&&(tflow=DFS(to[p],min(maxflow,c[p]))))39 c[p]-=tflow,c[p^1]+=tflow,maxflow-=tflow,flow+=tflow;40 return flow;41 }42 int dinic(int a,int b){43 int flow=0;44 start=a; end=b;45 while (BFS()) flow+=DFS(a,inf);46 return flow;47 } 48 graph(){49 tot=1;50 }51 } G;52 int main(){53 while (scanf("%d%d",&n,&m)!=EOF){54 G.clear();55 G.pointsize=m;56 for (int i=1;i<=n;++i){57 scanf("%d%d%d",&x,&y,&z);58 G.addedge(x,y,z);59 }60 printf("%d\n",G.dinic(1,m));61 }62 return 0;63 }
View Code

用C++中vector和queue实现(参考《算法艺术入门经典训练指南》): 

1 #include
2 #include
3 #include
4 #include
5 #include
6 using namespace std; 7 const int maxn=10010; 8 const int INF=1e8; 9 struct Edge{10 int from,to,cap,flow;11 };12 struct Dinic{13 int n,m,s,t;14 vector
edges;15 vector
G[maxn];16 void AddEdge(int from,int to,int cap){17 edges.push_back((Edge){ from,to,cap,0});18 edges.push_back((Edge){to,from,0,0});19 m=edges.size();20 G[from].push_back(m-2);21 G[to].push_back(m-1);22 }23 bool vis[maxn];24 int d[maxn],cur[maxn];25 bool BFS(){26 memset(vis,0,sizeof(vis));27 queue
Q;28 Q.push(s); d[s]=0; vis[s]=1;29 while (!Q.empty()){30 int x=Q.front(); Q.pop();31 for (int i=0;i
e.flow){34 vis[e.to]=1;35 d[e.to]=d[x]+1;36 Q.push(e.to);37 }38 }39 }40 return vis[t];41 }42 int DFS(int x,int a){43 if (x==t||a==0) return a;44 int flow=0,f;45 for (int &i=cur[x];i
0){48 e.flow+=f;49 edges[G[x][i]^1].flow-=f;50 flow+=f; a-=f;51 if (a==0) break;52 }53 }54 return flow;55 }56 int Maxflow(int ss,int tt){57 int flow=0;58 s=ss; t=tt;59 while (BFS()){60 memset(cur,0,sizeof(cur));61 flow+=DFS(s,INF);62 }63 return flow;64 }65 }Graph;66 int main(){67 int nn,mm,from,to,cap;68 while (scanf("%d%d",&nn,&mm)!=EOF){69 Graph.edges.clear();70 memset(Graph.G,0,sizeof(Graph.G));71 for (int i=1;i<=nn;++i){72 scanf("%d%d%d",&from,&to,&cap);73 Graph.AddEdge(from,to,cap);74 }75 printf("%d\n",Graph.Maxflow(1,mm));76 }77 return 0;78 }
View Code

 

声明:本博文为博主原创博文,未经允许请勿转载。

转载于:https://www.cnblogs.com/Double680/p/5207178.html

你可能感兴趣的文章
TeX中的引号
查看>>
Python 模块(module)
查看>>
region实现大纲效果
查看>>
[洛谷P4234]最小差值生成树
查看>>
LiveNVR传统安防摄像机互联网直播-二次开发相关的API接口
查看>>
LiveNVR高性能稳定RTSP、Onvif探测流媒体服务配置通道接入海康、大华等摄像机进行全终端无插件直播...
查看>>
c c++ sizeof
查看>>
Intellij IDEA连接Spark集群
查看>>
最长回文子串解法
查看>>
代码优化程序性能
查看>>
腾讯实习生招聘笔试题目
查看>>
Java Socket编程----通信是这样炼成的
查看>>
作业要求 20180925-1 每周例行报告
查看>>
1078. Hashing (25)-PAT甲级真题
查看>>
SQLite中的运算符表达式
查看>>
Grid使用 & ComboBox Binding & DateTime Format WPF
查看>>
.Net Core迁移到MSBuild的多平台编译问题
查看>>
数据结构之删除线性表中的元素
查看>>
redis安装配置
查看>>
结对项目博客
查看>>