poj Drainage Ditches 1273 (最大流入门)

来源:互联网 发布:郑州网络推广哪家专业 编辑:程序博客网 时间:2024/05/16 10:31
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 64026 Accepted: 24713

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
题意:(最大流模板)
有n个城市,n条路,出发点在1,终点在m,让求最大流。
#include<stdio.h>#include<string.h>#include<algorithm>#include<queue>#define INF 0x3f3f3f3f#define N 220using namespace std;int p[N];bool vis[N];int map[N][N];int s,e;int n,m;bool bfs(){queue<int>q;memset(p,0,sizeof(p));memset(vis,false,sizeof(vis));vis[s]=1;q.push(s);while(!q.empty()){int ss=q.front();q.pop();if(ss==e)return true;//如果找到一条从1到n的路。 for(int i=2;i<=n;i++){if(!vis[i]&&map[ss][i])//如果可到达,但还没访问,遍历所有与i相连的城市 {q.push(i);p[i]=ss;//p[]数组里存放父节点 vis[i]=1;}}}return false;//找不到 }int maxflow(){int ans=0;//记录最大流 while(1){if(!bfs())//如果没有从1到n的路 return ans;//直接输出流量 int mm=INF;for(int i=e;i!=s;i=p[i])//从终点往起点找(从最小子孙往祖宗找) mm=min(mm,map[p[i]][i]);//找到关系网中的最小的残留量。 for(int i=e;i!=s;i=p[i])//逐个更新 {map[p[i]][i]-=mm;//正着的(父到子) map[i][p[i]]+=mm;//反着的(子到父) }ans+=mm;//更新最大流 }}int main(){int u,v,w;while(scanf("%d%d",&m,&n)!=EOF){s=1;e=n;memset(map,0,sizeof(map));while(m--){scanf("%d%d%d",&u,&v,&w);map[u][v]+=w;//每条边赋初值 }printf("%d\n",maxflow());}return 0;}

0 0