网络流最大流ISAP算法(模板)
最大流算法的ISAP算法,Maxflow返回最大流的值
#include<bits/stdc++.h>
using namespace std;
const int inf=2e9;
const int maxn=10050;
struct Edge{
int from,to,cap,flow;
Edge(int u,int v,int c,int f):from(u),to(v),cap(c),flow(f){}
};
struct ISAP{
int n,m,s,t;//结点数,边数(包括反向弧),源点编号,汇点编号
vector<Edge>edges;
vector<int>g[maxn];
bool vis[maxn];
int d[maxn],cur[maxn];
int p[maxn],num[maxn];
void init(int n){
this->n=n;
for(int i=0;i<=n;++i) g[i].clear();
edges.clear();
}
void add(int from,int to,int cap){
edges.push_back(Edge(from,to,cap,0));
edges.push_back(Edge(to,from,0,0));
m=edges.size();
g[from].push_back(m-2);
g[to].push_back(m-1);
}
bool RevBFS(){
memset(vis,0,sizeof(vis));
queue<int>Q;
Q.push(t);
d[t]=0;
vis[t]=1;
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=0;i<g[x].size();i++){
Edge &e =edges[g[x][i]^1];
if(!vis[e.from]&&e.cap>e.flow){
vis[e.from]=1;
d[e.from]=d[x]+1;
Q.push(e.from);
}
}
}
return vis[s];
}
int Augment(){
int x=t, a=inf;
while(x!=s){
Edge &e = edges[p[x]];
a= min(a,e.cap-e.flow);
x=edges[p[x]].from;
}
x=t;
while(x!=s){
edges[p[x]].flow+=a;
edges[p[x]^1].flow-=a;
x=edges[p[x]].from;
}
return a;
}
int Maxflow(int s,int t){
this->s=s,this->t=t;
int flow=0;
RevBFS();
memset(num,0,sizeof(num));
for(int i=0;i<n;i++){
num[d[i]]++;
}
int x=s;
memset(cur,0,sizeof(cur));
while(d[s]<n){
if(x==t){
flow+=Augment();
x=s;
}
int ok=0;
for(int i=cur[x];i<g[x].size();i++){
Edge &e =edges[g[x][i]];
if(e.cap>e.flow&&d[x]==d[e.to]+1){
ok=1;
p[e.to]=g[x][i];
cur[x]=i;
x=e.to;
break;
}
}
if(!ok){
int m=n-1;
for(int i=0;i<g[x].size();i++){
Edge &e =edges[g[x][i]];
if(e.cap>e.flow)
m=min(m,d[e.to]);
}
if(--num[d[x]]==0)
break;
num[d[x]=m+1]++;
cur[x]=0;
if(x!=s)
x=edges[p[x]].from;
}
}
return flow;
}
};