[CODEVS1993] [network flow] lawn drainage, codevs1993 lawn Drainage
DescriptionDescription
On Farmer John's farm, Bessie accumulates a pool of water in his favorite clover field during the rain. This means that the grass is flooded, and it will take a long time for the grass to continue to grow. Therefore, Farmer John built a drainage system to free Bessie's lawn from the troubles of being drowned by the water (don't worry, the rain will flow to a nearby Creek ). As a top-notch technician, Farmer John has placed a controller at one end of each drainage ditch so that he can control the flow of water flowing into the drainage ditch.
Farmer John knows the amount of water that can flow per minute in each drainage ditch, and the precise layout of the drainage system (the starting point is a pool and the ending point is a network of streams ). Note that sometimes, from one place to the other, there is not only one drain.
Based on this information, calculate the maximum traffic from the pool to the creek. For each drainage ditch given, the rain can only flow in one direction. Note that the rain may flow cyclically.
Input description
Input Description
Row 1st: two integers N (0 <= N <= 200) and M (2 <= M <= 200) separated by spaces ). N is the number of troughs that farmer John has dug, and M is the number of troughs at the intersection. Point 1 is the water lake, point M is the creek.
Line 2 to line N + 1: each row has three integers, Si, Ei, and Ci. Si AND Ei (1 <= Si, Ei <= M) indicate the intersection of the two sides of the drainage ditch, and the rain flows from Si to Ei. Ci (0 <= Ci <= 10,000,000) is the maximum capacity of the drainage ditch.
Output description
Output Description
Output an integer, that is, the maximum traffic for drainage.
Sample Input
Sample Input
5 41 2 401 4 202 4 202 3 303 4 10
Sample output
Sample Output
50
USACO template question =
# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # define MAXN 2048 # define MAXINT 0x7fffffusing namespace std; int n, m; // number of points and edge number int dis [MAXN + 1]; // distance label int num [MAXN + 1]; int src, des; // src Source point, des sink point int u, v, w; struct edge {int f; // int c of the current traffic of a certain arc; // capacity int ver; // Arc Start edge * rev; // arc edge * next; // arc end edge () {}; edge (int v, int cap, edge * nex): ver (v), c (cap), next (nex), rev (NULL), f (0) {}; void * operator new (size_t, void * p) {return p;} * s [MAXN + 1]; void init () {int queue [MAXN + 1], head = 0, tail = 0; for (int I = 1; I <= n; I ++) dis [I] = MAXN, num [I] = 0; queue [tail ++] = des; dis [des] = 0; num [0] = 1; while (head! = Tail) {int v = queue [head ++]; edge * e = s [v]; while (e) {if (e-> rev) & (e-> rev-> c = 0) | dis [e-> ver] <MAXN) {} // If the modified edge cannot pass through the stream or has been updated, you do not need to update else {dis [e-> ver] = dis [v] + 1; ++ num [dis [e-> ver]; queue [tail ++] = e-> ver;} e = e-> next ;}} int maxflow () // ISAP algorithm implementation process {int st = src, ret = 0; edge * E [MAXN], * rep [MAXN]; for (int I = 1; I <= n; I ++) E [I] = s [I]; while (dis [src] <n) // if the augmented path does not exist, stop {if (st = des) // find the augmented path {int delta = MAXINT; for (int I = src; I! = Des; I = E [I]-> ver) delta = min (delta, E [I]-> c); for (int I = src; I! = Des; I = E [I]-> ver) {E [I]-> c-= delta; E [I]-> f + = delta; E [I]-> rev-> c + = delta; E [I]-> rev-> f-= delta;} ret + = delta; st = src ;} edge * e; for (e = E [st]; e = e-> next) if (e-> c> 0 & dis [st] = dis [e-> ver] + 1) break; if (e) // The Arc {E [st] = e; rep [e-> ver] = e-> rev; st = e-> ver;} is allowed ;} else {if (-- num [dis [st]) = 0) break; // GAP optimization E [st] = s [st]; int mind = n; for (edge * t = s [st]; t = t-> next) if (t-> c> 0) mind = min (mind, dis [t-> ver]); dis [st] = mind + 1; ++ num [dis [st] ]; If (st! = Src) st = rep [st]-> ver ;}} return ret ;}int main () {freopen ("ditch. in "," r ", stdin); freopen (" ditch. out "," w ", stdout); scanf (" % d ", & m, & n); src = 1, des = n; edge * buffer = new edge [2 * m]; edge * data = buffer; while (m --) {scanf ("% d", & u, & v, & w); s [u] = new (void *) data ++) edge (v, w, s [u]); s [v] = new (void *) data ++) edge (u, 0, s [v]); s [u]-> rev = s [v]; s [v]-> rev = s [u];} init (); printf ("% d", maxflow ());}