POJ -- 1273 Drainage Ditches

                                Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 52402   Accepted: 19946

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 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

思路:最大流算法,基于FF方法:
Dinic:
 1 /*************************************************************************
 2     > File Name:        Dith.cpp
 3     > Author:         wangzhili
 4     > Mail:           wangstdio.h@gmail.com
 5     > Created Time:   2014年03月07日 星期五 10时47分22秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<queue>
10 #include<cstring>
11 #include<cstdio>
12 #define MAX 205
13 using namespace std;
14 int res[MAX][MAX], level[MAX], N, M;
15 queue<int>q;
16 bool bfs(int s)
17 {
18     memset(level, -1, sizeof(level));
19     while(!q.empty()) q.pop();
20     level[s] = 0;
21     q.push(s);
22     while(!q.empty())
23     {
24         int p = q.front();
25         q.pop();
26         for(int i = 1;i <= M;i ++)
27         {
28             if(level[i] == -1 && res[p][i] > 0)
29             {
30                 level[i] = level[p] + 1;
31                 q.push(i);
32             }
33         }
34     }
35     if(level[M] >= 0)
36         return true;
37     return false;
38 }
39 
40 int Dinic(int s, int sum)
41 {
42     if(s == M)
43         return sum;
44     int os = sum;
45     for(int i = 1;i <= M;i ++)
46     {
47         if(level[i] == level[s] + 1 && res[s][i] > 0)
48         {
49             int temp = Dinic(i, min(sum, res[s][i]));
50             res[s][i] -= temp;
51             res[i][s] += temp;
52             sum -= temp;
53         }
54     }
55     return os - sum;
56 }
57 
58 int main(int argc, char const *argv[]) 
59 {
60     int flow, u, v, w;
61    // freopen("in.c", "r", stdin);
62     while(cin >> N >> M)
63     {
64         flow = 0;
65         memset(res, 0, sizeof(res));
66         while(N--)
67         {
68             cin >> u >> v >> w;
69             res[u][v] += w;
70         }
71         while(bfs(1))
72             flow += Dinic(1, 1 << 30);
73         cout << flow << endl;
74     }
75     return 0;
76 }

 Edmond-Karp:

 1 /*************************************************************************
 2     > File Name:        EK.cpp
 3     > Author:         wangzhili
 4     > Mail:           wangstdio.h@gmail.com
 5     > Created Time:   2014年03月07日 星期五 11时08分35秒
 6  ************************************************************************/
 7 
 8 #include<iostream>
 9 #include<queue>
10 #include<cstring>
11 #include<cstdio>
12 #define MAX 205
13 using namespace std;
14 int res[MAX][MAX], vis[MAX], pre[MAX], N, M;
15 queue<int>q;
16 bool bfs(int s, int t)
17 {
18     memset(vis, 0, sizeof(vis));
19     memset(pre, -1, sizeof(pre));
20     while(!q.empty()) q.pop();
21     vis[s] = 1;
22     q.push(s);
23     while(!q.empty())
24     {
25         int p = q.front();
26         q.pop();
27         for(int i = 1;i <= M;i ++)
28         {
29             if(!vis[i] && res[p][i] > 0)
30             {
31                 vis[i] = 1;
32                 pre[i] = p;
33                 if(i == t)
34                     return true;
35                 q.push(i);
36             }
37         }
38     }
39     return false;
40 }
41 
42 int EK(int s, int t)
43 {
44     int d, u;
45     d = 1 << 30;
46     u = t;
47     while(pre[u] != -1)
48     {
49         d = min(d, res[pre[u]][u]);
50         u = pre[u];
51     }
52     u = t;
53     while(pre[u] != -1)
54     {
55         res[pre[u]][u] -= d;
56         res[u][pre[u]] += d;
57         u = pre[u];
58     }
59     return d;
60 }
61 int main(int argc, char const *argv[]) 
62 {
63     int u, v, w, flow;
64    // freopen("in.c", "r", stdin);
65     while(cin >> N >> M)
66     {
67         flow = 0;
68         memset(res, 0, sizeof(res));
69         while(N--)
70         {
71             cin >> u >> v >> w;
72             res[u][v] += w;
73         }
74         while(bfs(1, M))
75             flow += EK(1, M);
76         cout << flow << endl;
77     }
78     return 0;
79 }
 
原文地址:https://www.cnblogs.com/anhuizhiye/p/3585935.html