最大流EK算法/DINIC算法学习
之前一直觉得很难,没学过网络流,毕竟是基础知识现在重新来看。
定义一下网络流问题,就是在一幅有向图中,每条边有两个属性,一个是cap表示容量,一个是flow
表示流过的流量。我们要求解的问题就是从S点到T点最多可以跑多少流量。用比较学术的话说,就是 一个有向图 G=(V,E);有两个特别的点:源点s、汇点t;图中每条边(u,v)∈E,有一个非负值的容量C(u,v),流量F(u,v)。
定义一下"残流网络":即当前边还可以流过的流量,也就是cap-flow。
其中,在最大流的问题中,我们要满足三个条件:
1:容量限制:f(u,v)<c(u,v),即每条边流过的流量不能超过容量上限。
2:斜对称性:(f(u,v)=-f(v,u))在后面的运用中,我们叫做一旦有物品从u运到v,那么则肯定有一个可退流从v运到u(这里不懂得稍后解释正确性)。
3:流量平衡:简单来说就是除了源点和汇点,没有其他点可以保存货物。显然f(s,u)==f(v,t)。也就是说非ST节点不累计流量,流进的等于流出的流量。
最大流问题,就是使得f(s,u)=f(v,t)达到最大。
现在讨论如何解决这个问题,有一个很容易想到的贪心思路是:我们暴力的从S点到T点找可行路,一旦找到一条路,总流量就加上这条路径上的MIN{cap-flow} ,因为最小的限制最大的。
比如下图: <c,f>表示当前弧的容量及流过的流量。

如果我们要求A->F的最大流的话,先找到第一条可行路(A->B->C->F),这条路上的最大允许通过的流量是5-2=3,执行之后,A->B这条路上就满流了,也就是不能从A->B在通过流量了。第二次找的时候显然只能找到(A->D->E->F)这条路,最大通过流量是5-3=2 ,第三次就找不到路了。答案就是2+3=5;
这样看来这种方法貌似可行,可惜的是有一点没考虑到,如果我们第一次找到的路影响到后面的流的行走导致最优解发生变化怎么办。程序没有反悔的机会,比如下图:
http://acm.hdu.edu.cn/showproblem.php?pid=1532
Drainage Ditches
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 20435 Accepted Submission(s): 9804
time it rains on Farmer John's fields, a pond forms over Bessie's
favorite clover patch. This means that the clover is covered by water
for awhile and takes quite a long time to regrow. Thus, Farmer John has
built a set of drainage ditches so that Bessie's clover patch is never
covered in water. Instead, the water is drained to a nearby stream.
Being an ace engineer, Farmer John has also installed regulators at the
beginning of each ditch, so he can control at what rate water flows into
that ditch.
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 includes several cases. For each case, the first line contains
two space-separated integers, N (0 <= N <= 200) and M (2 <= M
<= 200). N is the number of ditches that Farmer John has dug. M is
the number of intersections points 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.
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int cap[][];
int a[],p[];
int n,m;
int bfs(int s,int t)
{
queue<int>q;
memset(p,-,sizeof(p));
q.push(s);
a[s]=inf;
p[s]=;
while(!q.empty()){
int u=q.front();q.pop();
if(u==t)break;
for(int i=;i<=n;++i)
{
if(i!=s&&cap[u][i]>&&p[i]==-){
p[i]=u;
a[i]=min(cap[u][i],a[u]);
q.push(i);
}
}
}
if(p[t]==-)return -;
return a[t];
}
int maxflow(int s,int t)
{
int ret=,delta=;
while((delta=bfs(s,t))!=-){
int k=t;
while(k!=s){
int las=p[k];
cap[las][k]-=delta;
cap[k][las]+=delta;
k=p[k];
}
ret+=delta;
}
return ret;
}
int main()
{
int i,j,k;
int u,v,w;
while(cin>>m>>n){
memset(cap,,sizeof(cap));
memset(a,,sizeof(a));
for(i=;i<=m;++i){
cin>>u>>v>>w;
if(u==v)continue;
cap[u][v]+=w;
}
cout<<maxflow(,n)<<endl;
}
return ;
}
贴上一发DINIC算法的,数据不大所以看不出来二者的时间差异。
DINIC算法先跑bfs对点进行分层,然后逐层跑阻塞流来增广。
当前边优化,用一个cur[u]记录u点当前遍历到那一条边了,下一次再访问到u的时候直接从上一次记录的
边开始遍历,减少很多遍历的时间。因为已经访问过的边肯定已经满了(最大化)所以不必要再访问了。
多路增广,当前弧优化,炸点。
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
const int maxn=;
struct Edge
{
int v,cap,flow,next;
}e[maxn<<];
int first[maxn],d[maxn],cur[maxn],tot,N,M;
bool vis[maxn];
void add(int u,int v,int cap,int flow){
e[tot]=Edge{v,cap,flow,first[u]};
first[u]=tot++;
}
bool bfs(){
memset(vis,,sizeof(vis));
memset(d,,sizeof(d));
queue<int>q;
q.push();
d[]=;
vis[]=;
while(!q.empty()){
int u=q.front();
q.pop();
for(int i=first[u];~i;i=e[i].next){
if(!vis[e[i].v]&&e[i].cap>e[i].flow){
vis[e[i].v]=;
d[e[i].v]=d[u]+;
q.push(e[i].v);
}
}
}
return vis[N]; }
int dfs(int u,int a){
if(u==N||a==) return a;
int flow=,f;
for(int &i=cur[u];~i;i=e[i].next){
if(d[e[i].v]==d[u]+ && (f=dfs(e[i].v,min(a,e[i].cap-e[i].flow)))>){
e[i].flow+=f;
e[i^].flow-=f;
flow+=f;
a-=f;
if(!a) break;
}
}
return flow;
}
void solve(){
int ans=;
while(bfs()){
for(int i=;i<=N;++i) cur[i]=first[i];
ans+=dfs(,inf);
}
printf("%d\n",ans);
}
int main(){
while(scanf("%d%d",&M,&N)!=EOF){
tot=;
memset(first,-,sizeof(first));
int u,v,w;
while(M--){
scanf("%d%d%d",&u,&v,&w);
add(u,v,w,);
add(v,u,,);
}
solve();
}
return ;
}
/*
5 4
1 2 40
1 4 20
2 4 20
2 3 30
3 4 10
*/
最大流EK算法/DINIC算法学习的更多相关文章
- 最大流EK和Dinic算法
最大流EK和Dinic算法 EK算法 最朴素的求最大流的算法. 做法:不停的寻找增广路,直到找不到为止 代码如下: @Frosero #include <cstdio> #include ...
- POJ1273:Drainage Ditches(最大流入门 EK,dinic算法)
http://poj.org/problem?id=1273 Description Every time it rains on Farmer John's fields, a pond forms ...
- 网络最大流算法—Dinic算法及优化
前置知识 网络最大流入门 前言 Dinic在信息学奥赛中是一种最常用的求网络最大流的算法. 它凭借着思路直观,代码难度小,性能优越等优势,深受广大oier青睐 思想 $Dinic$算法属于增广路算法. ...
- Dinic算法学习
转自 此文虽为转载,但博主的网络流就是从这开始的,认为写的不错 网络流基本概念 什么是网络流 在一个有向图上选择一个源点,一个汇点,每一条边上都有一个流量上限(以下称为容量),即经过这条边的流量不能超 ...
- 网络流入门—用于最大流的Dinic算法
"网络流博大精深"-sideman语 一个基本的网络流问题 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3 ...
- 网络流(最大流-Dinic算法)
摘自https://www.cnblogs.com/SYCstudio/p/7260613.html 网络流定义 在图论中,网络流(Network flow)是指在一个每条边都有容量(Capacity ...
- Dinic算法(研究总结,网络流)
Dinic算法(研究总结,网络流) 网络流是信息学竞赛中的常见类型,笔者刚学习了最大流Dinic算法,简单记录一下 网络流基本概念 什么是网络流 在一个有向图上选择一个源点,一个汇点,每一条边上都有一 ...
- 网络流入门--最大流算法Dicnic 算法
感谢WHD的大力支持 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2,3,4},有向管道{A,B,C,D,E},即有向图一张. ...
- dinic 算法 基本思想及其模板
“网络流博大精深”—sideman语 一个基本的网络流问题 感谢WHD的大力支持 最早知道网络流的内容便是最大流问题,最大流问题很好理解: 解释一定要通俗! 如右图所示,有一个管道系统,节点{1,2, ...
随机推荐
- Ubuntu Linux下通过代理(proxy)使用git上github.com
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/loveaborn/article/details/24575659 github.com.作为程序猿 ...
- I2C通信
项目之前研究了I2C通信协议的实现,完成FPGA对视频解码芯片SAA7111A的初始化配置,设计实现了I2C主机对从机(SAA7111A)32个寄存器的写操作,因此只简单实现了I2C的写时序. 这次重 ...
- win7开启特定端口
win7开启特定端口 在xp系统的时代,修改防火墙很方便,很简单.windows7或许是做得过于复杂了.当然所谓安全性也是相当于其他之前版本的系统更高了.为什么要打开端口,肯定是在win ...
- 主机名 域名 网站名 URL
举几个域名的例子:google.com,baidu.com,163.com可以明确的告诉你,加上www,就不再是域名了! 以http://mail.163.com/index.html为例进行说明:1 ...
- go——方法
方法是与对象实例绑定的特殊函数.方法是面向对象编程的基本概念,用于维护和展示对象的自身状态.对象是内敛的,每个实例都有各自不同的独立特征,以属性和方法来暴露对外通信接口.普通函数则专注于算法流程,通过 ...
- React:快速上手(4)——掌握Redux(1)
React:快速上手(4)——掌握Redux 引入Redux 混乱的state管理 随着 JavaScript 单页应用开发日趋复杂,JavaScript 需要管理比任何时候都要多的 state (状 ...
- php debug函数
$debug=$_GET['debug'];//是说获取url中debug变量$debug=empty($debug)?'':$debug;//如果变量不为空,赋值为$debug,为空的话赋值 ''$ ...
- Spring框架学习之IOC(一)
Spring框架学习之IOC(一) 先前粗浅地学过Spring框架,但当时忙于考试及后期实习未将其记录,于是趁着最近还有几天的空闲时间,将其稍微整理一下,以备后期查看. Spring相关知识 spri ...
- java synchronized和(ReentrantLock)区别
原文:http://blog.csdn.net/zheng548/article/details/54426947 区别一:API层面 syschronized使用 synchronized即可修饰方 ...
- hadoop13---centos安装jdk
由于各Linux开发厂商的不同,因此不同开发厂商的Linux版本操作细节也不一样,今天就来说一下CentOS下JDK的安装: 方法一:手动解压JDK的压缩包,然后设置环境变量 1.在/usr/目录下创 ...