【问题描述】
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.
【算法分析】
翻译一下就是裸的最大流,在此不做解释。
【程序代码】
1 #include<cstdio> 2 #include<algorithm> 3 #include<cstring> 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 }
声明:本博文为博主原创博文,未经允许请勿转载。
原文:http://www.cnblogs.com/Double680/p/5207178.html