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

题解

这道题是一道裸的最大流,没什么好说的

不过这里有一个坑

每次加边的head数组要初始化为-1,自己以前都是0,被坑了

 #include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define N 205
#define MAX 1e8
using namespace std;
int n,m,x,y,z,tot,ans,fee,Min;
int head[N],level[N];
struct node{
int next,to,fee;
}e[*N];
void add(int x,int y,int z){
e[tot].next=head[x];
head[x]=tot;
e[tot].to=y;
e[tot].fee=z;
tot++;
e[tot].next=head[y];
head[y]=tot;
e[tot].to=x;
e[tot].fee=;
tot++;
}
queue<int> q;
bool bfs(int s,int t){
memset(level,,sizeof(level));
level[s]=;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty()){
int k=q.front();
q.pop();
if (k==t) return true;
for (int i=head[k];i!=-;i=e[i].next){
int v=e[i].to;
if (e[i].fee&&!level[v]){
level[v]=level[k]+;
q.push(v);
}
}
}
return false;
}
int dfs(int s,int maxf,int t){
if (s==t) return maxf;
int ret=;
for (int i=head[s];i!=-;i=e[i].next){
int v=e[i].to;
fee=e[i].fee;
if (level[v]==level[s]+){
Min=min(maxf-ret,fee);
fee=dfs(v,Min,t);
e[i].fee-=fee;
e[i^].fee+=fee;
ret+=fee;
if (ret==maxf) return ret;
}
}
return ret;
}
int Dinic(int s,int t){
ans=;
while (bfs(s,t)) ans+=dfs(s,MAX,t);
return ans;
}
int main(){
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof(head));
for (int i=;i<=n;i++)
scanf("%d%d%d",&x,&y,&z),add(x,y,z);
printf("%d\n",Dinic(,m));
}
return ;
}

这是之前做的

现在发现Dinic有一个不错的优化

就是在dfs找答案的时候判断答案是否为0,为0的话就说明当前这个点到达不了汇点,那么直接把level改为0,这样可以减少很多重复的操作

因为有可能很多的层次网络都是经过s的,那么把s的level改掉后就有很多不用做了

其实就多了一句话而已

 #include<algorithm>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#define N 205
#define MAX 1e8
using namespace std;
int n,m,x,y,z,tot,ans,fee,Min;
int head[N],level[N];
struct node{
int next,to,fee;
}e[*N];
void add(int x,int y,int z){
e[tot].next=head[x];
head[x]=tot;
e[tot].to=y;
e[tot].fee=z;
tot++;
e[tot].next=head[y];
head[y]=tot;
e[tot].to=x;
e[tot].fee=;
tot++;
}
queue<int> q;
bool bfs(int s,int t){
memset(level,,sizeof(level));
level[s]=;
while (!q.empty()) q.pop();
q.push(s);
while (!q.empty()){
int k=q.front();
q.pop();
if (k==t) return true;
for (int i=head[k];i!=-;i=e[i].next){
int v=e[i].to;
if (e[i].fee&&!level[v]){
level[v]=level[k]+;
q.push(v);
}
}
}
return false;
}
int dfs(int s,int maxf,int t){
if (s==t) return maxf;
int ret=;
for (int i=head[s];i!=-;i=e[i].next){
int v=e[i].to;
fee=e[i].fee;
if (level[v]==level[s]+){
Min=min(maxf-ret,fee);
fee=dfs(v,Min,t);
e[i].fee-=fee;
e[i^].fee+=fee;
ret+=fee;
if (ret==maxf) return ret;
}
}
if (!ret) level[s]=; //这里是关键
return ret;
}
int Dinic(int s,int t){
ans=;
while (bfs(s,t)) ans+=dfs(s,MAX,t);
return ans;
}
int main(){
while (~scanf("%d%d",&n,&m)){
tot=;
memset(head,-,sizeof(head));
for (int i=;i<=n;i++)
scanf("%d%d%d",&x,&y,&z),add(x,y,z);
printf("%d\n",Dinic(,m));
}
return ;
}

POJ-1273-Drainage Ditches(网络流之最大流)的更多相关文章

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

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

  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. POJ 1273 Drainage Ditches 网络流 FF

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

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

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

  5. poj 1273 Drainage Ditches(最大流)

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

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

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

  7. POJ 1273 Drainage Ditches

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

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

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

  9. 网络流--最大流--POJ 1273 Drainage Ditches

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

  10. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

随机推荐

  1. 深入浅出数据结构C语言版(16)——插入排序

    从这一篇博文开始,我们将开始讨论排序算法.所谓排序算法,就是将给定数据根据关键字进行排序,最终实现数据依照关键字从小到大或从大到小的顺序存储.而这篇博文,就是要介绍一种简单的排序算法--插入排序(In ...

  2. Android事件传递机制详解及最新源码分析——Activity篇

    版权声明:本文出自汪磊的博客,转载请务必注明出处. 在前两篇我们共同探讨了事件传递机制<View篇>与<ViewGroup篇>,我们知道View触摸事件是ViewGroup传递 ...

  3. Centos 6启动流程详解

    author:JevonWei 版权声明:原创作品 Centos6 启动流程 POST开机自检 当按下电源键后,会启动ROM芯片中的CMOS程序检查CPU.内存等硬件设备是否正常运行,CMOS中的程序 ...

  4. 10个经典的Java面试题集合(转载)

    1.Java的HashMap是如何工作的? HashMap是一个针对数据结构的键值,每个键都会有相应的值,关键是识别这样的值. HashMap 基于 hashing 原理,我们通过 put ()和 g ...

  5. 最近见到的JS返回函数的一些题

    JS返回值题一直都是考察重点,面试和笔试之中也经常涉及到,说一说我最近遇到的一些有意思的JS返回函数问题. 之前见到过一道有意思的问题,说有一个sum函数,用户可以通过sum(2,3)来取到2+3 = ...

  6. java-多个数的和

    目的:实现多个整数相加. 思路:1.首先要确定用户所需整数的个数n,此部分由用户在键盘上输入. 2.创建一个长度为n的数组. 3.用户从键盘上输入n个整数并判断是否输入正确,正确则存入数组,否则重新输 ...

  7. grunt之watch续

    上一回没有说完,我就是这样,做之前心中波澜壮阔,锦绣山河,等画完小草开始烦躁,完成鲜花出现动摇,然后心神涣散,最后虎头蛇尾. 现在弥补下之前遗漏的问题. watch(V0.6.1)的tasks和opt ...

  8. 201521123090 《Java程序设计》第5周学习总结

    1. 本周学习总结 1.1 尝试使用思维导图总结有关多态与接口的知识点. 1.2 可选:使用常规方法总结其他上课内容. 参考资料: 2. 书面作业 作业参考文件下载 1.代码阅读:Child压缩包内源 ...

  9. 201521123013 《Java程序设计》第4周学习总结

    1. 本章学习总结 1.1 尝试使用思维导图总结有关继承的知识点. 1.2 使用常规方法总结其他上课内容. 1.多态是面向对象的三大特性之一.多态的意思:相同的形态,可以实不同的行为.Java中实现多 ...

  10. 201521123044 《Java程序设计》第2周作业-Java基本语法与类库

    1. 本章学习总结 ·1.浮点型的不精确,不能简单的像C语言一样用float或者double来定义.在java中有更精确的BigDecimal类. 举例:BigDecimal bd1= new Big ...