POJ-1273 Drainage Ditches 【最大流】

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



题解:
裸的最大流

代码:
 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<queue>
 5 #include<stack>
 6 #include<algorithm>
 7 #include<vector>
 8 using namespace std;
 9 #define INF 0x3f3f3f3f
10 #define M(a, b) memset(a, b, sizeof(a))
11 const int N = 200 + 5;
12 struct Edge {
13     int from, to, cap, flow;
14 };
15 
16 struct Dinic {
17     int n, m, s, t;
18     vector<Edge> edges;
19     vector<int> G[N];
20     bool vis[N];
21     int d[N], cur[N];
22 
23     void init(int n) {
24         for (int i = 0; i <= n; ++i) G[i].clear();
25         edges.clear();
26         M(d, 0);
27     }
28 
29     void AddEdge(int from, int to, int cap) {
30         edges.push_back((Edge){from, to, cap, 0});
31         edges.push_back((Edge){to, from, 0, 0});
32         m = edges.size();
33         G[from].push_back(m-2); G[to].push_back(m-1);
34     }
35 
36     bool bfs() {
37         M(vis, 0);
38         queue<int> q;
39         q.push(s);
40         d[s] = 0; vis[s] = 1;
41         while (!q.empty()) {
42             int x = q.front(); q.pop();
43             for (int i = 0; i < G[x].size(); ++i) {
44                 Edge &e = edges[G[x][i]];
45                 if (!vis[e.to] && e.cap > e.flow) {
46                     vis[e.to] = 1;
47                     d[e.to] = d[x] + 1;
48                     q.push(e.to);
49                 }
50             }
51         }
52         return vis[t];
53     }
54 
55     int dfs(int x, int a) {
56         if (x == t || a == 0) return a;
57         int flow = 0, f;
58         for (int &i = cur[x]; i < G[x].size(); ++i) {
59             Edge &e = edges[G[x][i]];
60             if (d[e.to] == d[x] + 1 && (f = dfs(e.to, min(a, e.cap-e.flow))) > 0) {
61                 e.flow += f;
62                 edges[G[x][i]^1].flow -= f;
63                 flow += f; a -= f;
64                 if (a == 0) break;
65             }
66         }
67         return flow;
68     }
69 
70     int Maxflow(int s, int t) {
71         this->s = s; this->t = t;
72         int flow = 0;
73         while (bfs()) {
74             M(cur, 0);
75             flow += dfs(s, INF);
76         }
77         return flow;
78     }
79 
80 }solver;
81 
82 int main() {
83     int m, n;
84     while (~scanf("%d%d", &m, &n)) {
85         solver.init(n);
86         int u, w, v;
87         for (int i = 0; i < m; ++i) {
88             scanf("%d%d%d", &u, &v, &w);
89             solver.AddEdge(u, v, w);
90         }
91         printf("%d\n", solver.Maxflow(1, n));
92     }
93 
94     return 0;
95 }

 

posted @ 2017-04-20 19:59  Robin!  阅读(...)  评论(...编辑  收藏