Drainage ditches
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 62078 |
|
Accepted: 23845 |
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 93
#include <stdio.h>#include<string.h>#include<iostream>#include<queue>#include<algorithm>using namespacestd;intedge[ -][ -];//adjacency Matrixintdis[ -];//distance from source point, hierarchical graphintStart,end;intM,n;//N: points; M, number of sidesintBFs () {memset (DIS,-1,sizeof(dis));//fill with-1dis[1]=0; Queue<int>Q; Q.push (start); while(!Q.empty ()) { intu=Q.front (); Q.pop (); for(intI=1; i<=n;i++){ if(dis[i]<0&&Edge[u][i]) {Dis[i]=dis[u]+1; Q.push (i); } } } if(dis[n]>0) return 1; Else return 0;//the dis of the meeting point is less than 0, indicating that the BFS is not meeting point}//find represents an augmentation, the function returns the volume of this augmentation, and returns 0 to indicate that it cannot be augmentedintFindintXintLow) {//Low is the remaining flow from the source point to the narrowest edge of the current (the least remaining traffic) intA=0; if(x==N)returnLow//It's meeting point. for(intI=1; i<=n;i++){ if(edge[x][i]>0&&dis[i]==dis[x]+1&&//Unicom, is the next layer of the layered graph(A=find (I,min (low,edge[x][i)))) {//can reach meeting point (a <> 0)edge[x][i]-=A; EDGE[I][X]+=A; returnA; } } return 0;}intMain () { while(SCANF ("%d%d", &m,&n)! =EOF) {memset (Edge,0,sizeof(Edge)); for(intI=1; i<=m;i++){ intu,v,w; scanf ("%d%d%d",&u,&v,&W); EDGE[U][V]+=W; } Start=1; End=N; intans=0; while(BFS ()) {//to constantly build a hierarchical map, if the BFS does not reach the meeting point before the endAns+=find (1,0x7fffffff);//once the BFS has to keep looking for the augmented path until it can't find it.} printf ("%d\n", ans); } return 0;}
poj1273 Network inflow door problem dinic algorithm solution, can be used as a template