这 篇 b l o g 是 为 了 记 录 下 网 络 流 的 模 板 这篇blog是为了记录下网络流的模板 这篇blog是为了记录下网络流的模板
最 大 流 ( M a x f l o w ) 最大流(Maxflow) 最大流(Maxflow)
A l g o r i t h m : D i n i c Algorithm:Dinic Algorithm:Dinic
这个比较常用了 P r o b l e m Problem Problem l i n k link link
T i p s : Tips: Tips:
- 注意邻接表的 t o t tot tot从 1 1 1开始
- 反边边权为 0 0 0
- 可用当前弧优化 就是用一个 c u r cur cur数组覆盖 h e a d [ ] head[] head[]
可以看出 在同为手敲栈的前提下 有无弧优化是有一定差别的
C O D E : CODE: CODE:
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<bitset>
using namespace std;
typedef long long ll;
const int N=1e4+5,M=2e6+5;
int n,m,S,T;
ll ans,Head[N],tot=1,cur[N];
struct node{
ll to,next,val;
}a[M];
int dep[N],q[N],head,tail;
void add(int x,int y,int k)
{
a[++tot]=(node){y,Head[x],k};
Head[x]=tot;
}
bool bfs()
{
memset(dep,0,sizeof dep);
q[1]=S;head=tail=1;
dep[S]=1;
while(head<=tail)
{
int x=q[head++];
for(int i=Head[x];i;i=a[i].next)
{
int qwq=a[i].to;
if(a[i].val&&!dep[qwq])
{
dep[qwq]=dep[x]+1;
q[++tail]=qwq;
}
}
}
return dep[T];
}
ll Dinic(int x,ll k)
{
if(x==T) return k;
ll link=0;
for(int i=cur[x];i&&k;i=a[i].next)
{
cur[x]=i; //弧优化
int qwq=a[i].to;
if(a[i].val&&dep[qwq]==dep[x]+1)
{
ll res=Dinic(qwq,min(a[i].val,k));
a[i].val-=res;
a[i^1].val+=res;
k-=res; link+=res;
if(!k) break;
}
}
if(link==0) dep[x]=0;
return link;
}
int main()
{
scanf("%d%d%d%d",&n,&m,&S,&T);
while(m--)
{
int x,y; ll w;
scanf("%d%d%lld",&x,&y,&w);
add(x,y,w);
add(y,x,0);
}
while(bfs()){
memcpy(cur,Head,sizeof Head); //cur记录head
ans+=Dinic(S,0x3f3f3f3f3f3f3f3f);
}
printf("%lld",ans);
return 0;
}
最 小 费 用 最 大 流 ( M i n C o s t M a x F l o w ) 最小费用最大流(MinCost~MaxFlow) 最小费用最大流(MinCost MaxFlow)
A l g o r i t h m : S p f a Algorithm:Spfa Algorithm:Spfa
是的 它没有死 并且是常用算法 P r o b l e m l i n k Problem~link Problem link
T i p s : Tips: Tips:
- 邻接表 t o t tot tot从 1 1 1开始
- 反边容量为 0 0 0 边权为 − k -k −k
- 初始化 f l o w [ ] flow[] flow[] 和 d i s [ ] dis[] dis[]为 i n f inf inf
- 记录 t o to to点在 h e a d [ ] head[] head[]里的位置 以及 t o to to的父亲
- 每次将 v i s [ x ] vis[x] vis[x]清为 0 0 0
C O D E : CODE: CODE:
#include<cmath>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<iostream>
#include<queue>
using namespace std;
typedef long long ll;
const int N=5005;
int n,m,S,T,maxflow,mincost,dep[N],head[N],tot=1;
int vis[N],dis[N],flow[N],fa[N],p[N];
struct node{
int to,next,lim,k;
}a[N*20];
void add(int x,int y,int limit,int k)
{
a[++tot]=(node){y,head[x],limit,k};
head[x]=tot;
a[++tot]=(node){x,head[y],0,-k};
head[y]=tot;
}
bool spfa()
{
memset(dis,0x7f,sizeof dis);
memset(vis,0,sizeof vis);
memset(flow,0x7f,sizeof flow);
queue<int> q;
q.push(S);
vis[S]=1; dis[S]=0;
while(!q.empty())
{
int x=q.front();
q.pop();
for(int i=head[x];i;i=a[i].next)
{
int qwq=a[i].to;
if(a[i].lim&&dis[qwq]>dis[x]+a[i].k)
{
dis[qwq]=dis[x]+a[i].k;
p[qwq]=i;
fa[qwq]=x;
flow[qwq]=min(flow[x],a[i].lim);
if(!vis[qwq])
{
vis[qwq]=1;
q.push(qwq);
}
}
}
vis[x]=0;
}
return flow[S]!=flow[T];
}
void MCMF()
{
while(spfa())
{
maxflow+=flow[T];
mincost+=flow[T]*dis[T];
for(int i=T;i^S;i=fa[i])
{
int qwq=p[i];
a[qwq].lim-=flow[T];
a[qwq^1].lim+=flow[T];
}
}
}
int main()
{
scanf("%d%d%d%d",&n,&m,&S,&T);
while(m--)
{
int x,y,limit,k;
scanf("%d%d%d%d",&x,&y,&limit,&k);
add(x,y,limit,k);
}
MCMF();
printf("%d %d",maxflow,mincost);
return 0;
}
至于 H L P P ( HLPP( HLPP(预流推进 ) ) ) 以及负圈费用流 后面会慢慢 u p d a t e update update
( ( (毕竟这几天改题重要 且例题卡时限 q w q ) qwq) qwq)