Drainage Ditches
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 67823 |
|
Accepted: 26209 |
Description
Every time it rains on Farmer John's fields, a pond forms over Bessie ' s favorite Clover patch. This means, the clover is covered by water for awhile and takes quite a long time to regrow. Thus, Farmer John had built a set of drainage ditches so that Bessie ' s clover Patch was never covered in water. Instead, the water is drained to a nearby stream. Being an ace engineer, Farmer John have also installed regulators at the beginning of all ditch, so he can control at what Rate water flows to that ditch.
Farmer John knows not only what many gallons of water each ditch can transport per minute but also the exact layout of the Ditches, which feed out of the the pond and to each other and stream in a potentially complex network.
Given All this information, determine the maximum in which water can be transported off of the pond and into the stre Am. For any given ditch, water flows on only one direction, but there might is a-a-to-a-water can flow in a circle.
Input
The input includes several cases.For each case, the first line contains the space-separated integers, n (0 <= n <=) and M (2 <= M <= 200). N is the number of ditches this 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 would flow through this ditch from Si to Ei. CI (0 <= ci <= 10,000,000) is the maximum rate at which water would flow through the ditch.
Output
For each case, output a single integer and 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
Source
Usaco —————————— I am the dividing line —————————————————————————————————————————————————— water problem, template problem. Network flow. Maximum flow, augmented path algorithm solution. I've got two heads.
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cmath>5#include <algorithm>6#include <cassert>7#include <climits>8 #defineMAXN 2109 using namespacestd;Ten voidfind (); One voidflow (); A voidupdate (); - structEdge - { the intC; - intF; - }EDGE[MAXN][MAXN]; - intn,m; + ints,t; - intRESIDUAL[MAXN][MAXN]; + intque[maxn*Maxn],head,tail; A intPRE[MAXN]; at BOOLVIS[MAXN]; - intMax_flow,min_flow; - voidFind () - { - intI,cu; -memset (Vis,false,sizeof(Vis)); inmemset (Residual,0,sizeof(residual)); -memset (PRE,0,sizeof(pre)); toHead=0; que[head]=s;pre[s]=s;vis[s]=true; tail=1; + while(head<tail&&pre[t]==0) - { thecu=Que[head]; * for(i=1; i<=n;i++) $ {Panax Notoginseng if(vis[i]==false) - { the if(edge[cu][i].c-edge[cu][i].f>0) + { Aresidual[cu][i]=edge[cu][i].c-edge[cu][i].f; thepre[i]=cu;que[tail++]=i;vis[i]=true; + } - Else if(edge[i][cu].f>0) $ { $residual[cu][i]=edge[i][cu].f; -pre[i]=cu;que[tail++]=i;vis[i]=true; - } the } - }Wuyihead++; the } - } Wu voidflow () - { About intI=t,j; $ if(pre[i]==0) - { -min_flow=0;return; - } Aj=0x7fffffff; + while(i!=s) the { - if(RESIDUAL[PRE[I]][I]<J) j=Residual[pre[i]][i]; $I=Pre[i]; the } themin_flow=J; the } the voidUpdate () - { in intI=T; the if(pre[i]==0)return; the while(i!=s) About { the if(edge[pre[i]][i].c-edge[pre[i]][i].f>0) theedge[pre[i]][i].f+=Min_flow; the Else if(edge[i][pre[i]].f>0) +edge[pre[i]][i].f+=Min_flow; -I=Pre[i]; the }Bayi } the voidSolve () the { -s=1; t=N; -max_flow=0; the while(true) the { the find (); flow (); themax_flow+=Min_flow; - if(min_flow>0) update (); the Else return; the } the }94 intMain () the { theStd::ios::sync_with_stdio (false); the inti,u,v,c;98 while(SCANF ("%d%d", &m,&n)! =EOF) About { -Memset (Edge,0,sizeof(Edge));101 for(i=0; i<m;i++)102 {103scanf" %d%d%d",&u,&v,&c);104edge[u][v].c+=C; the }106 solve ();107printf"%d\n", Max_flow);108 }109 return 0; the}
View Code
POJ 1273 Drainage ditches--S.B.S.