Drainage Ditches
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 50321 | Accepted: 19105 |
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 41 2 401 4 202 4 202 3 303 4 10
Sample Output
50
Source
1 //472K 0MS C++ 1220B 2013-10-29 10:47:25 2 /* 3 4 题意: 5 给一个图,求最大流 6 7 网络流最大流: 8 第一题最大流..模板了EK算法.. 9 10 */11 #include12 #include 13 #define inf 0x7ffffff14 using namespace std;15 int c[205][205]; //容量 16 int flow[205][205]; //流量 17 int n,m;18 int f;19 void Edmondes_Karp()20 {21 int la[205]; //允许的最大调整量 22 int pre[205]; //前驱 23 queue Q;24 memset(flow,0,sizeof(flow));25 f=0;26 while(true){27 memset(la,0,sizeof(la));28 la[1]=inf;29 Q.push(1);30 while(!Q.empty()){31 int u=Q.front();32 Q.pop();33 for(int v=1;v<=m;v++){34 if(!la[v] && c[u][v]>flow[u][v]){35 pre[v]=u;36 Q.push(v);37 la[v]=min(la[u],c[u][v]-flow[u][v]);38 }39 }40 }41 if(la[m]==0) break; //不存在增广轨 42 for(int u=m;u!=1;u=pre[u]){ //调整增广轨 43 flow[pre[u]][u]+=la[m];44 flow[u][pre[u]]-=la[m];45 }46 f+=la[m];47 }48 }49 int main(void)50 {51 int s,e,c0;52 while(scanf("%d%d",&n,&m)!=EOF)53 {54 memset(c,0,sizeof(c));55 for(int i=0;i