网络流的具体解释请百度,这里是本题的粗略理解。
从增广路定理讲起。
已知每条边的残量(残余流量)等于其容量减去实际流量。而最大流是从s点(源点)到 t点(汇点)总流量最大的一种方案。由此我们可以认定当每条从s到 t 的路径流量都为最大时,当前流即为最大流。
在此我们引进一个概念——增广路,残余网络中的任何一条从s到 t的有向道路都对应一条原图中的增广路(augmenting path)。这时我们需要找到整条路径中残量的最小值d并使每条边的流量都加上d,对应的操作称为增广。不难看出如果增广前的流量满足3个条件(容量限制、斜对称性、流量平衡),那么增广后仍然满足。显然的是只要残余网络中存在增广路,那么流量就可以增大。可以证明其反命题也成立:如果残余网络中不存在增广路,则当前流就是最大流。(增广路定理)
寻找增广路时可以使用DFS,但我们采取稍微好一些的BFS来防止被卡,这也就是Edmonds-Karp算法。这里我们用结构体来实现。
下方代码;
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10010;
const int INF=1<<30;
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 EdmondsKarp{
int n,m,s,t;
vector<Edge> edges;
vector<int >G[maxn];
int a[maxn];
int p[maxn];
void init()
{
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void AddEdge(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);//挂上反向边
}
int Maxflow(){
int flow=0;
while(1){//循环到不存在增广路
memset(a,0,sizeof(a));
queue<int>Q;
Q.push(s);//加入源点
a[s]=INF;//源点的残余流量无限大
while(!Q.empty()){//用FIFO队列来维护BFS
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(!a[e.to]&&e.cap>e.flow){
p[e.to]= G[x][i];//记录边的序号,方便更新流量
a[e.to]= min(a[x],e.cap-e.flow);//更新残余流量的最小值d
Q.push(e.to);
}
}
if(a[t]) break;//存在增广路就停止寻找
}
if(!a[t]) break;//不存在增广路就弹出
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];//更新流量
edges[p[u]^1].flow-=a[t];//正向弧流量增大,反向弧流量减小[满足最大流中的f(u,v)==-f(v,u)]
}
flow+=a[t];
}
return flow;
}
}g;
int main()
{
int m;
cin>>g.n>>m>>g.s>>g.t;
g.init();
for(int i=1;i<=m;i++)
{
int x,y,c;
scanf("%d%d%d",&x,&y,&c);
g.AddEdge(x,y,c);
}
cout<<g.Maxflow() ;
return 0;
}
同时我们可以在BFS寻找增广路的同时,求出它的层次图。借助DFS过程,我们可以构建一张增广路网来减少BFS寻找增广路的次数。
下方代码:
#include<queue>
#include<cstdio>
#include<vector>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=10010;
const int INF=1<<29;
struct Edge{
int from,to,cap,flow;
Edge(int a,int b,int c,int w):from(a),to(b),cap(c),flow(w){}
};
struct Dinic{
int n,m,s,t;//节点数,边数(包括反向弧),源点编号和汇点编号
vector<Edge> edges;//边表。edges[e]和edges[e^1]互为反向弧
vector<int> G[maxn];//邻接表,G[i][j]表示节点i的第j条边在e数组中的序号
int d[maxn];//从源点到i的距离(第几层),同时可以辅助判断该点是否被遍历过
int cur[maxn];//当前弧的下标
void init(int n)
{
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void Add_edge(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);
return ;
}
bool BFS(){
memset(d,-1,sizeof(d));
queue<int>Q;
Q.push(s);
d[s]=0;
while(!Q.empty()){
int x=Q.front();Q.pop();
for(int i=0;i<G[x].size();i++){
Edge& e=edges[G[x][i]];
if(d[e.to]==-1&&e.cap>e.flow){//只考虑残量网络中的弧
d[e.to]=d[x]+1;
Q.push(e.to);
}
}
}
return d[t]!=-1;
}
int DFS(int x,int a){
if(x==t||a==0) return a;
int flow=0,f;
for(int& i=cur[x];i<G[x].size();i++){//从上次考虑的弧
Edge& e=edges[G[x][i]];
if(d[x]+1==d[e.to]&&(f=DFS(e.to,min(a,e.cap-e.flow)))>0){
e.flow+=f;
edges[G[x][i]^1].flow-=f;
flow+=f;
a-=f;//从s到x的路径中最小残量
if(a==0) break;//如前一部分(残量最值为0)不存在增广路,则向下DFS也一定不会找到增广路
}
}
return flow;
}
//核心是不停地用BFS构造分层网络,然后用DFS沿阻塞流增广
int Maxflow(int s,int t){
this->s=s,this->t=t;
int flow=0;
while(BFS()){
memset(cur,0,sizeof(cur));
flow+=DFS(s,INF);
}
return flow;
}
}g;
int main(){
int n,m,s,t;
cin>>n>>m>>s>>t;
g.init(n);
for(int i=1;i<=m;i++){
int x,y,c;
cin>>x>>y>>c;
g.Add_edge(x,y,c);
}
cout<<g.Maxflow(s,t);
return 0;
}