Drainage Ditches
Hal Burch

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. Note however, that there can be more than one ditch between two intersections.

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.

PROGRAM NAME: ditch

INPUT FORMAT

Line 1: 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.
Line 2..N+1: Each of 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.

SAMPLE INPUT (file ditch.in)

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

OUTPUT FORMAT

One line with a single integer, the maximum rate at which water may emptied from the pond.

SAMPLE OUTPUT (file ditch.out)

50
————————————————————————————————————————————————
它有个俗名叫草地排水
 /*
ID:ivorysi
PROG:ditch
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#define inf 0x7fffffff
#define ivorysi
#define siji(i,x,y) for(int i=x;i<=y;++i)
#define gongzi(j,x,y) for(int j=x;j>=y;--j)
#define xiaosiji(i,x,y) for(int i=x;i<y;++i)
#define sigongzi(j,x,y) for(int j=x;j>y;--j)
using namespace std;
struct node {
int to,next,val;
}edge[];
int head[],sum=;
void add(int u,int v,int c) {
edge[++sum].to=v;
edge[sum].next=head[u];
edge[sum].val=c;
head[u]=sum;
}
void AddTwoWays(int u,int v,int c) {
add(u,v,c);
add(v,u,);//反向边
}
int n,m,dis[],gap[],ans;
int sap(int u,int aug){//O(玄学)
if(u==m) return aug;
int dmin=m-,flow=,v,t; for(int i=head[u];i;i=edge[i].next) {
v=edge[i].to;
if(edge[i].val>) {//只有还能流动的地方才能分层
if(dis[u]==dis[v]+) {
t=sap(v,min(aug-flow,edge[i].val));//限制流量不超过该点流量和边的限制
edge[i].val-=t;
edge[i^].val+=t;
flow+=t;
if(aug==flow) return flow;//流尽
if(dis[]>=m) return flow;//搜索已在某处达到结束条件
}
dmin=min(dmin,dis[v]);
}
}
if(!flow) {
--gap[dis[u]];
if(gap[dis[u]]==) dis[]=m;//出现断层说明无法再流
dis[u]=dmin+;//分层
++gap[dis[u]];
}
return flow;
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("ditch.in","r",stdin);
freopen("ditch.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
scanf("%d%d",&n,&m);
int u,v,c;
siji(i,,n) {
scanf("%d%d%d",&u,&v,&c);
AddTwoWays(u,v,c);
}
while(dis[]<m) {ans+=sap(,inf);}
printf("%d\n",ans);
return ;
}
 

USACO 4.2 Drainage Ditches(网络流模板题)的更多相关文章

  1. POJ 1273:Drainage Ditches 网络流模板题

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

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

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

  3. Drainage Ditches~网络流模板

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

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

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

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

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

  6. NYOJ 323 Drainage Ditches 网络流 FF 练手

    Drainage Ditches 时间限制:1000 ms  |  内存限制:65535 KB 难度:4 描述 Every time it rains on Farmer John's fields, ...

  7. Drainage Ditches--hdu1532(网络流 模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=1532 Drainage Ditches Time Limit: 2000/1000 MS (Java/Other ...

  8. [POJ1273][USACO4.2]Drainage Ditches (网络流最大流)

    题意 网络流最大流模板 思路 EK也不会超时 所以说是一个数据比较水的模板题 但是POJ有点坑,多组数据,而且题目没给 哭得我AC率直掉 代码 用的朴素Dinic #include<cstdio ...

  9. HDU1532 Drainage Ditches 网络流EK算法

    Drainage Ditches Problem Description Every time it rains on Farmer John's fields, a pond forms over ...

随机推荐

  1. Extjs grid分页多选记忆功能

    很多同事在用extjs grid做分页的时候,往往会想用grid的多选功能来实现导出Excel之类的功能(也就是所谓的多选记忆功能),但在选选择下一页的时候 上一页选中的已经清除 这是因为做分页的时候 ...

  2. jquery实现文字上下滚动效果

    文字上下滚动是经常用到的js效果,这里介绍一种上下渐隐渐出的文字展现效果! 代码实现很简单,只需要引入jquery就可以. 代码如下: <!DOCTYPE> <head> &l ...

  3. 实现透明渐变的Activity

    如果光是透明全屏的Activity的话,直接继承内置theme即可 <activity android:theme="@android:style/Theme.NoTitleBar.F ...

  4. WebService和AngularJS实现模糊过滤查询

    WebService和AngularJS实现模糊过滤查询   [概要] 网上看到一个不错的帖子,用WebService获取json,然后在前端使用AngularJs进行过滤搜索,看完文章后,按自己的想 ...

  5. Node.js基础知识

    Node.js入门   Node.js     Node.js是一套用来编写高性能网络服务器的JavaScript工具包,一系列的变化由此开始.比较独特的是,Node.js会假设在POSIX环境下运行 ...

  6. Java中的嵌套类和内部类

    以前看<Java编程思想>的时候,看到过嵌套类跟内部类的区别,不过后来就把它们的概念给忘了吧.昨天在看<数据结构与算法分析(Java语言版)>的时候,又遇到了这个概念,当时就很 ...

  7. JAVA学习:多态

    多态:可以理解为事物存在的多种体现形态.   人:男人,女人 动物:猫,狗. 猫 x = new 猫(); 动物 x = new 猫()   1,多态的体现 父类的引用指向了自己的子类对象. 父类的引 ...

  8. MongoDb的“not master and slaveok=false”错误及解决方法,读写分离

    首先这是正常的,因为SECONDARY是不允许读写的, 在写多读少的应用中,使用Replica Sets来实现读写分离.通过在连接时指定或者在主库指定slaveOk,由Secondary来分担读的压力 ...

  9. Django模板引擎的研究

    Django模板引擎的研究 原创博文,转载请注明出处. 以前曾遇到过错误Reverse for ‘*’ with arguments '()' and keyword arguments' not f ...

  10. cocos2d-x C++的do...while(0)另类使用方法

    在C++中,有三种类型的循环语句:for, while, 和do...while, 但是在一般应用中作循环时, 我们可能用for和while要多一些,do...while相对不受重视.      但是 ...