Drainage Ditches
| Time Limit: 1000MS |
|
Memory Limit: 10000K |
| Total Submissions: 24043 |
|
Accepted: 8706 |
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
/*題目大意:一個農夫為了挖了很多條溝排水。1是pond,m是stream。求最大排水量題目解答:純粹的最大流問題.ps:第一次做腦子糊塗了,居然複製反向邊,導致糾結了。這個題目另外要注意的一點是可能產生平行邊。Source CodeProblem: 1273 User: wawadimu Memory: 564K Time: 16MS Language: C++ Result: Accepted Source Code*/ #include<iostream>#include<queue>using namespace std;#define maxn 220#define inf INT_MAX/*struct node{int u,v;}e[maxn];*/int n,m;//邊數,頂點數//int first[maxn],next[maxn];int flow[maxn][maxn],cap[maxn][maxn];int a[maxn],pre[maxn];int maxflow(int s,int t){int max=0;while(1){memset(a,0,sizeof(a));a[s]=inf;queue<int> q;q.push(s);while(!q.empty()){int u=q.front();q.pop();for(int v=1;v<=m;v++){if(!a[v] && cap[u][v] > flow[u][v]){pre[v]=u;a[v]=a[u] > cap[u][v]-flow[u][v] ? cap[u][v]-flow[u][v] : a[u];q.push(v);}}}if(!a[t]) break;for(int x=t;x!=s;x=pre[x]){flow[pre[x]][x]+=a[t];flow[x][pre[x]]-=a[t];}//cout<<max<<endl;max+=a[t];}return max;}int main(){//freopen("1273.txt","r",stdin);int s,t;int i,j;int c,tmp,u,v;while(scanf("%d%d",&n,&m)!=EOF){memset(cap,0,sizeof(cap));memset(flow,0,sizeof(flow));for(i=1;i<=n;i++){scanf("%d%d",&u,&v);scanf("%d",&c);cap[u][v]+=c;//有多重邊}s=1;t=m;int ans=maxflow(s,t);printf("%d/n",ans);}return 0;}