http://poj.org/problem?id=1273
Drainage Ditches
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions:87219 | Accepted: 33916 |
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个池塘(从1到m开始编号,1为源点,m为汇点),及n条水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水渠中所能流过的水的最大容量.一道基础的最大流题目。
——其他练习题 POJ3436 、
1 #include <cstdio> 2 #include <cstdlib> 3 #include <cstring> 4 #include <bitset> 5 #include <cmath> 6 #include <cctype> 7 #include <iostream> 8 #include <algorithm> 9 #include <string> 10 #include <vector> 11 #include <queue> 12 #include <map> 13 #include <set> 14 #include <sstream> 15 #include <iomanip> 16 using namespace std; 17 typedef long long ll; 18 typedef unsigned long long ull; 19 const ll inff = 0x3f3f3f3f3f3f3f3f; 20 #define FOR(i,a,b) for(int i(a);i<=(b);++i) 21 #define FOL(i,a,b) for(int i(a);i>=(b);--i) 22 #define REW(a,b) memset(a,b,sizeof(a)) 23 #define inf int(0x3f3f3f3f) 24 #define si(a) scanf("%d",&a) 25 #define sl(a) scanf("%I64d",&a) 26 #define sd(a) scanf("%lf",&a) 27 #define ss(a) scanf("%s",a) 28 #define mod ll(998244353) 29 #define pb push_back 30 #define eps 1e-6 31 #define lc d<<1 32 #define rc d<<1|1 33 #define Pll pair<ll,ll> 34 #define P pair<int,int> 35 #define pi acos(-1) 36 const int N=100008,M=100008; 37 int head[N],tot,n,m,a,b,c; 38 struct node{ 39 int next,c,to;}e[M]; 40 struct max_flow{ 41 int S,T,n; 42 int lev[N],q[N],cur[N],f; 43 void init(int _s,int _t) 44 { 45 tot=0;S=_s,T=_t,n=T+1; 46 FOR(i,0,n) head[i]=-1; 47 } 48 void add(int a,int b,int c) 49 { 50 e[tot].next=head[a]; 51 e[tot].to=b; 52 e[tot].c=c; 53 head[a]=tot++; 54 } 55 void Add(int a,int b,int c) 56 { 57 add(a,b,c); 58 add(b,a,0); 59 } 60 int bfs() 61 { 62 FOR(i,0,n) lev[i]=0; 63 lev[S]=1,f=0,q[f++]=S; 64 FOR(i,0,f-1) 65 { 66 int u=q[i]; 67 for(int i=head[u];i!=-1;i=e[i].next) 68 if(lev[e[i].to]==0&&e[i].c>0) 69 { 70 int to=e[i].to; 71 lev[to]=lev[u]+1; 72 q[f++]=to; 73 if(to==T) return 1; 74 } 75 } 76 return 0; 77 } 78 int dfs(int u,int f) 79 { 80 if(u==T) return f; 81 int tag=0,c; 82 for(int &i=cur[u];i!=-1;i=e[i].next) 83 { 84 int to=e[i].to; 85 if(e[i].c>0&&lev[to]==lev[u]+1) 86 { 87 c=dfs(to,min(f-tag,e[i].c)); 88 e[i].c-=c; 89 e[i^1].c+=c; 90 tag+=c; 91 if(tag==f) return tag; 92 } 93 } 94 return tag; 95 } 96 int slove() 97 { 98 int ans=0; 99 while(bfs()) 100 { 101 FOR(i,0,n) cur[i]=head[i]; 102 ans+=dfs(S,inf); 103 } 104 return ans; 105 } 106 }flow; 107 int main() 108 { 109 cin.tie(0); 110 cout.tie(0); 111 while(~scanf("%d%d",&m,&n)) 112 { 113 flow.init(1,n); 114 while(m--) 115 { 116 si(a),si(b),si(c); 117 flow.Add(a,b,c); 118 } 119 cout<<flow.slove()<<endl; 120 } 121 return 0; 122 }