「学习笔记」最大流I

· · 算法·理论

:::warning[Tip]{open}

\small{请复制\ Markdown\ 到洛谷以获取最佳阅读体验}

:::

:::info[]{open}

\small{网络流最迷人的地方在于那让人拍案叫绝的建模思想和以不变应万变的扩展性} \qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad\qquad——By\ strcmp.

:::

\LARGE\text{Part-1\ 目录} \qquad\small\text{Part4.0\ 关于}\ ISAP \qquad\small\text{Part4.1\ 算法步骤} \qquad\small\text{Part4.2}\ bfs \qquad\small\text{Part4.3}\ dfs \qquad\small\text{Part4.4}\ ISAP\cdot main \qquad\small\text{Part4.5}\ Code \qquad\small\text{Part}\infty\small\text{.1\ 参考文献} \qquad\small\text{Part}\infty\small\text{.2\ update}

\small\color{White}{Part\ 1\ Ford-Fullerson}

\LARGE\text{Part1}\cdot Ford-Fullerson

:::info[增广路定理]{open}

\small当网络中存在可行流且无可增广路径时,该流即为最大流

:::

从汇点 t 向源点 s 标号,即为每个点设置一个高度,服务于后续最大流的寻找。

  1. 初始所有边的流量为 0
  2. 找到一条增广路
  3. 更新残留网络
  4. 重复 2、3 操作,直到找不到增广路

此算法并不优秀,但其是后续算法实现的基础。

\small\color{White}{Part\ 2\ Edmond−Karp}

\LARGE\text{Part2}\cdot Edmond−Karp

本质上是使用 bfs 优化了 FF 算法,使得每次寻找皆为最短增广路。

反向边无愧为网络流最具代表性的思想,巧妙利用构建反向边,与正向边的消耗抵消,使得较晚流可以让较早流改向,在残量网络中寻找增广路时将流量返还正向边。

让我们看一个例子:(V:4\to5 打错了,权值为 55)

如图,有一条 s\to3\to5\to t 的增广路,发现 5\to3 有 1 条流量为 10 的反向边,此时可以将此边改流,使得 3\to5 回归 11,而 5\to4 与 4\to s 变为 45。

\large\text{Part2.2}\ Code

:::info[完整代码]{open}

```cpp //EK算法求解最大流 #include<iostream> #include<cstring> #include<cstdio> #include<algorithm> #include<queue> using namespace std; const int inf=1<<30; int n,m,s,t; struct Node{ int v; int val; int next; }node[201010]; int top=1,head[101010];//top必须从一个奇数开始,一般用-1但我不习惯,解释见下方 inline void addedge(int u,int v,int val){ node[++top].v=v; node[top].val=val; node[top].next=head[u]; head[u]=top; } inline int Read(){ int x=0; char c=getchar(); while(c>'9'||c<'0')c=getchar(); while(c>='0'&&c<='9')x=x*10+c-'0',c=getchar(); return x; } int inque[101010];//点是访问过里 struct Pre{ int v;//该点的前一个点(从起点过来) int edge;//与该点相连的边(靠近起点的) }pre[101010]; inline bool bfs(){ queue<int>q; memset(inque,0,sizeof(inque)); memset(pre,-1,sizeof(pre)); inque[s]=1; q.push(s); while(!q.empty()){ int u=q.front(); q.pop(); for(int i=head[u];i;i=node[i].next){ int d=node[i].v; if(!inque[d]&&node[i].val){//node[i].val==0则已经该路径满了 pre[d].v=u; pre[d].edge=i; if(d==t)return 1; inque[d]=1; q.push(d); } } } return 0; }//是否有增广路 int EK(){ int ans=0; while(bfs()){ int mi=inf; for(int i=t;i!=s;i=pre[i].v){ mi=min(mi,node[pre[i].edge].val);//每次只能增加增广路上最小的边的权值 } for(int i=t;i!=s;i=pre[i].v){ node[pre[i].edge].val-=mi; node[pre[i].edge^1].val+=mi; //反向的边的编号是正向边的编号^1 //这就是为什么top开始时必须是奇数 //-------------------------->(并非必须,仅用作方便) } ans+=mi; } return ans; } int main(){ register int i; n=Read(),m=Read(),s=Read(),t=Read(); int u,v,w; for(i=1;i<=m;i++) u=Read(),v=Read(),w=Read(),addedge(u,v,w),addedge(v,u,0); printf("%d",EK()); return 0; } ``` ::: --- ### $\small\color{White}{Part\ 3\ Dinic} \LARGE\text{Part3}\cdot Dinic

:::success[Dinic的复杂度分析]{open}

  1. Dinic$ 使用 $bfs$ ~~暴力~~维护层次图,单次复杂度 $O(V)
  2. Dinic$ 使用 $dfs$ 多路增广求取最大流,复杂度为 $O(VE)
  3. 综上,总复杂度为 O(V^2E)

以上是 Dinic 算法理论复杂度上限,特别的,当 U\in (V,E) 为二分图时,其复杂度降为 O(\sqrt VE),所以 Dinic 跑二分图匹配优于匈牙利算法。

:::

\small\color{White}{Part\ 4\ ISAP}

\LARGE\text{Part4}\cdot ISAP

ISAP 的正确性:

:::success[ISAP算法的正确性证明]{open}

对于图 G \in (V,E),源点 s 的深度会在每次 dfs 后提高,且每次 dfs 找的都是最短增广路,定义函数 dep(U),U \in V 为结点 U 的深度,若未断层,最多会跑 V−dep(s) 次增广路,可以证明,当 dep(s) \ge V 时必定出现断层,也就不存在增广路径,因此 ISAP 算法找出的一定是最大流。

:::

时间复杂度分析:

:::success[ISAP算法的复杂度分析]{open}

在图 G \in (V,E) 中,bfs 是 \Theta(V+E) 的,几乎不影响总时间复杂度。显然,每次 dfs 后,源点 s 到汇点 t 的距离都会增加,最多进行 V−dep(s) 次 bfs,dep(s) 比 V 的阶小,因此共进行 O(V) 次 DFS,每次构造出一个新的层次图,图上最多有 O(E) 个增广路,寻找每个增广路的时间最多是 O(V) 的,所以 ISAP 算法的时间复杂度上限为 O(V^2E)。

:::

$2.$ 沿**深度连续**且**边权非零**的结点进行增广,同时**更新路径上的结点深度** $3.$ 若**深度断层**或**源点高于总高**则结束,否则转至 $2. 记结点 $i$ 的深度为 $dep_i$,记深度 $dep$ 下的结点数为 $gap_{dep}

以下是 bfs 的步骤:

1.$ 初始化节点深度 $dep_i=-1,i\in E $3.$ 取出队首 $u$,仅沿**反向边**跳边,更新 $dep_{to_i}=-1$ 的结点深度为 $dep_u+1$,并将**更新点**入队 $4.$ 若队列为空则结束,否则转至 $3.

:::info[Code \cdot bfs]{open}

void bfs(){
    memset(dep,-1,V*sizeof(int));
    dep[t]=0,++gap[dep[t]];
    queue<int> qwq;
    qwq.push(t);
    while(!qwq.empty()){
        int u=qwq.front();qwq.pop();
        for(int i=head[u];i!=-1;i=nxt[i]){
            if(dep[to[i]]==-1&&i%2){
                dep[to[i]]=dep[u]+1;
                ++gap[dep[to[i]]];
                qwq.push(to[i]);
            }
        }
    }
}

:::

其有如下原则: $1.$ 从源点 $s$ 开始 $2.$ 仅沿**深度连续**的增广路增广,并且仅通过边权**非零**的边 $3.$ 当 $used=flow$ 时终止(记 $used$ 为示当前点增广流量,记 $flow$ 为前一结点流入的总流量) $4.$ 当增广后 $used$ 小于 $flow$ 时将结点 $u$ 的深度增加。 其中 $1.2.3.$ 显而易见,让我们来看看 $4.$。 --- $ISAP$ 的 $dfs$ 有一显著特点,即在寻找增广路的**同时维护层次图**。 这里就引出了 $Dinic$ 与 $ISAP$ 的不同。 $Dinic$ 是一个与 $ISAP$ 极其相似的算法,其不同之处在于,$Dinic$ 在 $dfs$ 之后**直接暴力 $bfs$ 维护层次图**,而 $ISAP$ 则是在 $dfs$ 的**同时维护层次图**,这也是 $ISAP$ 通常比 $Dinic$ 快数倍的原因。 看看 $ISAP$ 如何维护层次图: - 若 $used< flow$,$dep_u+1

简洁而精妙,将 ISAP 的思想体现得淋漓尽致。

为什么是对的?

假设有一个结点 u \in V 是当前 dfs 考虑的结点,且 dep_t \equiv 0,每当遇到了汇点 t 就 return,而 dep_s 是每轮必变的,因为源点 s 的 flow 是 \infty。

这就可以推导出一个很重要的结论:“当前 DFS 找到的增广路径长度相等且都等于 dep(s)”。

这说明,若有一个结点 u \in V 增广出去的流量小于增广路径上最小边权的容量,就意味着经过 u 的所有长度等于 dep(s) 的增广路都已被增广,进一步也可证明,所有长度小于 dep(s) 的增广路都已被增广。

这时候将结点 u 的深度提高,相当于通过结点 u 的增广路径长度变长了,也就能增广其他比原本的增广路更长的增广路了。

这又引出了一个优化:断层优化即 gap 优化。

:::info[gap优化]{open}

if(!gap[dep[u]])dep[s]=n+1;

:::

显然,一个结点被多次遍历时,第一次就已经足以找完增广路,那就没必要多搜,可以将 $head$ 复制给 $cur$,并实时更新 $cur$ 为当前边。 :::info[当前弧优化]{open} ```cpp for(int i=cur[u];i!=-1;i=nxt[i]){ cur[u]=i; ... } ``` ::: --- :::info[$Code \cdot dfs$]{open} ```cpp int dfs(int u,int flow){ if(u==t||flow==0)return flow;int used=0,aug=0; for(int i=cur[u];i!=-1;i=nxt[i]){ cur[u]=i; if(dep[to[i]]==dep[u]-1&&val[i]){ aug=dfs(to[i],min(flow-used,val[i])); if(aug){ val[i]-=aug; val[i^1]+=aug; used+=aug; } } if(used==flow)return used; } if(used<flow){ --gap[dep[u]]; if(!gap[dep[u]])dep[s]=n+1; ++gap[++dep[u]]; } return used; } ``` ::: --- - $\large\text{Part4.4}\ $ $ISAP \cdot main

这是 ISAP 的核心代码之一。

:::info[ISAP主函数]{open}

int ISAP(){
    bfs();int ans=0;
    while(dep[s]<n){
        memcpy(cur,head,(n+1)*sizeof(int));
        ans+=dfs(s,inf);
    }
    return ans;
}

:::

注意结束条件: dep[s]<n

即增广路最长只有 n,当 dep[s] \ge n 时增广路必定已找完。

:::info[完整代码]{close}

#pragma gcc optimize(2)
#include <iostream>
#include <algorithm>
#include <queue>
#include <set>
#include <cstring>
#define int long long
#define V 50010
#define E 1000010
#define inf 9223372036854775807
using namespace std;
inline int read(){
    int x=0,f; char ch=0;
    while(!isdigit(ch)) f=(ch=='-'?-1:1),ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    return x*f;
}
inline void print(int x){
    if(x<0) x=-x,putchar('-');
    if(x>9) print(x/10);
    putchar(x%10+48);
}
inline void write(int x){print(x);putchar('\n');}
int n,m,s,t;
int head[V],to[E],nxt[E],val[E],cnt=-1;
int dep[V],gap[V],cur[V];
void add(int u,int v,int w){
    to[++cnt]=v;
    val[cnt]=w;
    nxt[cnt]=head[u];
    head[u]=cnt;
}
void bfs(){
    memset(dep,-1,V*sizeof(int));
    dep[t]=0,++gap[dep[t]];
    queue<int> qwq;
    qwq.push(t);
    while(!qwq.empty()){
        int u=qwq.front();qwq.pop();
        for(int i=head[u];i!=-1;i=nxt[i]){
            if(dep[to[i]]==-1&&i%2){
                dep[to[i]]=dep[u]+1;
                ++gap[dep[to[i]]];
                qwq.push(to[i]);
            }
        }
    }
}
int dfs(int u,int flow){
    if(u==t||flow==0)return flow;int used=0,aug=0;
    for(int i=cur[u];i!=-1;i=nxt[i]){
        cur[u]=i;
        if(dep[to[i]]==dep[u]-1&&val[i]){
            aug=dfs(to[i],min(flow-used,val[i]));
            if(aug){
                val[i]-=aug;
                val[i^1]+=aug;
                used+=aug;
            }
        }
        if(used==flow)return used;
    }
    if(used<flow){
        --gap[dep[u]];
        if(!gap[dep[u]])dep[s]=n+1;
        ++gap[++dep[u]];
    }
    return used;
}
int ISAP(){
    bfs();
    int ans=0;
    while(dep[s]<n){
        memcpy(cur,head,(n+1)*sizeof(int));
        ans+=dfs(s,inf);
    }
    return ans;
}
signed main(){
    memset(head,-1,V*sizeof(int));
    n=read(),m=read(),s=read(),t=read();
    for(register int i=1;i<=m;++i){
        int a=read(),b=read(),c=read();
        add(a,b,c);
        add(b,a,0);
    }
    write(ISAP());
    return 0;
}

:::

\small\color{White}{\text{Part}\ \infty\text{\ 末页}}

\LARGE\text{Part}\infty\large\text{.1\ 参考文献} \LARGE\text{Part}\infty\large\text{.2\ update} - $2025.7.9\ 3\ p.m. \quad$ 更新了 $ISAP$,并增加了 $\text{updata}