Drainage ditchesproblem descriptionevery 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 ction, but there might be a way that water can flow in a circle.
Inputthe input parameter des 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
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.
Outputfor each case, output a single integer, the maximum rate at which water may emptied from the pond.
Sample Input
5 41 2 401 4 202 4 202 3 303 4 10
Sample output
50# Include <iostream> # include <cstring> # include <cstdio> using namespace STD; const int maxn = 200; const int INF = (1 <29 ); int flow [maxn] [maxn]; // Capacity Limit int pre [maxn]; // precursor int dalta [maxn]; // int flag [maxn]; // whether the specified int queue [maxn], front, rear; int n, m; int EK () {int I, j, maxflow; maxflow = 0; while (1) {for (I = 1; I <= m; I ++) Flag [I] = 0; front = rear = 0; queue [rear ++] = 1; flag [1] = 1; dalta [1] = inf; Pre [1] = 1; while (front! = Rear &&! Flag [m]) {int v = queue [Front ++]; for (I = 1; I <= m; I ++) {If (flag [I]) continue; If (flow [v] [I]) {dalta [I] = min (dalta [v], flow [v] [I]); pre [I] = V; flag [I] = 1; queue [rear ++] = I ;}} if (! Flag [m]) break; maxflow + = dalta [m]; I = m; while (I! = 1) {flow [pre [I] [I]-= dalta [m]; flow [I] [pre [I] + = dalta [m]; I = pre [I] ;}}return maxflow;} int main () {int Si, EI, CI; while (~ Scanf ("% d", & N, & M) {for (INT I = 1; I <= m; I ++) for (Int J = 1; j <= m; j ++) flow [I] [J] = 0; while (n --) {scanf ("% d", & Si, & EI, & CI); flow [Si] [ei] + = CI; // note that there are parallel edges} printf ("% d \ n", EK ());} return 0 ;}