首页 > 代码库 > hdu 1532 Drainage Ditches(最大流)
hdu 1532 Drainage Ditches(最大流)
Drainage Ditches
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.
InputThe 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.
OutputFor 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 10Sample Output
50
最大流的模板 考虑重边
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 #include<cstdlib> 6 #include<string.h> 7 #include<set> 8 #include<vector> 9 #include<queue>10 #include<stack>11 #include<map>12 #include<cmath>13 typedef long long ll;14 typedef unsigned long long LL;15 using namespace std;16 const double PI=acos(-1.0);17 const double eps=0.0000000001;18 const int INF=1e9;19 const int N=1000+100;20 int mp[N][N];21 int vis[N];22 int pre[N];23 int m,n;24 int BFS(int s,int t){25 queue<int>q;26 memset(pre,-1,sizeof(pre));27 memset(vis,0,sizeof(vis));28 pre[s]=0;29 vis[s]=1;30 q.push(s);31 while(!q.empty()){32 int p=q.front();33 q.pop();34 for(int i=1;i<=n;i++){35 if(mp[p][i]>0&&vis[i]==0){36 pre[i]=p;37 vis[i]=1;38 if(i==t)return 1;39 q.push(i);40 }41 }42 }43 return false;44 }45 int EK(int s,int t){46 int flow=0;47 while(BFS(s,t)){48 //BFS(s,t);49 int dis=INF;50 for(int i=t;i!=s;i=pre[i])51 dis=min(mp[pre[i]][i],dis);52 for(int i=t;i!=s;i=pre[i]){53 mp[pre[i]][i]=mp[pre[i]][i]-dis;54 mp[i][pre[i]]=mp[i][pre[i]]+dis;55 }56 flow=flow+dis;57 }58 return flow;59 }60 int main(){61 while(scanf("%d%d",&n,&m)!=EOF){62 memset(mp,0,sizeof(mp));63 for(int i=0;i<n;i++){64 int u,v,w;65 scanf("%d%d%d",&u,&v,&w);66 mp[u][v]=mp[u][v]+w;67 }68 int ans=EK(1,m);69 cout<<ans<<endl;70 }71 72 }
hdu 1532 Drainage Ditches(最大流)
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。