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

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

时间: 2024-10-10 04:59:14

TOJ 4085 Drainage Ditches(最大流)的相关文章

POJ 1273 Drainage Ditches 最大流

很裸的最大流问题,不过注意会有重边,o(╯□╰)o,被阴了WA了一发 还有就是要用long long #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <climits> #include <string> #include <iostream> #include <map> #include

hdu 1532 Drainage Ditches(最大流)

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 drai

poj-1273 Drainage Ditches(最大流基础题)

题目链接: Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67475   Accepted: 26075 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 cover

poj 1273 Drainage Ditches (最大流入门)

1 /****************************************************************** 2 题目: Drainage Ditches(POJ 1273) 3 链接: http://poj.org/problem?id=1273 4 题意: 现在有m个池塘(从1到m开始编号,1为源点,m为汇点),及n条 5 水渠,给出这n条水渠所连接的池塘和所能流过的水量,求水 6 渠中所能流过的水的最大容量.水流是单向的. 7 算法: 最大流之增广路(入门)

Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )

题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... 1 #include<cstdio> 2 #include<vector> 3 #include<queue> 4 #include<cstring> 5 #include<set> 6 #include<algorithm> 7 #define CLR(a,b) memset((a),(b),si

poj 1273 Drainage Ditches 最大流入门题

题目链接:http://poj.org/problem?id=1273 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

[POJ 1273] Drainage Ditches &amp; 最大流Dinic模板

Drainage Ditches 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

POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)

http://poj.org/problem?id=1273 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 Jo

HDU 1532 Drainage Ditches(最大流 EK算法)

题目网址:http://acm.hdu.edu.cn/showproblem.php?pid=1532 思路: 网络流最大流的入门题,直接套模板即可~ 注意坑点是:有重边!!读数据的时候要用"+="替换"=". 对网络流不熟悉的,给一篇讲解:http://www.cnblogs.com/ZJUT-jiangnan/p/3632525.html. ?(? ? ??)我是看这篇博客才入门的. 代码: 1 #include <cstdio> 2 #includ