POJ 1273:Drainage Ditches 网络流模板题
Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 63339 | Accepted: 24434 |
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.
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 4 1 2 40 1 4 20 2 4 20 2 3 30 3 4 10
Sample Output
50
给了M个池塘,N个水渠,以及每个水渠连接的两个点和水渠的容量。(编号1为源点,编号M为汇点)。求整个网络中最大能流的水的流量。
网络流模板题。
代码:
#include <iostream> #include <algorithm> #include <cmath> #include <vector> #include <string> #include <queue> #include <cstring> #pragma warning(disable:4996) using namespace std; const int sum = 201; const int INF = 99999999; int cap[sum][sum],flow[sum][sum],a[sum],p[sum]; int N,M; void Edmonds_Karp() { int u,t,start=1,result=0; queue <int> s; while(s.size())s.pop(); while(1) { memset(a,0,sizeof(a)); memset(p,0,sizeof(p)); a[1]=INF; s.push(1); while(s.size()) { u=s.front(); s.pop(); for(t=1;t<=M;t++) { if(!a[t]&&flow[u][t]<cap[u][t]) { s.push(t); p[t]=u; a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量 } } } if(a[M]==0) break; result += a[M]; for(u=M;u!=1;u=p[u]) { flow[p[u]][u] += a[M]; flow[u][p[u]] -= a[M]; } } cout<<result<<endl; } int main() { int i,u,v,value; while(scanf("%d%d",&N,&M)==2) { memset(cap,0,sizeof(cap)); memset(flow,0,sizeof(flow)); for(i=1;i<=N;i++) { scanf("%d%d%d",&u,&v,&value); cap[u][v] += value; } Edmonds_Karp(); } return 0; }
版权声明:本文为博主原创文章,未经博主允许不得转载。