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* ...
随机推荐
- HTML+CSS学习笔记(5)- 与浏览者交互,表单标签
HTML+CSS学习笔记(5)- 与浏览者交互,表单标签 1.使用表单标签,与用户交互 网站怎样与用户进行交互?答案是使用HTML表单(form).表单是可以把浏览者输入的数据传送到服务器端,这样服务 ...
- 找不到System.Runtime.Serialization.Json的解决方案
System.ServiceModel System.ServiceModel.Web System.Runtime.Serialization 三者均要添加引用
- win7下64位系统memcache/memcached安装教程
折腾了1个多小时,终于搞定.操作系统时64位的,php5.3.13 类似于上一篇的xdebug安装教程~~ memcache和memcached的区别 在自己的新程序中打算全面应用memcached ...
- 利用js来实现一些常用的算法
示例代码中的arr指的是给出的数组,s指的是数组的起始坐标0,end指的是数组的最后一个坐标arr.length-1,n指的是要查找的数字 查找某个值: 1.线性法 function findInAr ...
- Java多线程(四) 线程池
一个优秀的软件不会随意的创建.销毁线程,因为创建和销毁线程需要耗费大量的CPU时间以及需要和内存做出大量的交互.因此JDK5提出了使用线程池,让程序员把更多的精力放在业务逻辑上面,弱化对线程的开闭管理 ...
- 英文版firefox显示中文字体丑的问题
在Options里面选择Content,在Fonts&Colors区域的Default font中,选择Times New Roman 如下图1: 在旁边的Advanced中选择,Fonts ...
- javascript 代码优化工具 UglifyJS
安装: 1. 安装 node.js 环境 (这个不用我教了吧,网上教程一大堆哦.) 2. 进入 https://github.com/mishoo/UglifyJS 右上角 “Download” Z ...
- GridView编辑、取消按钮自定义控件
这个需求来自于论坛一位坛友提出的问题,他希望能够自定义编辑.取消按钮,而不是用GridView自带的编辑和取消.这里只当抛砖引玉,提出一些解决方案. 首先在页面前台设置一个GridView. < ...
- C#编写记事本(高仿)
近一周写的关于记事本的代码,高仿记事本.本人C#入门不久,其中存在代码冗余,但懒得修改了. 经测试运行正常. 一.主窗体设计及代码 namespace BestEditor { public part ...
- pdo 连接数据库 报错 could not find driver 解决方法
在windows 下,调试一个PHP程序时,报了这个错误, could not find driver 原来我的这个程序中用到了PDO对象, 连接mysql 5. 在PHP的默认设置中,只打开了ph ...