增广路的核心就是引入了反向边,使在进行道路探索选择的时候增加了类似于退路的东西【有一点dp的味道??】

具体操作就是:1、首先使用结构体以及数组链表next[ MAXN ]进行边信息的存储

2、【核心】在存储正向边时,将正向边结构体数组相邻编号的元素存储反向边

  【具体操作:如正向边为AA[0],则对应的反向边为AA[1],这样就可以在之后通过异或来对正向边对应的反向边进行更新】【初始时反向边的容量为0】

3、通过dfs进行寻路,寻路过程要用数组记录下来路经过的边的编号,便于在找到终点后遍历找边的最小剩余容量,然后在总流量上加上最小的剩余容量即可

4、【易错】dfs应该写成bool 型的,在找到一条路或找不到路之后直接return【而不是一直dfs到最后】,而且要在主函数中加一个for(;;),在bool dfs 返回值为false时跳出循环即可

 #include<cstdio>
#include<cstring>
#include<algorithm>
#define N 10000+10
#define M 10000+10
#define inf 1e9;
using namespace std;
int head[N],arnum=,used[N],ans,way[M];
struct ss{int next,to,cap;}a[M];
void add(int from,int to,int cap){a[++arnum]=(ss){head[from],to,cap};head[from]=arnum;}
void insert(int u,int v,int cap){add(u,v,cap),add(v,u,);}
int n,m,S,T;
void work(int step)
{
int minn=inf;
for(int i=;i<=step;i++)
minn=min(minn,a[way[i]].cap);
ans+=minn;
for(int i=;i<=step;i++)
{
a[way[i]].cap-=minn;
a[way[i]^].cap+=minn;
}
}
int dfs(int u,int step)
{
for(int i=head[u];i;i=a[i].next)
{
int v=a[i].to;
int cap=a[i].cap;
if(not used[v] and cap>)
{
way[step]=i;
used[v]=;
if(v==T){work(step);return ;}
else if(dfs(v,step+))return ;
}
return ;
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&S,&T);
int u,v,c;
for(int i=;i<=m;i++){scanf("%d%d%d",&u,&v,&c);insert(u,v,c);}
for(;;)
{
memset(used,,sizeof(used));
used[S]=;
if(not dfs(S,))break;
}
printf("%d",ans);
return ;
}

网络流初步:<最大流>——核心(增广路算法)(模板)的更多相关文章

  1. Power Network (最大流增广路算法模板题)

    Time Limit: 2000MS   Memory Limit: 32768K Total Submissions: 20754   Accepted: 10872 Description A p ...

  2. HDU3549 Flow Problem(网络流增广路算法)

    题目链接. 分析: 网络流增广路算法模板题.http://www.cnblogs.com/tanhehe/p/3234248.html AC代码: #include <iostream> ...

  3. hdu 3549 Flow Problem【最大流增广路入门模板题】

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 Flow Problem Time Limit: 5000/5000 MS (Java/Others ...

  4. hdu 3549 Flow Problem(增广路算法)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=3549 模板题,白书上的代码... #include <iostream> #include & ...

  5. 网络流初步——增广路算法(EK)模板

    #include <iostream> #include <queue> #include<string.h> using namespace std; #defi ...

  6. 网络最大流最短增广路Dinic算法模板

    #include<cstdio> #include<cstring> #include<string> #include<cmath> #include ...

  7. 落谷p3376 最大流EdmondsKarp增广路模板

    参考: https://blog.csdn.net/txl199106/article/details/64441994 分析: 该算法是用bfs求出是否有路从s到t, 然后建立反向边(关于反向边), ...

  8. 网络流——增广路算法(dinic)模板 [BeiJing2006]狼抓兔子

    #include<iostream> #include<cstring> #include<algorithm> #include<cmath> #in ...

  9. POJ-1273-Drainage Ditches 朴素增广路

    Drainage Ditches Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 70588   Accepted: 2743 ...

随机推荐

  1. eclipse安装adt插件后工具栏不显示android相关图标

    到eclipse官网下载luna(开发android,推荐此版本,当前最新版4.4.2)版本的eclipse http://ftp.jaist.ac.jp/pub/eclipse/technology ...

  2. yii新手在实例化models(controller调用models实化化)php warning错误

    新手在执照yii教程来的时候,config/main.php文件是全新写的,post提交的时候,会出错 include(LoginForm.php) [<a href='function.inc ...

  3. Confluence 6 权限设置

    备注:当 '外部用户管理' 权限没有被选择的时候,你仅可以指派 LDAP 用户到本地用户组中. 只读(Read Only) 从你目录服务器上获得 LDAP 用户,用户组只能通过你的目录服务器进行修改. ...

  4. 51 jquery 节点操作和 bootstrapt

    jquery 和 bootstrapt1.jquery each 函数 1.each 循环方式一: 可循环对象: var arr =["alex","deng" ...

  5. 『Collections』namedtuple_具名元组

    namedtuple()类 需要两个参数,参数一为nametupe名称,参数二为字段一般为序列(多个字段) Python中存储系列数据,比较常见的数据类型有list,除此之外,还有tuple数据类型. ...

  6. php数组转化为字符串

    1.函数explode();  这个是字符串转化为数组 , implode() ;这个是数组转化为字符串. $array=explode(separator,$string); $string=imp ...

  7. unity3d 博客

    博客: 1.http://my.csdn.net/caoboya 2.http://my.csdn.net/OnafioO

  8. 84. Largest Rectangle in Histogram *HARD* -- 柱状图求最大面积 85. Maximal Rectangle *HARD* -- 求01矩阵中的最大矩形

    1. Given n non-negative integers representing the histogram's bar height where the width of each bar ...

  9. OC Foundation框架—字符串

    一.Foundation框架中一些常用的类 字符串型: NSString:不可变字符串 NSMutableString:可变字符串 集合型: 1) NSArray:OC不可变数组 NSMutableA ...

  10. zabbix LLD 自定义脚本

    一 前言 二 懒人必备zabbix监控之 LLD (low level discovery) 本次的教程是我想监控kafka的消费情况,举个栗子 [root@VM_0_98_centos bin]# ...