网络流模板 最大流maxflow
2016-09-30 08:37
381 查看
#include <cstdio> const int inf=2147483647; int n,m,idx=1; int to[20201],next[20201],flow[20201],head[102], dep[102],q[102]; void add(int x,int y,int z) { to[++idx]=y; flow[idx]=z; next[idx]=head[x]; head[x]=idx; to[++idx]=x; flow[idx]=0; next[idx]=head[y]; head[y]=idx; return ; } bool bfs(int s,int t,int L,int R) { for(int i=L;i<=R;++i) dep[i]=-1; int l=0,r=0; dep[q[r++]=s]=1; while(l<r) { int x=q[l++]; for(int i=head[x];i;i=next[i]) if(flow[i]&&dep[to[i]]==-1) dep[q[r++]=to[i]]=dep[x]+1; } return dep[t]!=-1; } int dinic(int x,int t,int maxflow) { if(x==t) return maxflow; int last=maxflow; for(int i=head[x];i;i=next[i]) if(flow[i]&&dep[to[i]]==dep[x]+1) { int tof=dinic(to[i],t,flow[i]<last?flow[i]:last); if(tof) { flow[i]-=tof; flow[i^1]+=tof; if(!(last-=tof)) return maxflow; } } dep[x]=-1; return maxflow-last; } int Maxflow(int s,int t,int l,int r) { int ans=0; while(bfs(s,t,l,r)) ans+=dinic(s,t,inf); return ans; }
相关文章推荐
- 网络流--最大流ek模板
- ACM网络流模板 最大流ISAP URAL1774
- 网络流模板:最大流ISAP算法和Dinic算法
- 网络流(最大流+模板)
- Dining POJ - 3281 EK网络流模板/最大流/拆点
- 网络流-最大流 模板(poj 1273)
- POJ1459-Power Network-网络流-最大流(EK模板题)
- 计蒜之道 初赛 第一场 题解 dp 高效 网络流 最小割 最大流 ISAP 模板
- 【网络流】最大流:Drainage Ditches【EK模板】
- 网络流模板(最大流,最小费)
- hdu4280 Island Transport (网络流最大流)&(dinic算法模板)
- 网络流模板:最大流ISAP算法和Dinic算法
- 【最大流-ISAP】POJ-1273-网络流模板!!
- [Luogu] P3376 模板-网络流-最大流
- 【网络流】最大流:PIGS【EK模板、建图题】
- 最大网络流模板Dinic算法
- (模板)网络流最大流,dinic算法
- poj 1459 Power Network(网络流 最大流 dinic模板题)
- 网络流入门 最大流,带下界,最小费用,EK算法,Dinic算法 模板
- 网络流最大流EdmondKarp、SAP【模板】