在写这篇文章之前,xxx已经写过了几篇关于改queue标号主题的文章,想要了解的朋友可以去翻一下之前的文章
Drainage Ditches
Problem Description
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.
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.
Input
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.
Output
For each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
#include<iostream> #include<cstring> #include<cstdio> using namespace std; const int MAXN=200; const int INF=(1<<29); int flow[MAXN][MAXN];//容量制约 int pre[MAXN];//前驱 int dalta[MAXN];//改变量 int flag[MAXN];//是不是已标号 int queue[MAXN],front,rear; int n,m; int EK() { int i,j,maxflow; maxflow=0; while(1) { for(i=1;i<=m;i++) flag[i]=0; front=rear=0; queue[rear++]=1; flag[1]=1; dalta[1]=INF; pre[1]=1; while(front!=rear&&!flag[m]) { int v=queue[front++]; for(i=1;i<=m;i++) { if(flag[i]) continue; if(flow[v][i]) { dalta[i]=min(dalta[v],flow[v][i]); pre[i]=v; flag[i]=1; queue[rear++]=i; } } } if(!flag[m]) break; maxflow+=dalta[m]; i=m; while(i!=1) { flow[pre[i]][i]-=dalta[m]; flow[i][pre[i]]+=dalta[m]; i=pre[i]; } } return maxflow; } int main() { int si,ei,ci; while(~scanf("%d%d",&n,&m)) { for(int i=1;i<=m;i++) for(int j=1;j<=m;j++) flow[i][j]=0; while(n--) { scanf("%d%d%d",&si,&ei,&ci); flow[si][ei]+=ci;//注意有平行边 } printf("%d\n",EK()); } return 0; }
文章结束给大家分享下程序员的一些笑话语录: 手机终究会变成PC,所以ip会比wm更加畅销,但是有一天手机强大到一定程度了就会发现只有wm的支持才能完美享受。就好比树和草,草长得再高也是草,时间到了条件成熟了树就会窜天高了。www.ishuo.cn
---------------------------------
原创文章 By
queue和标号
---------------------------------