POJ1273 Drainage Ditches(最大流)

来源:互联网 发布:电脑淘宝详情页打不开 编辑:程序博客网 时间:2024/05/16 08:34

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.

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 41 2 401 4 202 4 202 3 303 4 10

Sample Output

50


参考Kuangbin大神的,发现他和别人写的不一样的一点就是每次调整都在原图上直接调整,而很多人都是又开了一个可改进量的数组,说不上哪种更好,看个人喜好吧。

#include<iostream>#include<queue>#include<cstring>#include<cstdio>#include<algorithm>const int INF=0x3f3f3f3f;using namespace std;int n,m,map[205][205],path[205],flow[205],start,end;queue<int> q;int bfs(){while(!q.empty())q.pop();memset(path,-1,sizeof(path));path[start]=0;flow[start]=INF;q.push(start);while(!q.empty()){int now=q.front();q.pop();if(now==end)break;for(int i=1;i<=m;i++){if(i!=start&&path[i]==-1&&map[now][i]){flow[i]=min(flow[now],map[now][i]);q.push(i);path[i]=now;}}}if(path[end]==-1)return -1;return flow[end];}int Edmonds_Karp(){int MaxFlow=0,step,now,pre;while(bfs()!=-1){step=bfs();MaxFlow+=step;now=end;while(now!=start){pre=path[now];map[pre][now]-=step;map[now][pre]+=step;now=pre;}}return MaxFlow;}int main(void){while(~scanf("%d%d",&n,&m)){memset(map,0,sizeof(map));for(int i=0;i<n;i++){int u,v,w;scanf("%d%d%d",&u,&v,&w);map[u][v]+=w;}start=1;end=m;printf("%d\n",Edmonds_Karp());}return 0;}

0 0
原创粉丝点击