TOJ 4085 Drainage Ditches(最大流)

Posted taozi1115402474

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了TOJ 4085 Drainage Ditches(最大流)相关的知识,希望对你有一定的参考价值。

描述

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.

输入

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.

输出

For each case, output a single integer, the maximum rate at which water may emptied from the pond.

样例输入

5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

样例输出

50
题意

M条边N个点,M行每行u,v,w,代表从u到v的最大流量w

题解

直接建图跑最大流,入门题

代码

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 
 4 const int N=205,M=205;
 5 int c[N][N],pre[N],n,m,S,T;
 6 bool bfs()
 7 {
 8     int vis[N]={0};
 9     memset(pre,0,sizeof pre);
10     queue<int>q;
11     q.push(S);
12     while(!q.empty())
13     {
14         int u=q.front();q.pop();
15         for(int v=1;v<=n;v++)
16         {
17             if(c[u][v]>0&&!vis[v])
18             {
19                 pre[v]=u;
20                 if(v==n)return true;
21                 vis[v]=1;
22                 q.push(v);
23             }
24         }
25     }
26     return false;
27 }
28 int maxflow()
29 {
30     int flow=0;
31     while(bfs())
32     {
33         int d=1e9;
34         for(int i=T;i!=1;i=pre[i])d=min(d,c[pre[i]][i]);
35         for(int i=T;i!=1;i=pre[i])
36             c[pre[i]][i]-=d,
37             c[i][pre[i]]+=d;
38         flow+=d;
39     }
40     return flow;
41 }
42 int main()
43 {
44     while(scanf("%d%d",&m,&n)!=EOF)
45     {
46         memset(c,0,sizeof c);
47         for(int i=0,u,v,w;i<m;i++)
48         {
49             scanf("%d%d%d",&u,&v,&w);
50             c[u][v]+=w;
51         }
52         S=1,T=n;
53         printf("%d
",maxflow());
54     }
55     return 0;
56 }

 









以上是关于TOJ 4085 Drainage Ditches(最大流)的主要内容,如果未能解决你的问题,请参考以下文章

POJ1273 Drainage Ditches

POJ 1273 Drainage Ditches

POJ-1273 Drainage Ditches 最大流

POJ 1273 Drainage Ditches 费用流

网络流入门 Drainage Ditches

hdu 1532:Drainage Ditches