【图论-网络流】最大流

· · 算法·理论

【0】学网络流需要了解的一些东西

咕咕咕

【1】几种计算最大流的算法

【1.0】FF 方法

咕咕咕

【1.1】EK 算法

【1.1.1】EK 算法大致思路

可以概括为 bfs 找增广路。

咕咕咕

EK 的复杂度是 O(能过)(划掉),最坏复杂度是 O(n m ^ 2),但一般情况下远远达不到这个上界。

【1.1.2】EK 算法的实现

虽然我平时是用的邻接表存图,但我实现的时候用的是邻接矩阵。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 209,INF = 1 << 30;
int n,m,s,t;
int cap[N][N];
int pre[N];
int flow[N];
int bfs(int s,int t){
    memset(pre,-1,sizeof pre);
    pre[s] = 0;
    flow[s] = INF;
    queue<int> q;
    q.push(s);
    while(!q.empty()){
        int u = q.front();
        q.pop();
        if(u == t)
            break;
        for(int v = 1;v <= n;v++){
            if(u != v && cap[u][v] > 0 && pre[v] == -1){
                q.push(v);
                flow[v] = min(flow[u],cap[u][v]);
                pre[v] = u;
            }
        }
    }
    if(pre[t] == -1)
        return 0;
    return flow[t];
}
void update(int f){
    int cur = t;
    while(cur != s){
        cap[pre[cur]][cur] -= f;
        cap[cur][pre[cur]] += f;
        cur = pre[cur];
    }
}
int ans;
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m >> s >> t;
    for(int i = 1;i <= m;i++){
        int u,v,c;
        cin >> u >> v >> c;
        cap[u][v] += c;//可能有重边 
    }
    while(1){
        int f = bfs(s,t);
        if(!f)
            break;
        ans += f;
        update(f);
    }
    cout << ans;
    return 0;
}

【1.2】Dinic 算法

【1.2.1】Dinic 算法的思路

上文提到的 EK 算法还有优化的空间,因为它每次遍历了整张图但只找到了一条增广路,Dinic 算法通过每次在残量网络上构造分层图配合 dfs 回溯一次找多条增广路。

【1.2.2】Dinic 算法的实现

这次换成了邻接表了。

#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 209,M = 5002,INF = 0x3f3f3f3f3f3f3f3f;//5002好闪! 
int n,m,s,t;
int ans;
struct edge{
    int to,cap,nex;
} e[M << 1];
int head[N],ecnt = 1;
int head2[N];//分层图的表头 
void addedge(int u,int v,int c){
    ecnt++;
    e[ecnt] = (edge){v,c,head[u]};
    head[u] = ecnt;
}
queue<int> q;
int dep[N];//层次 
bool bfs(){//构造分层图 
    memset(dep,0,sizeof dep);
    q.push(s);
    dep[s] = 1;
    head2[s] = head[s];
    while(!q.empty()){
        int u = q.front();
        q.pop();
        for(int i = head[u];i;i = e[i].nex){
            int v = e[i].to,c = e[i].cap;
            if(dep[v] || !c)
                continue;
            head2[v] = head[v];
            dep[v] = dep[u] + 1;
            q.push(v);
        }
    }
    return dep[t];
}
int dfs(int u,int flow){//在分层图上增广 
    if(u == t)
        return flow;
    int rest = flow,tmp;
    for(int i = head2[u];i && rest;i = e[i].nex){
        head2[u] = i;
        int v = e[i].to,c = e[i].cap;
        if(dep[v] != dep[u] + 1 || !c)
            continue;
        tmp = dfs(v,min(rest,c));
        if(!tmp)
            dep[v] = 0;//当无法从v 增广时,将v从分层图中删除
        e[i].cap -= tmp;
        e[i ^ 1].cap += tmp;
        rest -= tmp;
    }
    return flow - rest;//流到u的流量减去流出u后剩余的流量就是流到汇点的流量
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0);cout.tie(0);
    cin >> n >> m >> s >> t;
    for(int i = 1;i <= m;i++){
        int u,v,c;
        cin >> u >> v >> c;
        addedge(u,v,c);
        addedge(v,u,0);
    }
    int flow = 0;
    while(bfs())
        while(flow = dfs(s,INF))
            ans += flow;
    cout << ans;
    return 0;
}

【2】最大流建模相关