POJ 1273 Drainage Ditches -dinic
dinic版本
感觉dinic算法好帅,比Edmonds-Karp算法不知高到哪里去了
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
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
Sample Input
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
Sample Output
50
/*Dinic*/
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<queue>
using namespace std;
const int inf=0x7fffffff;
int n,m;//路径数 结点数
int fl[][],dis[];
int bfs(){
// memset(dis,-1,sizeof(dis));
for(int i=;i<=m+;i++)dis[i]=-;
dis[]=;//原点层数为0
queue<int> q;
q.push();
while(!q.empty()){
int k=q.front();q.pop();
for(int i=;i<=m;i++){
if(fl[k][i]> &&dis[i]<){
dis[i]=dis[k]+;
q.push(i);
}
}
}
// return (dis[m]>0);
if(dis[m]>)return ;
else return ;
}
int dfs(int q,int mx){
if(q==m)return mx;
int i,a;
for(i=;i<=m;i++){
if(fl[q][i]> && dis[i]==dis[q]+ && (a=dfs(i,min(fl[q][i],mx))) ){
fl[i][q]+=a;
fl[q][i]-=a;
return a;
}
}
return ;
}
int main(){ int i,j,u,v,d;
while(scanf("%d%d",&n,&m)!=EOF){
memset(fl,,sizeof(fl)); for(i=;i<=n;i++){
scanf("%d%d%d",&u,&v,&d);
fl[u][v]+=d;
}
int ans=;
int res;
while(bfs()){
while(res=dfs(,inf))ans+=res;
}
printf("%d\n",ans);
}
return ;
}
POJ 1273 Drainage Ditches -dinic的更多相关文章
- 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 - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- POJ 1273 Drainage Ditches(网络流dinic算法模板)
POJ 1273给出M条边,N个点,求源点1到汇点N的最大流量. 本文主要就是附上dinic的模板,供以后参考. #include <iostream> #include <stdi ...
- 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 最大流入门题
题目链接:http://poj.org/problem?id=1273 Every time it rains on Farmer John's fields, a pond forms over B ...
- POJ 1273 Drainage Ditches题解——S.B.S.
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 67823 Accepted: 2620 ...
- 网络流最经典的入门题 各种网络流算法都能AC。 poj 1273 Drainage Ditches
Drainage Ditches 题目抽象:给你m条边u,v,c. n个定点,源点1,汇点n.求最大流. 最好的入门题,各种算法都可以拿来练习 (1): 一般增广路算法 ford() #in ...
- POJ 1273 Drainage Ditches(网络流,最大流)
Description Every time it rains on Farmer John's fields, a pond forms over Bessie's favorite clover ...
随机推荐
- 查看和设置oracle数据库的最大连接数
当前的连接数 select count(*) from v$process; 设置的最大连接数(默认值为150) select value from v$parameter where name = ...
- GitHub入门之一:使用github下载项目
git作为目前比较流行的版本控制系统,被各个互联网公司广泛使用着.目前国外的网站有GitHub,国内的有CSDN和OSCHINA的git. 使用git可以很方便地进行多人协作和版本控制.作为一个入门小 ...
- 运维工作中常用到的几个rsync同步命令
作为一个运维工程师,经常可能会面对几十台.几百台甚至上千台服务器,除了批量操作外,环境同步.数据同步也是必不可少的技能.说到“同步”,不得不提的利器就是rsync. 下面结合本人近几年运维工作中对这一 ...
- java中的单引号和双引号
1.单引号引的数据 是char类型的,双引号引的数据 是String类型的:单引号只能引一个字符,而双引号可以引0个及其以上.char只是一个基本类型,而String 可以是一个类,可以直接引用.比如 ...
- python算法:rangeBitwiseAnd(连续整数的与)
def rangeBitwiseAnd(self, m, n): i = 0 while m != n: m >>= 1 n >>= 1 i += 1 return n < ...
- 19Mybatis_订单商品数据模型分析
这篇文章是对订单商品数据模型进行分析(会给出分析思路),有四张表.这篇文章是后续文章的基础,因为后续的文章要针对这个数据模型(四张表)进行一对一,一对多,多对多进行查询. 我们以后会碰到各种各样的数据 ...
- [6]Telerik TreeView 复选框
参考连接:http://demos.telerik.com/aspnet-mvc/razor/treeview/clientsideapi 问题: Telerik TreeView 选择或取消 父选项 ...
- 【转】【C#】异常类 Exception 枚举所有类型的异常
一.基础 在C# 里,异常处理就是C# 为处理错误情况提供的一种机制.它为每种错误情况提供了定制的处理方式,并且把标识错误的代码与处理错误的代码分离开来. 对.NET类来说,一般的 异常类System ...
- WorldWind源码剖析系列:BMNG类构造函数深入分析
BMNG构造函数深入分析 一.主要类图 二.主要功能: 1) BMNG类 BMNG类将包含以“Blue Marble”为主题的所有可渲染影像的根节点添加到当前星球的可渲染对象列表中,包括 ...
- python中class 的一行式构造器
好处:避免类初始化时大量重复的赋值语句 用到了魔法__dict__ # 一行式构造器 class Test(): # 初始化 def __init__(self, a, b, c=2, d=3, e= ...