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. Element is not clickable at point error in chrome

    I see this only in Chrome. The full error message reads: "org.openqa.selenium.WebDriverExceptio ...

  2. [2014-08-24]为 Xamarin Studio 创建的 Asp.Net Mvc 项目配置 gitignore

    今天在尝试 Mac 下使用 Xamarin Studio (以下简称XS) 开发 Asp.Net Mvc 项目,发现XS没启用版本控制,故自己去命令行下使用 git init,想到需要一个.gitig ...

  3. INotifyPropertyChanged(监听数据),当数据改变时调用

    public class BaseViewModel : INotifyPropertyChanged    {        public event PropertyChangedEventHan ...

  4. 为什么a标签中使用img后,高度多了几个像素?

    <li><a href="#"><img src="images/audio.jpg" alt="">& ...

  5. wifi pineapple 外接USB无线网卡桥接外网

    0:选择USB网卡 在没有有线网络的情况下,可以外挂一个usb无线网卡来桥接上网,目前支持3070L.8187L芯片的网卡,反正linux系统都用这些芯片, 免的安装驱动, 我选择的是 WN-722N ...

  6. Akka(23): Stream:自定义流构件功能-Custom defined stream processing stages

    从总体上看:akka-stream是由数据源头Source,流通节点Flow和数据流终点Sink三个框架性的流构件(stream components)组成的.这其中:Source和Sink是stre ...

  7. [js高手之路]深入浅出webpack教程系列7-( babel-loader,css-loader,style-loader)的用法

    什么是loader呢,官方解释为文件的预处理器,通俗点说webpack在处理静态资源的时候,需要加载各种loader,比如,html文件,要用html-loader, css文件要用css-loade ...

  8. 比较三个 CSS 预处理器:Sass、LESS 和 Stylus(上)

    前沿 : 第一次写不够成熟,可能描述有所错误,还请大家修改指正,我会对已完成的文章进行修改. 一.什么是CSS预处理器 CSS预处理器定义了一种新的语言,基本的思想是用一种专门的编程语言,开发者只需要 ...

  9. 汇编指令-MOV与ldr区别(7)

    MOV 1.可以寄存器与寄存器之间传递数据 2.可以常数传递到寄存器中(常数不能超过32位) LDR 1.可以地址与寄存器之间的数据传递 2.也可以常数传递到寄存器中 实例: 1.r1与r2之间传递就 ...

  10. [2017BUAA软工]第0次个人作业

    第一部分:结缘计算机 1.你为什么选择计算机专业?你认为你的条件如何?和这些博主比呢? 我觉得我选择计算机系完全是误打误撞吧.当时我的分数上北航是没问题的,所以填专业时就是机械,电气,自动化,计算机等 ...