POJ1273Drainage Ditches[最大流]
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 71559 | Accepted: 27846 |
Description
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
Source
Dinic算法的基本思路:
- 根据残量网络计算层次图。
- 在层次图中使用DFS进行增广直到不存在增广路
- 重复以上步骤直到无法增广
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
const int N=,M=,INF=1e9;
int read(){
char c=getchar();int x=,f=;
while(c<''||c>''){if(c=='-')f=-; c=getchar();}
while(c>=''&&c<=''){x=x*+c-''; c=getchar();}
return x*f;
}
int n,m,u,v,c;
struct edge{
int v,ne,f,c;
}e[M<<];
int h[N],cnt=;
void ins(int u,int v,int c){
cnt++;
e[cnt].v=v;e[cnt].c=c;e[cnt].f=;e[cnt].ne=h[u];h[u]=cnt;
cnt++;
e[cnt].v=u;e[cnt].c=;e[cnt].f=;e[cnt].ne=h[v];h[v]=cnt;
}
int cur[N],d[N],s,t;
int vis[N],q[N],head=,tail=; int stop=;
bool bfs(){
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
head=tail=;
q[tail++]=s;d[s]=;vis[s]=;
while(head!=tail){
int u=q[head++];
for(int i=h[u];i;i=e[i].ne){
int v=e[i].v;
if(!vis[v]&&e[i].f<e[i].c){
q[tail++]=v;vis[v]=;
d[v]=d[u]+;
if(v==t) return ;
}
}
}
return ;
} int dfs(int u,int a){
if(u==t||a==) return a;
int flow=,f;
for(int &i=cur[u];i;i=e[i].ne){
int v=e[i].v;
if(d[v]==d[u]+&&(f=dfs(v,min(a,e[i].c-e[i].f)))>){
flow+=f;
e[i].f+=f;
e[((i-)^)+].f-=f;
a-=f;
if(a==) break;
}
}
return flow;
}
int dinic(){
int flow=;
while(bfs()){
for(int i=s;i<=t;i++) cur[i]=h[i];
flow+=dfs(s,INF);
}
return flow;
}
int main(){
while(scanf("%d%d",&m,&n)!=EOF){
cnt=;
memset(h,,sizeof(h));
for(int i=;i<=m;i++){
u=read();v=read();c=read();
ins(u,v,c);
}
s=;t=n;
printf("%d\n",dinic());
}
}
POJ1273Drainage Ditches[最大流]的更多相关文章
- POJ-1273-Drainage Ditches(网络流之最大流)
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...
- poj1273--Drainage Ditches(最大流Edmond-Karp算法 邻接表实现)
最大流模板题 大部分Edmond-Karp算法代码都是邻接矩阵实现,试着改成了邻接表. #include <iostream> #include <cstdio> #inclu ...
- 图论-网络流-最大流--POJ1273Drainage Ditches(Dinic)
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 91585 Accepted: 3549 ...
- POJ-1273Drainage Ditches(网络流入门题,最大流)
Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover patch. This ...
- 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)
#include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...
- Poj 1273 Drainage Ditches(最大流 Edmonds-Karp )
题目链接:poj1273 Drainage Ditches 呜呜,今天自学网络流,看了EK算法,学的晕晕的,留个简单模板题来作纪念... #include<cstdio> #include ...
- poj 1273 Drainage Ditches 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 || HDU 1532 Drainage Ditches (最大流模型)
Drainage DitchesHal Burch Time Limit 1000 ms Memory Limit 65536 kb description Every time it rains o ...
- POJ-1273-Drainage Ditches 朴素增广路
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 70588 Accepted: 2743 ...
随机推荐
- 响应式WEB设计的9项基本原则
响 应式Web设计对于解决多类型屏幕问题来说是个不错方案,但从印刷的角度来看,其却存在着很多的困难.没有固定的页面尺寸.没有毫米或英寸,没有任何物理 限制,让人感到无从下手.随着建立网站可用的各种小工 ...
- 应用程序启动管理 Winform版
★前言 开发这个小工具的想法主要是机器中安装了数据库,每次设置开机启动数据库服务的话,则系统启动很慢,每次都得手动到服务管理器中停止服务,很是繁琐,相信不少做开发的朋友会遇到同样的问题,就有 ...
- 使用SwipeListView实现滑动效果
QQ的滑动删除效果很不错,要实现这种效果,可以使用SwipeListView.1. 下载com.fortysevendeg.swipelistview这个项目(以前GitHub上有,现在GitHub上 ...
- AMD and CMD are dead之KMDjs在JS工程化的努力
总览 kmdjs发布了最接近最终版本的0.0.4版本https://github.com/kmdjs/kmdjs,你已经完全可以在项目中使用.我已经无法用语言形容其完美程度.借用我发的微博: 模块 ...
- jsonp 演示实例 —— 基于node
序 同源策略是浏览器处于安全考虑,为通信设置了"相同的域.相同的端口.相同的协议"这一限制.这让我们的ajax请求存在跨域无权限访问的问题. 同时我们发现script标签引入脚本的 ...
- iOS多线程之2.NSThread的加锁@synchronized
我在上一篇文章讲了线程的生命周期,这篇文章来讲讲线程加锁的注意事项与@synchronized关键字. 那什么时候需要加锁呢,就是当多条线程同时操作一个变量时,就需要加锁了.至于为什么要加锁,可以 ...
- Linux0.11内核--引导程序分析
1.简介 本文主要介绍三个文件bootsect.s.setup.s.head.s,主要是做了些从软盘加载内核和设置32位保护模式的操作. 2.程序分析 当PC电源打开后,BIOS自检后将bootsec ...
- 关于iOS的runtime
runtime是一个很有意思的东西,如果你学iOS开发很经常就会用到或被问到runtime.那么runtime是什么呢,如何去了解它. runtime:中文名 运行时,系统在编译时留下的一些 类型,操 ...
- 【代码笔记】iOS-UIView的placeholder的效果
一,效果图. 二,工程图. 三,代码. RootViewController.h #import <UIKit/UIKit.h> @interface RootViewController ...
- 基于Server-Sent Event的简单在线聊天室
Web即时通信 所谓Web即时通信,就是说我们可以通过一种机制在网页上立即通知用户一件事情的发生,是不需要用户刷新网页的.Web即时通信的用途有很多,比如实时聊天,即时推送等.如当我们在登陆浏览知乎时 ...