码迷,mamicode.com
首页 > 其他好文 > 详细

TOJ 4085 Drainage Ditches(最大流)

时间:2018-08-23 23:12:54      阅读:322      评论:0      收藏:0      [点我收藏+]

标签:The   ane   otto   i++   fir   NPU   ESS   最大流   gallon   

描述

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\n",maxflow());
54     }
55     return 0;
56 }

 

TOJ 4085 Drainage Ditches(最大流)

标签:The   ane   otto   i++   fir   NPU   ESS   最大流   gallon   

原文地址:https://www.cnblogs.com/taozi1115402474/p/9526884.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!