最大流:EK,FF,Dinic,ISAP。因为实现方法几乎差不多,记住最优的即可。
FF:dfs寻找增广路,思想是给程序一个反悔的机会,每次找到一条增广路(正向减,反向加给程序反悔的机会)。
EK:bfs寻找增广路,思想和FF一样。
Dinic:在增广路的基础上加上层次图。
ISAP:概括地说,ISAP 算法就是不停地找最短增广路,找到之后增广;如果遇到死路就 retreat,直到发现s, t不连通,算法结束。找最短路本质上就是无权最短路径问题,因此采用 BFS 的思想。具体来说,使用一个数组d,记录每个节点到汇点t的最短距离。搜索的时候,只沿着满足d[u]=d[v]+1的边u→v(这样的边称为允许弧)走。显然,这样走出来的一定是最短路
kuangbin的网络流板子
sap邻接矩阵
int maze[maxn][maxn];
int gap[maxn],dis[maxn],pre[maxn],cur[maxn];
int flow[maxn][maxn];
int sap(int start,int end,int nodenum)
{
mem(cur,0);
mem(dis,0);
mem(gap,0);
mem(flow,0);
int u=pre[start]=start,maxflow=0,aug=-1;
gap[0]=nodenum;
while(dis[start]<nodenum)
{
loop:
for(int v=cur[u]; v<nodenum; v++)
if(maze[u][v]-flow[u][v]&&dis[u]==dis[v]+1)
{
if(aug==-1||aug>maze[u][v]-flow[u][v])
aug=maze[u][v]-flow[u][v];
pre[v]=u;
u=cur[u]=v;
if(v==end)
{
maxflow+=aug;
for(u=pre[u]; v!=start; v=u,u=pre[u])
{
flow[u][v]+=aug;
flow[v][u]-=aug;
}
aug=-1;
}
goto loop;
}
int mindis=nodenum-1;
for(int v=0; v<nodenum; v++)
{
if(maze[u][v]-flow[u][v]&&mindis>dis[v])
{
cur[u]=v;
mindis=dis[v];
}
}
if((--gap[dis[u]])==0) break;
gap[dis[u]=mindis+1]++;
u=pre[u];
}
return maxflow;
}
ISAP邻接表形式
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],pre[MAXN],cur[MAXN];
void init()
{
tol = 0;
memset(head,-1,sizeof(head));
}
//加边,单向图三个参数,双向图四个参数
void addedge(int u,int v,int w,int rw=0)
{
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].next = head[u];
edge[tol].flow = 0;
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].next = head[v];
edge[tol].flow = 0;
head[v]=tol++;
}
//输入参数:起点、终点、点的总数
//点的编号没有影响,只要输入点的总数
int sap(int start,int end,int N)
{
memset(gap,0,sizeof(gap));
memset(dep,0,sizeof(dep));
memcpy(cur,head,sizeof(head));
int u = start;
pre[u] = -1;
gap[0] = N;
int ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
for(int i = pre[u]; i != -1; i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
}
u = start;
ans += Min;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
{
flag = true;
cur[u] = pre[v] = i;
break;
}
}
if(flag)
{
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min+1;
gap[dep[u]]++;
if(u != start) u = edge[pre[u]^1].to;
}
return ans;
}
ISAP+bfs初始化+栈优化
const int MAXN = 100010;//点数的最大值
const int MAXM = 400010;//边数的最大值
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow;
} edge[MAXM]; //注意是MAXM
int tol;
int head[MAXN];
int gap[MAXN],dep[MAXN],cur[MAXN];
void init()
{
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w,int rw = 0)
{
edge[tol].to = v;
edge[tol].cap = w;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = rw;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
int Q[MAXN];
void BFS(int start,int end)
{
memset(dep,-1,sizeof(dep));
memset(gap,0,sizeof(gap));
gap[0] = 1;
int front = 0, rear = 0;
dep[end] = 0;
Q[rear++] = end;
while(front != rear)
{
int u = Q[front++];
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(dep[v] != -1)continue;
Q[rear++] = v;
dep[v] = dep[u] + 1;
gap[dep[v]]++;
}
}
}
int S[MAXN];
int sap(int start,int end,int N)
{
BFS(start,end);
memcpy(cur,head,sizeof(head));
int top = 0;
int u = start;
int ans = 0;
while(dep[start] < N)
{
if(u == end)
{
int Min = INF;
int inser;
for(int i = 0; i < top; i++)
if(Min > edge[S[i]].cap - edge[S[i]].flow)
{
Min = edge[S[i]].cap - edge[S[i]].flow;
inser = i;
}
for(int i = 0; i < top; i++)
{
edge[S[i]].flow += Min;
edge[S[i]^1].flow -= Min;
}
ans += Min;
top = inser;
u = edge[S[top]^1].to;
continue;
}
bool flag = false;
int v;
for(int i = cur[u]; i != -1; i = edge[i].next)
{
v = edge[i].to;
if(edge[i].cap - edge[i].flow && dep[v]+1 == dep[u])
{
flag = true;
cur[u] = i;
break;
}
}
if(flag)
{
S[top++] = cur[u];
u = v;
continue;
}
int Min = N;
for(int i = head[u]; i != -1; i = edge[i].next)
if(edge[i].cap - edge[i].flow && dep[edge[i].to] < Min)
{
Min = dep[edge[i].to];
cur[u] = i;
}
gap[dep[u]]--;
if(!gap[dep[u]])return ans;
dep[u] = Min + 1;
gap[dep[u]]++;
if(u != start)u = edge[S[--top]^1].to;
}
return ans;
}
最小费用最大流
const int MAXN = 10000;
const int MAXM = 100000;
const int INF = 0x3f3f3f3f;
struct Edge
{
int to,next,cap,flow,cost;
} edge[MAXM];
int head[MAXN],tol;
int pre[MAXN],dis[MAXN];
bool vis[MAXN];
int N;//节点总个数,节点编号从0~N-1
void init(int n)
{
N = n;
tol = 0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int cap,int cost)
{
edge[tol].to = v;
edge[tol].cap = cap;
edge[tol].cost = cost;
edge[tol].flow = 0;
edge[tol].next = head[u];
head[u] = tol++;
edge[tol].to = u;
edge[tol].cap = 0;
edge[tol].cost = -cost;
edge[tol].flow = 0;
edge[tol].next = head[v];
head[v] = tol++;
}
bool spfa(int s,int t)
{
queue<int>q;
for(int i = 0; i < N; i++)
{
dis[i] = INF;
vis[i] = false;
pre[i] = -1;
}
dis[s] = 0;
vis[s] = true;
q.push(s);
while(!q.empty())
{
int u = q.front();
q.pop();
vis[u] = false;
for(int i = head[u]; i != -1; i = edge[i].next)
{
int v = edge[i].to;
if(edge[i].cap > edge[i].flow &&dis[v] > dis[u] + edge[i].cost )
{
dis[v] = dis[u] + edge[i].cost;
pre[v] = i;
if(!vis[v])
{
vis[v] = true;
q.push(v);
}
}
}
}
if(pre[t] == -1)return false;
else return true;
}
int minCostMaxflow(int s,int t,int &cost)
{
int flow = 0;
cost = 0;
while(spfa(s,t))
{
int Min = INF;
for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
{
if(Min > edge[i].cap - edge[i].flow)
Min = edge[i].cap - edge[i].flow;
}
for(int i = pre[t]; i != -1; i = pre[edge[i^1].to])
{
edge[i].flow += Min;
edge[i^1].flow -= Min;
cost += edge[i].cost * Min;
}
flow += Min;
}
return flow;
}
Dinic
#include<stdio.h>
#include<stack>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1000;
const int inf=0x3f3f3f3f;
#define LL long long
struct node2
{
int v,w,nex;
} e[maxn*100];
int first[maxn];
int tot;
int s,t;
void edge(int u,int v,int w)
{
e[tot].v=v;
e[tot].w=w;
e[tot].nex=first[u];
first[u]=tot++;
e[tot].v=u;
e[tot].w=0;
e[tot].nex=first[v];
first[v]=tot++;
}
int deep[maxn];
int vis[maxn];
int bfs()
{
memset(deep,0,sizeof(deep));
memset(vis,0,sizeof(vis));
vis[s]=1;
queue<int>q;
q.push(s);
while(!q.empty())
{
int p=q.front();
q.pop();
for(int i=first[p]; i!=-1; i=e[i].nex)
{
int x=e[i].v;
if(e[i].w>0&&!vis[x])
{
vis[x]=1;
deep[x]=deep[p]+1;
q.push(x);
}
}
}
return deep[t];
}
int dfs(int u,int k)
{
if(u==t) return k;
for(int i=first[u]; i!=-1; i=e[i].nex)
{
int x=e[i].v;
if(e[i].w>0&&deep[x]==deep[u]+1)
{
int ff=dfs(x,min(e[i].w,k));
if(ff>0)
{
e[i].w-=ff;
e[i^1].w+=ff;
return ff;
}
}
}
deep[u]=0;
return 0;
}
/*
听说这个更好,不怎么用,应该不会卡这个吧
int dfs(int s,int t,int min1)
{
if(s==t)
return min1;
int flow,ans=0;
for(int i=head[s];i!=-1;i=edge[i].next)
{
int v=edge[i].v;
if(dis[v]==dis[s]+1&&edge[i].w&&(flow=dfs(v,t,min(min1,edge[i].w))))
{
edge[i].w-=flow;
edge[i^1].w+=flow;
ans+=flow;
min1-=flow;
if(!min1) break;
}
}
if(ans) return ans;
dis[s]=-1;
return 0;
}
*/
void Dinic()
{
int sum=0;
while(bfs())
{
while(int f=dfs(s,inf))
{
sum+=f;
}
}
}
int main()
{
memset(first,-1,sizeof(first));
tot=0;
s=;
t=;
Dinic();
return 0;
}
ISAP,不怎么用,如果点非常多,那就用吧
#include<stdio.h>
#include<stack>
#include<queue>
#include<map>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
#define LL long long
#define mem(a,b) memset(a,b,sizeof(a))
const int inf=0x3f3f3f3f3f3f3f3f;
const int N=200000+10;
const int M=200000+10;
int top;
int tt;
int h[N],pre[N],g[N];//h[i]记录每个节点的高度,pre[i]记录前驱,g[i]表示距离为i个节点数有多少个
int first[N];
map<string,int>name;
string str1,str2;
int s,t;
int n;
struct node
{
int v,next;
int cap,flow;
} E[M*4];
void init()
{
mem(first,-1);
top=0;
tt=1;
}
void add_edge(int u,int v,int c)
{
E[top].v=v;
E[top].cap=c;
E[top].flow=0;
E[top].next=first[u];
first[u]=top++;
}
void add(int u,int v,int c)
{
add_edge(u,v,c);
add_edge(v,u,c);
}
void set_h(int t)//标高函数,从终点向起点标高
{
queue<int>q;
mem(h,-1);
mem(g,0);
h[t]=0;
q.push(t);
while(!q.empty())
{
int v=q.front();
q.pop();
g[h[v]]++;//当前高度的个数+1
for(int i=first[v]; ~i; i=E[i].next)
{
int u=E[i].v;
if(h[u]==-1)//当前节点未标高
{
h[u]=h[v]+1;
q.push(u);
}
}
}
}
void Isap()//isap算法
{
set_h(t);
int ans=0;
int u=s;
int d;
while(h[s]<n)//节点的高度小于顶点数
{
int i=first[u];
if(u==s) d=inf;
for(; ~i; i=E[i].next)
{
int v=E[i].v;
if(E[i].cap>E[i].flow&&h[u]==h[v]+1)//容量大于流量且当前的高度等于要去的高度+1
{
u=v;
pre[v]=i;
d=min(d,E[i].cap-E[i].flow);//找最小增量
if(u==t)//到达汇点
{
while(u!=s)
{
int j=pre[u];//找到u的前驱
E[j].flow+=d;//正向流量+d
E[j^1].flow-=d;//反向流量-d
u=E[j^1].v;//向前搜索
}
ans+=d;
d=inf;
}
break;
}
}
if(i==-1)//邻接边搜索完毕,无法行进
{
if(--g[h[u]]==0)//重要的优化,这个高度的节点只有一个,结束
break;
int hmin=n-1;//重贴标签的高度初始为最大
for(int j=first[u]; ~j; j=E[j].next)
{
if(E[j].cap>E[j].flow)
hmin=min(hmin,h[E[j].v]);//取所有邻接点高度的最小值
}
h[u]=hmin+1;//重新标高
g[h[u]]++;//标高后该高度的点数+1
if(u!=s)//不是源点时,向前退一步,重新搜
u=E[pre[u]^1].v;
}
}
printf("%d\n",ans);
}
int main()
{
init();
Isap();
return 0;
}
最小费用最大流
#include<stdio.h>
#include<stack>
#include<queue>
#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=200+5;
const int inf=0x3f3f3f3f;
#define mem(a,b) memset(a,b,sizeof(a))
#define LL long long
struct node
{
int v,w,c,nex;
} e[maxn*maxn];
int tot;
int first[maxn];
int n,m,s,t;
void init()
{
tot=0;
memset(first,-1,sizeof(first));
}
void edge(int u,int v,int w,int c)
{
e[tot].v=v;
e[tot].w=w;
e[tot].c=c;
e[tot].nex=first[u];
first[u]=tot++;
e[tot].v=u;
e[tot].w=0;
e[tot].c=-c;//为什么为负,
//有可能你走了重边,要减去这个多加的值
e[tot].nex=first[v];
first[v]=tot++;
}
int dis[maxn];
int vis[maxn];
int pre[maxn];
int res[maxn];
int spfa()
{
mem(pre,-1);
mem(res,-1);
mem(dis,inf);
mem(vis,0);
vis[s]=1;
dis[s]=0;
queue<int>q;
q.push(s);
while(!q.empty())
{
int p=q.front();
q.pop();
vis[p]=0;
for(int i=first[p]; i!=-1; i=e[i].nex)
{
int x=e[i].v;
if(e[i].w)
{
if(dis[x]>dis[p]+e[i].c)
{
dis[x]=dis[p]+e[i].c;
pre[x]=p;
res[x]=i;
if(!vis[x])
{
q.push(x);
}
}
}
}
}
if(dis[t]==inf) return 0;
return 1;
}
int mincostflow()
{
int sum=0,ans=0;
while(spfa())
{
int f=inf;
for(int i=t; i!=s; i=pre[i])
{
f=min(f,e[res[i]].w);
}
sum+=f;
ans+=f*dis[t];
for(int i=t; i!=s; i=pre[i])
{
e[res[i]].w-=f;
e[res[i]^1].w+=f;
}
}
return sum;
}
int main()
{
init();
s=;
t=;
printf("%d\n",ans);
return 0;
}
最小割等于最大流