Drainage Ditches
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 63339   Accepted: 24434

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 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10

Sample Output

50

给了M个池塘,N个水渠,以及每个水渠连接的两个点和水渠的容量。(编号1为源点,编号M为汇点)。求整个网络中最大能流的水的流量。

网络流模板题。

代码:

#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <queue>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; const int sum = 201;
const int INF = 99999999;
int cap[sum][sum],flow[sum][sum],a[sum],p[sum]; int N,M; void Edmonds_Karp()
{
int u,t,start=1,result=0;
queue <int> s;
while(s.size())s.pop(); while(1)
{
memset(a,0,sizeof(a));
memset(p,0,sizeof(p)); a[1]=INF;
s.push(1); while(s.size())
{
u=s.front();
s.pop(); for(t=1;t<=M;t++)
{
if(!a[t]&&flow[u][t]<cap[u][t])
{
s.push(t);
p[t]=u;
a[t]=min(a[u],cap[u][t]-flow[u][t]);//要和之前的那个点,逐一比较,到M时就是整个路径的最小残量
}
}
}
if(a[M]==0)
break;
result += a[M]; for(u=M;u!=1;u=p[u])
{
flow[p[u]][u] += a[M];
flow[u][p[u]] -= a[M];
}
}
cout<<result<<endl;
} int main()
{
int i,u,v,value;
while(scanf("%d%d",&N,&M)==2)
{
memset(cap,0,sizeof(cap));
memset(flow,0,sizeof(flow)); for(i=1;i<=N;i++)
{
scanf("%d%d%d",&u,&v,&value);
cap[u][v] += value;
}
Edmonds_Karp();
}
return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

POJ 1273:Drainage Ditches 网络流模板题的更多相关文章

  1. poj 1273 Drainage Ditches (网络流 最大流)

    网络流模板题. ============================================================================================ ...

  2. POJ 1273 Drainage Ditches (网络流Dinic模板)

    Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...

  3. USACO 4.2 Drainage Ditches(网络流模板题)

    Drainage DitchesHal Burch Every time it rains on Farmer John's fields, a pond forms over Bessie's fa ...

  4. POJ 1273 Drainage Ditches 网络流 FF

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 74480   Accepted: 2895 ...

  5. poj 1273 Drainage Ditches 网络流最大流基础

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 59176   Accepted: 2272 ...

  6. HDU 1532 Drainage Ditches(网络流模板题)

    题目大意:就是由于下大雨的时候约翰的农场就会被雨水给淹没,无奈下约翰不得不修建水沟,而且是网络水沟,并且聪明的约翰还控制了水的流速, 本题就是让你求出最大流速,无疑要运用到求最大流了.题中m为水沟数, ...

  7. POJ 1273 Drainage Ditches

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 67387   Accepted: 2603 ...

  8. poj 1273 Drainage Ditches(最大流)

    http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Subm ...

  9. POJ 1273 Drainage Ditches (网络最大流)

    http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Sub ...

随机推荐

  1. Unable to execute dex:Multuple dex files define 解决方法

    困扰我两天的问题终于解决了,在网上查的方法无非有三种 一. Eclipse->Project->去掉Build Automatically->Clear ->Build Pro ...

  2. Spring Boot-运行部署

    Main方法 直接运行启动类main方法 遵循应用程序入口点的Java约定的标准方法.我们的main方法SpringApplication通过调用委托给Spring Boot的类run. Spring ...

  3. 解决phpinfo 和php -v版本不一致的问题

    解决方法:参考博文 http://blog.csdn.net/laomengnevergiveup/article/details/52104862

  4. 008-PHP定义数组

    <?php /*定义数组$Cities[]*/ $Cities[0] = "北京"; $Cities[1] = "天津"; $Cities[2] = &q ...

  5. 四、Vue过渡与动画、过渡css类名、自定义指定、过滤器

    一.过渡 动画 1.1简单的过渡动画使用 parent.vue [0]定义一个待显示的数据 [1]定义一个显示隐藏flag [2]使用动画过滤标签,name用来连接style样式:v-show用来控制 ...

  6. IDEA快速定位一个文件到项目目录

    第一步:快捷键搜索java文件关键字 快捷键Ctrl+N,如果设置为Eclipse版本快捷键为Ctrl+Shift+R 第二步:定位文件到项目目录中 1.在当前文件下 2.点击定位按钮 3.定位到项目 ...

  7. Java按位运算符之按位取反

    一 数据储存形式 二进制在内存中以补码的形式存在. 补码首位是符号位,0表示该数是正数,1表示该数是负数.   例如:   数值 带符号的二进制原码 (首位表示符号位) 补码 内存中的形式 (*表示无 ...

  8. Golang的基础数据类型-字符串型

    Golang的基础数据类型-字符串型 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.字符型概述 上一篇博客学习了使用单引号括起来的单个字符就是字符类型,在Golang中有两种表 ...

  9. Codeforces735D Taxes(哥德巴赫猜想)

    题意:已知n元需缴税为n的最大因子x元.现通过将n元分成k份的方式来减少缴税.问通过这种处理方式需缴纳的税费. 分析: 1.若n为素数,不需分解,可得1 2.若n为偶数,由哥德巴赫猜想:一个大于2的偶 ...

  10. python学习笔记2018-9-17

    1.print("{0:^30}\n{1:^30}\n{1:10}".format("age","name")) {0:^30}中的0是一个 ...