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的更多相关文章

  1. BZOJ 1001 - 狼抓兔子 - [Dinic最大流][对偶图最短路]

    题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1001 Description现在小朋友们最喜欢的"喜羊羊与灰太狼", ...

  2. POJ 1815 - Friendship - [拆点最大流求最小点割集][暴力枚举求升序割点] - [Dinic算法模板 - 邻接矩阵型]

    妖怪题目,做到现在:2017/8/19 - 1:41…… 不过想想还是值得的,至少邻接矩阵型的Dinic算法模板get√ 题目链接:http://poj.org/problem?id=1815 Tim ...

  3. POJ 1273 - Drainage Ditches - [最大流模板题] - [EK算法模板][Dinic算法模板 - 邻接表型]

    题目链接:http://poj.org/problem?id=1273 Time Limit: 1000MS Memory Limit: 10000K Description Every time i ...

  4. poj1273 Drainage Ditches Dinic最大流

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 76000   Accepted: 2953 ...

  5. Dinic算法学习&&HDU2063

    http://www.cnblogs.com/SYCstudio/p/7260613.html 看这篇博文懂了一点,做题再体会体会吧 找了好久都没找到一个好用的模板…… 我也是佛了..最后决定用峰神的 ...

  6. hdu2732 Leapin' Lizards (网络流dinic)

    D - Leapin' Lizards Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u ...

  7. 网络流之Dinic算法

    初学网络流.存一下Dinic板子. 复杂度O(n^2*m) UVA - 1515 Pool construction 把每个草地与 S 相连,花费为dig,每个洞与 T 相连,花费为 然后对于每个两个 ...

  8. POJ 1637 混合图的欧拉回路判定

    题意:一张混合图,判断是否存在欧拉回路. 分析参考: 混合图(既有有向边又有无向边的图)中欧拉环.欧拉路径的判定需要借助网络流! (1)欧拉环的判定:一开始当然是判断原图的基图是否连通,若不连通则一定 ...

  9. BZOJ4439——[Swerc2015]Landscaping

    0.题目: FJ有一块N*M的矩形田地,有两种地形高地(用'#'表示)和低地(用'.'表示) FJ需要对每一行田地从左到右完整开收割机走到头,再对每一列从上到下完整走到头,如下图所示   对于一个4* ...

随机推荐

  1. MyBatis参数传入集合之foreach动态sql

    foreach的主要用在构建in条件中,它可以在SQL语句中进行迭代一个集合.foreach元素的属性主要有item,index,collection,open,separator,close.ite ...

  2. 关键字 static

    A. 面向过程: 1. 静态全局变量:在全局变量前,加上关键词static,该变量就被定义成为一个静态全局变量. 1.1. 特点: |该变量在全局数据区分配内存: |未经初始化的静态全局变量会被程序自 ...

  3. Libcurl笔记二

    一: multi与easy接口的不同处The multi interface offers several abilities that the easy interface doesn't. The ...

  4. Asp.net Response.Redirect with post data

    string url = String.Format("{0}://{1}/{2}", Request.Url.Scheme, Request.Url.Authority, &qu ...

  5. window下安装composer和laravel

    安装composer: 1.在https://getcomposer.org/download/ 中下载 Composer-Setup.exe 2.安装composer步骤如下: 至此,compose ...

  6. php语法检查方法——命令行模式和代码形式

    1. 命令行形式 php -l /path/to/file.php 2. php代码形式 function php_syntax_check($file){ $code = file_get_cont ...

  7. delphi中的临界区

    var fLock:TRTLCriticalSection; //定义临界区域 // 初始化 InitializeCriticalSection(fLock); //进入临界区 EnterCritic ...

  8. API删除文件

    using System; using System.Runtime.InteropServices; namespace ConsoleApplication1 { class Program { ...

  9. 在centos 6.4下安装opencv 2.3.1

    系统环境介绍: centos 6.4 1.安装依赖包 yum install cmake gcc gcc-c++ gtk+-devel gimp-devel gimp-devel-tools gimp ...

  10. Qt+MinGW+OpenCV开发环境在win7系统下的搭建(最新20140423)

    1 搭建环境 (1)联想Y470笔记本电脑,win7操作系统 (2)Qt 5.2.1 Open Source :(Qt Online installer for Window(9MB),即下载页面最上 ...