POJ-1273-Drainage Ditches(网络流之最大流)
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
Output
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(网络流之最大流)的更多相关文章
- poj 1273 Drainage Ditches 网络流最大流基础
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 59176 Accepted: 2272 ...
- POJ 1273 Drainage Ditches (网络流Dinic模板)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- POJ 1273 Drainage Ditches 网络流 FF
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 74480 Accepted: 2895 ...
- poj 1273 Drainage Ditches (网络流 最大流)
网络流模板题. ============================================================================================ ...
- poj 1273 Drainage Ditches(最大流)
http://poj.org/problem?id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Subm ...
- POJ 1273 Drainage Ditches (网络最大流)
http://poj.org/problem? id=1273 Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Sub ...
- POJ 1273 Drainage Ditches
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67387 Accepted: 2603 ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
- 网络流--最大流--POJ 1273 Drainage Ditches
链接 Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clov ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
随机推荐
- Spring AOP术语解释
话说,越来越感觉有些人解释概念真的是晦涩难懂,我刚开始学习Spring aop时,对那些切入点,连接点,引入等概念搞得头疼.太多人就直接照搬定义,让我们这些初学者如何理解啊.下面是我找了大量的博客,终 ...
- 系统引导器GRUB
系统引导器GRUB 理解/boot/grub/grub.conf 1 # grub.conf generated by anaconda 2 # 3 # Note that you do not ha ...
- Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(2)
上一篇:Ubuntu & GitLab CI & Docker & ASP.NET Core 2.0 自动化发布和部署(1) 服务器版本 Ubuntu 16.04 LTS. 本 ...
- Day-8: 面对对象编程
面对过程的程序设计方法意在将函数分成子函数,再依次调用这些函数来解决问题. 而面对对象的程序设计方法,来源于自然界,类是实例的抽象,实例是类的具体.自定义出来的对象是类,而所有的数据都可以看成是对象, ...
- Java学习记录 : 画板的实现
接触java不满一个月,看厚厚的java入门简直要醉,故利用实例来巩固所学知识. 画板的实现其实从原理来说超级简单,可能一会儿就完成了. 但作为一名强迫症患者,要实现和win下面的画板一样的功能还是需 ...
- Vuforia开发完全指南---Vuforia概述
Vuforia概述 AR(Augmented Reality)增强现实,想必大家都已经很熟悉了.这是当下最热的技术之一,是利用计算机视觉和计算机图像学领域的相关知识将虚拟世界融入到现实生活当中.AR和 ...
- 【Beta阶段】第六次scrum meeting
Coding/OSChina 地址 1. 会议内容 学号 主要负责的方向 昨日任务 昨日任务完成进度 接下去要做 99 PM 着手联网功能 100% 配合100完成联网功能 100 DEV 完善服务器 ...
- 微信小程序简单入门理解
简单的小程序示例结构: (一):理解小程序结构app.js,app.json,app.wxss ①app.js,app.json是小程序结构必要的部分,app.wxss可选择 ②app.js用于创建小 ...
- 201521123098 《Java程序设计》第6周学习总结
1. 本周学习总结 1.1 面向对象学习暂告一段落,请使用思维导图,以封装.继承.多态为核心概念画一张思维导图,对面向对象思想进行一个总结. 注1:关键词与内容不求多,但概念之间的联系要清晰,内容覆盖 ...
- 201521123016 《Java程序设计》第3周学习总结
1. 本周学习总结 2. 书面作业 2.1代码阅读 public class Test1 { private int i = 1;//这行不能修改 private static int j = 2; ...