POJ 1273 Drainage Ditches

来源:互联网 发布:死亡岛低配置优化补丁 编辑:程序博客网 时间:2024/03/29 23:17
Drainage Ditches
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 43505 Accepted: 16377

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

Source

USACO 93

最大流问题,用Edmonds_Karp算法,Dinic算法,SAP算法均可以实现,这里贴出Edmonds_Karp的代码。

Edmonds_Karp Code:

#include<iostream>using namespace std;#define Inf 10000010;bool visit[210];int flow[210][210];int m,n,F,total,i,j,x,y,f;;int min(int x,int y){ return x>y?y:x;}bool Dfs(int pos,int end)//用dfs寻找增广路{ int temp; visit[pos]=true; if(pos==n)//每条路径完成搜索total加上这条路的流量   {   total+=F;   return true;     }  for(;end<=n;end++)  if(flow[pos][end]>0&&!visit[end])   //如果这一条路可以流通且流入点未被访问  {   temp=F;//记下流入的流量   F=min(F,flow[pos][end]);//记下流出的流量   if(Dfs(end,1))//变换起始流点   {    flow[pos][end]-=F;//正向减流    flow[end][pos]+=F;//反向加流    return true;   }   F=temp;//如果此路径无法流到终点,F恢复为前一个流点的流入量  } return false;}int main(){ while(cin>>m>>n) {  total=0;  memset(flow,0,sizeof(flow));  for(i=1;i<=m;i++)  {   cin>>x>>y>>f;   if(x!=y)   flow[x][y]+=f;//考虑重边的情况用+=而不是==  }  while(1)  {   memset(visit,false,sizeof(visit));//每重新搜索一条路径设各节点未被访问   F=Inf;   if(!Dfs(1,1))//所有路径搜索完毕则退出搜索    break;  }  cout<<total<<endl; } return 0;}
原创粉丝点击