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. Mysql分区表使用的一些限制和需要注意的地方

    详见:http://blog.yemou.net/article/query/info/tytfjhfascvhzxcyt347 mysql分区策略都基于两个非常重要的假设:查询都能够过滤(prunn ...

  2. 第2阶段——编写uboot之启动内核和制作Makefile(2)

    目标: 1   添加头文件setup.h和serial.h 2   写main函数   2.1 帮内核设置串口0, (内核启动会打印出启动信息) 2.2把内核读入到SDRAM 2.3设置参数(参考u- ...

  3. SourceTree使用方法介绍

    SourceTree比命令行更容易操作,能更直观看到发生了什么.但是没有哪一家git图形化软件能完成git的所有操作,封装后的使用也隐藏了git的一些细节,在图形化工具出现一些非常罕见的情况时,还是需 ...

  4. 深入理解计算机系统(2.3)------布尔代数以及C语言运算符

    本篇博客我们主要讲解计算机中的布尔代数以及C语言的几个运算符. 1.布尔代数 我们知道二进制值是计算机编码.存储和操作信息的核心,随着计算机的发展,围绕数值0和1的研究已经演化出了丰富的数学知识体系. ...

  5. Java学习10——package和import

    package和import语句 为了便于管理大型软件系统中数目众多的类,解决类的命名冲突问题,Java引入包(package)机制,提供类的多重类命名空间,使用时,import引入相应package ...

  6. Java学习2——HelloWorld(编写第一个java程序)

    编写 在自己的工作文件目录下(如上一篇中配置的classpath路径)创建HelloWorld.java文件,编写如下代码,并保存 public class HelloWorld { public s ...

  7. 201521123106 《Java程序设计》第8周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 1.2 选做:收集你认为有用的代码片段 2. 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1 ...

  8. 201521044091 《java程序设计》第八周学习总结

    本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容.1.2 选做:收集你认为有用的代码片段 书面作业 本次作业题集集合 List中指定元素的删除(题目4-1)1.1 实验 ...

  9. 201521123019 《Java程序设计》第5周学习总结

    1. 本章学习总结 2. 书面作业 一.代码阅读:Child压缩包内源代码 1.1 com.parent包中Child.java文件能否编译通过?哪句会出现错误?试改正该错误.并分析输出结果. Ans ...

  10. 201521123122 《java程序设计》 第三周学习总结

    1. 本章学习总结 你对于本章知识的学习总结 链接点击此处 2. 书面作业 代码阅读 public class Test1 { private int i = 1;//这行不能修改 private s ...