1. 无源汇可行流

1.1. 问题

传送门

1.2. 解决方案

\(u,v\) 的边为 \(e\)。我们可以把限制拆成两条:一条固定边边权为 \(\text{lower}(e)\), 另一条边边权为 \(\text{upper}(e)-\text{lower}(e)\)(我们称其为附加边),这一条直接在原图上连接即可。

怎么判定固定边有没有真的有 \(\text{lower}(e)\) 的流量呢?显然如果还是连接 \(u,v\) 是无法起到这个效果的。

容易发现固定边可以等价为:\(u\) 能流出 \(\text{lower}(e)\)保持流量守恒\(v\) 同理。所以 \(u,v\) 之间不必建固定边,只要达到这个效果就行了。

新建源点 \(S\),汇点 \(T\). 将固定边变成 \(S\)\(v\) 连边权为 \(\text{lower}(e)\) 的边,\(u\)\(T\) 连边权为 \(\text{lower}(e)\) 的边。对于某个点,如果固定边的流入总和为 \(\text{FlowIn}(i)\),流出为 \(\text{FlowOut}(i)\),我们将两者减去彼此的较小值,此时相当于消掉了一条边。

接着我们跑一遍 \(\text{dinic}\) 的最大流即可,如果满流就找到了一个可行流。可以发现我们将两个限制做了巧妙的转换:对于固定边,满流意味着满足固定边与流量平衡的限制;对于附加边,在起到平衡流量作用的同时也用容量满足附加边的限制。

1.3. 代码

#include <cstdio>

#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

#include <queue>
using namespace std;

const int maxn=205,maxm=10205,inf=0x3f3f3f3f;

queue <int> q;
int n,m,low[maxm],S=0,T,dep[maxn],arc[maxn],Flow[maxn],sum,ans[maxm];
int cnt=1,head[maxn],to[maxm*3],flow[maxm*3],nxt[maxm*3],id[maxm*3];

void addEdge(int u,int v,int w,int ID) {
    nxt[++cnt]=head[u],to[cnt]=v,flow[cnt]=w,id[cnt]=ID,head[u]=cnt;
    nxt[++cnt]=head[v],to[cnt]=u,flow[cnt]=0,id[cnt]=ID,head[v]=cnt;
}

bool bfs() {
    rep(i,0,n+1) dep[i]=inf;
    while(!q.empty()) q.pop();
    q.push(S),arc[S]=head[S],dep[S]=0;
    while(!q.empty()) {
        int u=q.front(); q.pop();
        erep(i,u)
            if(flow[i]>0 && dep[v]==inf) {
                dep[v]=dep[u]+1,arc[v]=head[v];
                q.push(v);
                if(v==T) return 1;
            }
    }
    return 0;
}

int dfs(int u,int CanFlow) {
    if(u==T) return CanFlow;
    int SumFlow=0,d;
    for(int i=arc[u];i;i=nxt[i]) {
        int v=to[i]; arc[u]=i;
        if(flow[i]>0 && dep[v]==dep[u]+1) {
            d=dfs(v,Min(CanFlow,flow[i]));
            if(!d) dep[v]=inf;
            SumFlow+=d,CanFlow-=d;
            flow[i]-=d,flow[i^1]+=d;
            if(!CanFlow) break;
        }
    }
    return SumFlow;
}

int Dinic() {
    int ret=0;
    while(bfs()) ret+=dfs(S,inf);
    return ret;
}

int main() {
    int u,v,x;
    n=read(9),m=read(9); T=n+1;
    rep(i,1,m) {
        u=read(9),v=read(9),low[i]=read(9),x=read(9);
        addEdge(u,v,x-low[i],i); Flow[u]-=low[i],Flow[v]+=low[i];
    }
    rep(i,1,n)
        if(Flow[i]>0) sum+=Flow[i],addEdge(S,i,Flow[i],0);
        else if(Flow[i]<0) addEdge(i,T,-Flow[i],0);
    if(sum==Dinic()) {
        puts("YES");
        rep(i,1,n) erep(j,i) {
            if(!(j&1) || id[j]==0) continue;
            ans[id[j]]=low[id[j]]+flow[j];
        }
        rep(i,1,m) print(ans[i],'\n');
    }
    else puts("NO");
    return 0;
}

2. 有源汇可行流

2.1. 问题

就是在前问题基础上多了源点 \(s\) 和汇点 \(t\).

2.2. 解决方案

在这张图上,显然源汇不需要保证流量平衡,但需要保证 \(s\) 的流出量等于 \(t\) 的流入量。为了套上上文那个模型,可以从 \(t\)\(s\) 连一条容量为 \(+\infty\) 的边,此时我们就保证了 \(t\) 的流出量等于 \(s\) 的流入量,从而保证了 \(s\) 的流出量等于 \(t\) 的流入量!这样模型成功完成了转换,再跑一遍最大流,那么满流等价于所有除了 \(S,T\) 的点流量平衡。


另外,再讲一个我犯过的错误思路:在建图时,扔掉 \(S,T\),将有关 \(S,T\) 的边都接到相应的 \(s,t\) 上。这个思路在 \(\text{POJ - 2396}\) 中是能够通过的,但是在模板题中可以被一个小数据轻松 \(\text{hack}\) 掉:

4 4 1 4
1 2 2 4
1 3 1 3
3 2 1 5
2 4 0 5

这是因为在 \(\text{POJ - 2396}\) 中源汇点 \(s,t\) 本来就是虚构的,它们之间 没有附加边,所以这样建图实际上和正确的建图方式是等价的。而在模板题中,\(s,t\) 之间存在附加边,所以满流不再与满足固定边与流量平衡的限制等价,所以无论求可行流还是下文要讲的最大流都是错误的。

3. 有源汇最大流

3.1. 问题

传送门

3.2. 解决方案

2.2. 解决方案 的基础上再跑一遍从 \(s\)\(t\) 的最大流(注意,这里 无需重新建图),输出这个最大流。

看上去非常不合理,但这竟然是对的,为什么捏?首先,\(\text{FlowOut}(t)=0,\text{FlowIn}(t)>0\),那么按 \(\text{lower}(e)\) 建图时,连向 \(t\) 的边(其实就是 \(S\rightarrow t\) 的那一条)就没有抵消的作用,相当于 \(t\) 至少接收的流量。此时 \(t\) 的入边只剩下附加边,跑可行流时相当于调整流量平衡需要接收的流量。

综上可以得出,在跑完可行流后,\(t\) 流入的流量就是可行流,而 \(t\rightarrow s\) 这条边的反边此时就代表这个数值!现在跑一遍从 \(s\)\(t\) 的最大流,沿着反边可以统计可行流,而且此时关于 \(S,T\) 的边都已经流满了,所以相当于在残余网络上跑最大流,由网络流基本定理,这两项相加就是我们要求的最大流。

3.3. 代码

#include <cstdio>

#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

#include <queue>
using namespace std;

const int maxn=205,maxm=10205,inf=0x3f3f3f3f;

queue <int> q;
int n,m,low[maxm],S=0,T,dep[maxn],arc[maxn],Flow[maxn],sum,ans[maxm];
int cnt=1,head[maxn],to[maxm*3],flow[maxm*3],nxt[maxm*3];

void addEdge(int u,int v,int w) {
    nxt[++cnt]=head[u],to[cnt]=v,flow[cnt]=w,head[u]=cnt;
    nxt[++cnt]=head[v],to[cnt]=u,flow[cnt]=0,head[v]=cnt;
}

bool bfs(int s,int t) {
    rep(i,0,n+1) dep[i]=inf;
    while(!q.empty()) q.pop();
    q.push(s),arc[s]=head[s],dep[s]=0;
    while(!q.empty()) {
        int u=q.front(); q.pop();
        erep(i,u)
            if(flow[i]>0 && dep[v]==inf) {
                dep[v]=dep[u]+1,arc[v]=head[v];
                q.push(v);
                if(v==t) return 1;
            }
    }
    return 0;
}

int dfs(int u,int t,int CanFlow) {
    if(u==t) return CanFlow;
    int SumFlow=0,d;
    for(int i=arc[u];i;i=nxt[i]) {
        int v=to[i]; arc[u]=i;
        if(flow[i]>0 && dep[v]==dep[u]+1) {
            d=dfs(v,t,Min(CanFlow,flow[i]));
            if(!d) dep[v]=inf;
            SumFlow+=d,CanFlow-=d;
            flow[i]-=d,flow[i^1]+=d;
            if(!CanFlow) break;
        }
    }
    return SumFlow;
}

int Dinic(int s,int t) {
    int ret=0;
    while(bfs(s,t)) ret+=dfs(s,t,inf);
    return ret;
}

int main() {
    int u,v,x,s,t;
    n=read(9),m=read(9),s=read(9),t=read(9); T=n+1;
    rep(i,1,m) {
        u=read(9),v=read(9),low[i]=read(9),x=read(9);
        addEdge(u,v,x-low[i]); Flow[u]-=low[i],Flow[v]+=low[i];
    }
    rep(i,1,n)
        if(Flow[i]>0) sum+=Flow[i],addEdge(S,i,Flow[i]);
        else if(Flow[i]<0) addEdge(i,T,-Flow[i]);
    addEdge(t,s,inf);
    if(sum==Dinic(S,T)) print(Dinic(s,t),'\n');
    else puts("please go home to sleep");
    return 0;
}

4. 有源汇最小流

4.1. 问题

传送门

4.2. 解决方案

根据上文的铺垫,在 2.2. 解决方案 的基础上,我们得到了从 \(s\)\(t\) 的可行流,先将它记录为 \(P\). 再删除从 \(t\)\(s\) 的双向边,再跑一遍从 \(t\)\(s\) 的最大流。相当于在可行流上进行退流操作,显然要退最多的。

4.3. 代码

#include <cstdio>

#define rep(i,_l,_r) for(register signed i=(_l),_end=(_r);i<=_end;++i)
#define fep(i,_l,_r) for(register signed i=(_l),_end=(_r);i>=_end;--i)
#define erep(i,u) for(signed i=head[u],v=to[i];i;i=nxt[i],v=to[i])
#define efep(i,u) for(signed i=Head[u],v=to[i];i;i=nxt[i],v=to[i])
#define print(x,y) write(x),putchar(y)

template <class T> inline T read(const T sample) {
    T x=0; int f=1; char s;
    while((s=getchar())>'9'||s<'0') if(s=='-') f=-1;
    while(s>='0'&&s<='9') x=(x<<1)+(x<<3)+(s^48),s=getchar();
    return x*f;
}
template <class T> inline void write(const T x) {
    if(x<0) return (void) (putchar('-'),write(-x));
    if(x>9) write(x/10);
    putchar(x%10^48);
}
template <class T> inline T Max(const T x,const T y) {if(x>y) return x; return y;}
template <class T> inline T Min(const T x,const T y) {if(x<y) return x; return y;}
template <class T> inline T fab(const T x) {return x>0?x:-x;}
template <class T> inline T gcd(const T x,const T y) {return y?gcd(y,x%y):x;}
template <class T> inline T lcm(const T x,const T y) {return x/gcd(x,y)*y;}
template <class T> inline T Swap(T &x,T &y) {x^=y^=x^=y;}

#include <queue>
using namespace std;
typedef long long ll;

const int maxn=5e4+5,maxm=125005,inf=0x3f3f3f3f;
const ll infty=1e15;

queue <int> q;
ll Flow[maxn],flow[maxm*3],sum;
int n,m,low[maxm],S=0,T,dep[maxn],arc[maxn];
int cnt=1,head[maxn],to[maxm*3],nxt[maxm*3];

void addEdge(int u,int v,ll w) {
    nxt[++cnt]=head[u],to[cnt]=v,flow[cnt]=w,head[u]=cnt;
    nxt[++cnt]=head[v],to[cnt]=u,flow[cnt]=0,head[v]=cnt;
}

bool bfs(int s,int t) {
    rep(i,0,n+1) dep[i]=inf;
    while(!q.empty()) q.pop();
    q.push(s),arc[s]=head[s],dep[s]=0;
    while(!q.empty()) {
        int u=q.front(); q.pop();
        erep(i,u)
            if(flow[i]>0 && dep[v]==inf) {
                dep[v]=dep[u]+1,arc[v]=head[v];
                q.push(v);
                if(v==t) return 1;
            }
    }
    return 0;
}

ll dfs(int u,int t,ll CanFlow) {
    if(u==t) return CanFlow;
    ll SumFlow=0,d;
    for(int i=arc[u];i;i=nxt[i]) {
        int v=to[i]; arc[u]=i;
        if(flow[i]>0 && dep[v]==dep[u]+1) {
            d=dfs(v,t,Min(CanFlow,flow[i]));
            if(!d) dep[v]=inf;
            SumFlow+=d,CanFlow-=d;
            flow[i]-=d,flow[i^1]+=d;
            if(!CanFlow) break;
        }
    }
    return SumFlow;
}

ll Dinic(int s,int t) {
    ll ret=0;
    while(bfs(s,t)) ret+=dfs(s,t,infty);
    return ret;
}

int main() {
    int u,v,x,s,t; ll tmp;
    n=read(9),m=read(9),s=read(9),t=read(9); T=n+1;
    rep(i,1,m) {
        u=read(9),v=read(9),low[i]=read(9),x=read(9);
        addEdge(u,v,x-low[i]); Flow[u]-=low[i],Flow[v]+=low[i];
    }
    rep(i,1,n)
        if(Flow[i]>0) sum+=Flow[i],addEdge(S,i,Flow[i]);
        else if(Flow[i]<0) addEdge(i,T,-Flow[i]);
    addEdge(t,s,infty);
    if(sum==Dinic(S,T)) {
        tmp=flow[cnt];
        flow[cnt]=flow[cnt-1]=0;
        print(tmp-Dinic(t,s),'\n');
    }
    else puts("please go home to sleep");
    return 0;
}

4.4. 一个奇怪的点

\(\text{LOJ}\) 上有一个 \(\text{Discussion}\),它先开始没有引起我的注意……直到我交了 \(n\) 发另一道板子题,\(\text{wa}\) 得人神共愤 qwq.

在原图上 \(t\) 就能到达 \(s\) 时会出现问题:这是因为我们的本意是进行退流,然而这条边其实开始时根本没有流量!或者说,即使有流量也可以看作是 \(t\rightarrow s\) 的那条容量为 \(\rm infty\) 的边的贡献。当然按 "流可以为负" 来理解就是正确的😃。

可是,大多数题目要求流量不为负,怎么办呢?你可以再新建一对源汇 \(ss,tt\),从 \(ss\)\(s\)\(t\)\(tt\) 连一条容量为 \(\text{infty}\) 的边!好处就是从 \(tt\rightarrow ss\) 的必经之路 —— \(tt\rightarrow t\)(一条反边)一定只执行退流操作,不存在 \(\rm infty\) 边的干扰了!\(\color{LimeGreen}{\text{Solved}}\)

或者,你可以将最后的部分换一下:

int Flow = dinic();
addEdge(t,s,inf);
Flow += dinic();
if(sm==Flow) print(e[cnt].w,'\n');

我也不知道为什么是对的,也懒得想了,拜拜

5. 一些题目

例 1. \(\text{[ZJOI 2011] }\)营救皮卡丘

首先可以将题意转化成:将 \(n\) 个点划分成 \(k\) 个可空集合,每个集合 \(S_i\) 包含第 \(i\) 个人摧毁的据点,要求所有集合的交集为空,并集为全集。显然,对于第 \(i\) 个人,从 \(u\)\(v\) 的花费是 \(\text{dis}(u,v)\),其中 \(\text{dis}(u,v)\) 是从 \(u\) 开始,不经过 \(>\max\{u,v\}\) 的点的最短路。

这是因为我们总能找到一个移动人物的顺序,使得其满足 "先摧毁前 \(1\)\(k-1\) 个据点,再摧毁第 \(k\) 个据点" 的限制。

类似路径覆盖的问题,可以考虑用网络流解决。将每个人行走的路径看作一条流,那么网络流图上的边实际上就是两点之间的路径。

接着考虑需要满足什么限制:流量小于等于 \(k\);每个点都至少被经过一次。可以发现,"交集为空" 的限制实际上没有用。

第一个限制可以建源点,连 \(ss\rightarrow 1\)(点从 \(1\) 开始编号,令 \(n=N+1\))的容量为 \(k\) 的边;对于第二条限制,可以利用经典的 "拆点为边" 思想,拆成出/入点,再限制拆出的边流量至少为 \(1\)。对于据点之间,就用出点连入点即可。最后,将出点连向 \(tt\),表示路径可以在任意节点终止;将 \(1\) 连向入点,费用为 \(\text{dis}(1,i)\).

我们得到了朴素网络流图,现在需要满足 "流量至少为 \(1\)" 的限制,也即再转化成上下界网络流。可以发现此模型是 2. 有源汇可行流,而非最大流,所以需要建立 循环流,即从 \(tt\)\(ss\) 连容量为 \(\rm infty\) 的边。然后新建 \(S,T\),朴素上下界连边即可。

另外,对于这类最少代价路径覆盖还有另一种解决方案 —— 二分图匹配。以下先以 \(\text{[SDOI 2010] }\)星际竞速 为例。

还是建出源汇,将每个点拆分成出点 \(i\)(左部点)与入点 \(i'\)(右部点),\(S\) 向左部连容量为 \(1\) 的边,\(T\) 向右部连容量为 \(1\) 的边。对于其它边,类似证明 "最小路径覆盖 \(=\) 点数 \(-\) 最大匹配数" 这个结论,我们假设初始状态有 \(n\) 条路径,那么总代价是 \(\sum a_i\). 一个匹配对应减少一条路径,假设 \(i\)\(j'\) 匹配,就意味着 \(i\rightarrow j\) 变成某条路径上 连续 的两点,此时减去 \(a_j\)(不能减去 \(a_i\) 的原因请自行思考),所以匹配贡献是 \(w(i,j)-a_j\),并令其容量为 \(1\). 用这张图跑 \(\mathtt{FF}\) 算法,选择最短路小于零的流加入 \(\rm ans\),如果最短路大于等于零可以直接终止。

不难发现每个点至多只会前后各匹配一次,所以正确性保证。

回到这道题,难搞的其实是 \(k\) 个人的限制,我们发现这其实就是强制要求进行 \(N-k\) 次匹配,并且忽略点 \(1\),于是这道题就做完了。代码简单而且思路非常好理解,比上下界快到不知道哪里去了 /kk.

posted on 2021-02-06 16:18  Oxide  阅读(65)  评论(0编辑  收藏  举报