Drainage Ditches
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
Source
题目类型:最大流
算法分析:直接建图,使用EK算法求解即可
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
#include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; const int INF = 0x7FFFFFFF; const int MOD = 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 200 + 66; struct Node { int c, f;//分别表示弧之间的容量和流量 }; Node edge[maxn][maxn]; int n, m; int vis[maxn], pre[maxn], res[maxn][maxn]; void FindAugmentPath (int s, int t) { memset (vis, 0, sizeof (vis)); memset (res, 0, sizeof (res)); memset (pre, 0, sizeof (pre)); pre[s] = s, vis[s] = 1; queue <int> qu; qu.push(s); while(!qu.empty() && pre[t] == 0) { int tt = qu.front(); qu.pop (); for (int i = 1; i <= n; i++) { if (!vis[i]) { if (edge[tt][i].f < edge[tt][i].c) { res[tt][i] = edge[tt][i].c - edge[tt][i].f; vis[i] = 1, pre[i] = tt, qu.push(i); } else if (edge[i][tt].f > 0) { res[tt][i] = edge[i][tt].f; vis[i] = 1, pre[i] = tt, qu.push(i); } } } } } long long GetAugmentFlow (int s, int t) { if (pre[t] == 0) return 0; int pa = t, pb = INF; while (pa != s) { if (res[pre[pa]][pa] < pb) pb = res[pre[pa]][pa]; pa = pre[pa]; } return pb; } void UpDateFlow (int s, int t, int v) { if (pre[t] == 0) return ; int pa = t; while (pa != s) { if (edge[pre[pa]][pa].c > edge[pre[pa]][pa].f) edge[pre[pa]][pa].f += v; else if (edge[pa][pre[pa]] .f > 0) edge[pre[pa]][pa].f += v; pa = pre[pa]; } } long long Edmonds_Karp (int s, int t) { long long maxflow = 0, tt; while (1) { FindAugmentPath (s, t); tt = GetAugmentFlow (s, t); maxflow += tt; if (tt > 0) UpDateFlow (s, t, tt); else return maxflow; } } int main() { // ifstream cin ("aaa.txt"); while (cin >> m >> n) { memset (edge, 0, sizeof (edge)); for (int i = 1; i <= m; i++) { int u, v, w; cin>> u >> v >> w; edge[u][v].c += w; } cout << Edmonds_Karp(1, n) << endl; } return 0; } |
- « 上一篇:poj1258
- poj1284:下一篇 »