POJ3204+DInic+maxflow
Dinic+maxflow
题意:找这样一种边的个数,就是增加该边的容量,可以使得最大流变大
思路:求maxflow,再枚举流量为0的边,增加容量,看是否能找到增广路径。
/*
Dinic+maxflow
题意:找这样一种边的个数,就是增加该边的容量,可以使得最大流变大
思路:求maxflow,再枚举流量为0的边,增加容量,看是否能找到增广路径。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<queue>
#include<map>
#include<stack>
#include<set>
#include<math.h>
using namespace std;
typedef long long int64;
//typedef __int64 int64;
typedef pair<int64,int64> PII;
#define MP(a,b) make_pair((a),(b))
const int maxn = ;
const int maxm = ;
const int inf = 0x3f3f3f3f;
const double pi=acos(-1.0);
const double eps = 1e-; struct Edge{
int u,v,next,val;
bool flag;
}edge[ maxm<< ];
int cnt,head[ maxn ];
int vis[ maxn ];
int lev[ maxn ];
int q[ maxn<< ]; void init(){
cnt = ;
memset( head,-,sizeof( head ) );
}
void addedge( int a,int b,int c ){
edge[ cnt ].u = a;
edge[ cnt ].v = b;
edge[ cnt ].val = c;
edge[ cnt ].next = head[ a ];
if( cnt%== ) edge[ cnt ].flag = true;
else edge[ cnt ].flag = false;
head[ a ] = cnt ++;
} bool bfs( int n,int start,int end ){
int head2 = ,tail2 = ;
q[ tail2++ ] = start;
memset( lev,-,sizeof( lev ) );
lev[ start ] = ;
while( head2<tail2 ){
int u = q[ head2++ ];
for( int i=head[u];i!=-;i=edge[i].next ){
int v = edge[i].v;
if( edge[i].val>&&lev[v]==- ){
lev[v] = lev[u]+;
q[ tail2++ ] = v;
}
}
}
if( lev[ end ]==- ) return false;
else return true;
} int Dinic( int n,int start,int end ){
int maxflow = ;
while( true ){
if( bfs(n,start,end )==false ) break;
int id = start;
int tail = ;
while( true ){
if( id==end ){
int flow = inf;
int flag = -;
for( int i=;i<tail;i++ ){
if( edge[ q[i] ].val<flow ){
flow = edge[ q[i] ].val ;
flag = i;
}
}
for( int i=;i<tail;i++ ){
edge[ q[i] ].val -= flow;
edge[ q[i]^ ].val += flow;
}
if( flag!=- ){
maxflow += flow;
tail = flag;
id = edge[ q[flag] ].u;
}
else
return inf;
}
id = head[ id ];
while( id!=- ){
if( edge[id].val>&&lev[edge[id].u]+==lev[edge[id].v] ){
break;
}
id = edge[ id ].next;
}
if( id!=- ){
q[ tail++ ] = id;
id = edge[ id ].v;
}
else{
if( tail== ) break;
lev[ edge[q[tail-]].v ] = -;
id = edge[ q[--tail] ].u;
}
}
}
return maxflow;
} int main(){
int n,m;
while( scanf("%d%d",&n,&m)== ){
init();
int a,b,c;
int start = ;
int end = n-;
for( int i=;i<m;i++ ){
scanf("%d%d%d",&a,&b,&c);
addedge( a,b,c );
addedge( b,a, );
}
Dinic( n,start,end );
int ans = ;
for( int i=;i<cnt;i++ ){
if( edge[i].val==&&edge[i].flag==true ){
edge[i].val ++ ;
if( bfs( n,start,end )==true ){
ans ++ ;
}
edge[i].val -- ;
}
}
printf("%d\n",ans);
}
return ;
}
POJ3204+DInic+maxflow的更多相关文章
- BZOJ 1001 - 狼抓兔子 - [Dinic最大流][对偶图最短路]
题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 Description现在小朋友们最喜欢的"喜羊羊与灰太狼", ...
- POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]
妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...
- POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]
题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...
- poj1273 Drainage Ditches Dinic最大流
Drainage Ditches Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 76000 Accepted: 2953 ...
- Dinic算法学习&&HDU2063
http://www.cnblogs.com/SYCstudio/p/7260613.html 看这篇博文懂了一点,做题再体会体会吧 找了好久都没找到一个好用的模板…… 我也是佛了..最后决定用峰神的 ...
- hdu2732 Leapin' Lizards (网络流dinic)
D - Leapin' Lizards Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u ...
- 网络流之Dinic算法
初学网络流.存一下Dinic板子. 复杂度O(n^2*m) UVA - 1515 Pool construction 把每个草地与 S 相连,花费为dig,每个洞与 T 相连,花费为 然后对于每个两个 ...
- POJ 1637 混合图的欧拉回路判定
题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...
- BZOJ4439——[Swerc2015]Landscaping
0.题目: FJ有一块N*M的矩形田地,有两种地形高地(用'#'表示)和低地(用'.'表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示 对于一个4* ...
随机推荐
- Linux命令(1):cd命令
1.作用:改变工作目录: 2.格式:cd [路径] 其中的路径为要改变的工作目录,可为相对路径或绝对路径 3.使用实例:[root@www uclinux]# cd /home/yourname/ ...
- Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型
原文 Contoso 大学 - 1 - 为 ASP.NET MVC 应用程序创建 EF 数据模型 原文地址:Creating an Entity Framework Data Model for an ...
- MD5和Base64介绍与应用
MD5:概念:MD5是一种不可逆的消息摘要算法.为计算机安全领域广泛使⽤的一种散列函数, 用以提供消息的完整性保护.效果:把一个任意长度的字节串变换成⼀定⻓度的⼗六进制数字串. 目的是让⼤容量信息在⽤ ...
- VS2013 打开项目时出现 未定义标识符string的解决办法
---恢复内容开始--- 前两天从前辈那儿弄到一份源码,VC 6时期写出来的mfc程序. 打开之后直接编译编译成功,可以运行.但是看代码的时候却发现出现了好多错误,如 未定义标识符string,NUL ...
- C#判断Textbox是否为数字
第一种方法: try { ) { //操作代码 } else { MessageBox.Show("必须是正整数"); } } catch (FormatException) { ...
- Linux学习三部曲(之一)
作为.NET程序员,一直以来都是windows环境下工作,很少接触到linux系统.但是随着微软跨出跨平台这一步之后,相信.NET程序员在linux平台进行开发也会变得越来越寻常. 所以,今天这篇文章 ...
- eclipse中英文切换--四种方式
若转载,请注明出处 http://www.cnblogs.com/last_hunter/p/5627009.html 谢谢! ------------------------------------ ...
- 在Apache中开启虚拟主机
最近在自学LAMP,在Apache中尝试着开启虚拟主机的时候,遇到了挺多麻烦的,这里也顺便总结一下,在Apache中开启虚拟主机的时候,主要有下面几个步骤: 1.新建一个文件夹作为虚拟主机,用来存储网 ...
- StringBuilder的Append()方法会比+=效率高
StringBuilder strSql = new StringBuilder(); strSql.Append("select top 1 id from " + databa ...
- mysql中limit的用法实例解析
mysql中limit的用法解析. 在mysql中,select * from table limit m,n.其中m是指记录开始的index,从0开始,n是指从第m条开始,取n条. 例如: mysq ...