POJ-1273 Drainage Ditches(最大流)
题目链接:http://poj.org/problem?id=1273
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
最大流水题,适合入门熟悉算法
1 #include<iostream> 2 #include<cstring> 3 #include<cmath> 4 #include<algorithm> 5 #include<map> 6 #include<cstdio> 7 #include<queue> 8 #include<stack> 9 10 using namespace std; 11 12 const int INF = 0x3f3f3f3f; 13 const int MAXM = 205; 14 15 int cost[MAXM][MAXM]; 16 17 int n, m; 18 int start, end; 19 int path[MAXM], flow[MAXM]; 20 21 int bfs(){ 22 memset( path, -1, sizeof( path ) ); 23 path[start] = 0; flow[start] = INF; 24 25 queue<int> Q; 26 Q.push( start ); 27 28 while( !Q.empty() ){ 29 int x = Q.front(); Q.pop(); 30 if( x == end ) break; 31 for( int i = 1; i <= m; i++ ){ 32 if( i != start && path[i] == -1 && cost[x][i] ){ 33 flow[i] = flow[x] < cost[x][i] ? flow[x] : cost[x][i]; 34 Q.push( i ); 35 path[i] = x; 36 } 37 } 38 } 39 40 if( path[end] == -1 ) return -1; 41 return flow[end]; 42 } 43 44 int Edmonds_Karp(){ 45 int max_flow = 0, add, now, pre; 46 47 while( ( add = bfs() ) != -1 ){ 48 max_flow += add; 49 now = end; 50 while( now != start ){ 51 pre = path[now]; 52 cost[pre][now] -= add; 53 cost[now][pre] += add; 54 now = pre; 55 } 56 } 57 58 return max_flow; 59 } 60 61 int main(){ 62 ios::sync_with_stdio( false ); 63 64 while( cin >> n >> m ){ 65 memset( cost, 0, sizeof( cost ) ); 66 int a, b, c; 67 while( n-- ){ 68 cin >> a >> b >> c; 69 cost[a][b] += c; 70 } 71 start = 1; 72 end = m; 73 74 cout << Edmonds_Karp() << endl; 75 } 76 }