【poj1273】Drainage Ditches 【USACO 93】

来源:互联网 发布:域名的价格是怎么回事 编辑:程序博客网 时间:2024/05/22 01:26

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

这道题是一道网络流模板题,大致题意就是求1到m的最大流,可以用增广路算法,下面是代码:

#include<queue>#include<cstdio>#include<cstdlib>#include<cstring>#include<iostream>using namespace std;struct edge{int u,v,w;edge *next,*other;}*head[205],*last[205];int n,m,f[205];edge *make(){return (edge *)(malloc(sizeof(edge)));}void add(int u,int v,int w){edge *x=make(),*y=make();x->other=y;y->other=x;x->u=u;y->u=v;x->v=v;y->v=u;x->w=w;y->w=0;x->next=head[u];y->next=head[v];head[u]=x;head[v]=y;}void bfs(){memset(f,0,sizeof(f));memset(last,0,sizeof(last));queue<int>q;q.push(1);f[1]=1<<30;while(!f[n]&&!q.empty()){int x=q.front();q.pop();edge *now=head[x];while(now){if(now->w){int k=now->v;if(!f[k]){last[k]=now;f[k]=min(f[x],now->w);q.push(k);}if(f[n]){return;}}now=now->next;}}}void sum(){edge *now=last[n];while(now){now->w-=f[n];now->other->w+=f[n];now=last[now->u];}}void work(){int u,v,w,s=0,i;while(m--){scanf("%d%d%d",&u,&v,&w);add(u,v,w);}do{bfs();sum();s+=f[n];}while(f[n]);printf("%d\n",s);for(i=1;i<=n;i++){edge *now=head[i];while(now){edge *x=now->next;free(now);now=x;}}memset(head,0,sizeof(head));}int main(){while(scanf("%d%d",&m,&n)!=EOF){work();}return 0;}

注:代码中n和m与题中是相反的。

原创粉丝点击