[学习笔记] 上下界网络流

1. 无源汇可行流

1.1. 问题

传送门

1.2. 解决方案

(u,v) 的边为 (e)。我们可以把限制拆成两条:一条 固定 边边权为 ( ext{lower}(e)), 另一条边边权为 ( ext{upper}(e)- ext{lower}(e))

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

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

新建源点 (S),汇点 (T)。将固定边变成 (S)(v) 连边权为 ( ext{lower}(e)) 的边,(u)(T) 连边权为 ( ext{lower}(e)) 的边。

接着我们跑一遍 (mathtt{Dinic}) 的最大流即可,如果满流就找到了一个可行流。

可以想一想满流的意义即总流量(最后 (T) 收到的流量)等于 (S) 流出的流量(固定边的流量和)。

因为 (S,T) 都只连接了固定边拆开的边,所以这时这些边一定都跑满了,也就达到了我们的要求(那些 ( ext{upper}(e)- ext{lower}(e)) 的边可以理解为平衡流量的)。

还有一个困扰了我较久的对源点理解的点:(mathtt{Dinic})(x) 开始跑就赋予了 (x) 无尽的流量,其他的点的流量都从 (x) 来,所以如果一个不为 (x) 的点没有入度肯定是没有流量的。

另外还有一个建边的优化:对于某个点,如果固定边的流入总和为 ( ext{FlowIn}(i)),流出为 ( ext{FlowOut}(i)),我们将两者减去彼此的较小值,此时相当于消掉了一条边。

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],'
');
    }
    else puts("NO");
    return 0;
}

2. 有源汇可行流

2.1. 问题

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

2.2. 解决方案

由于 (s,t) 不需要 满足流量守恒,而且 (s) 肯定需要流量(因为 (s) 就是上文入度为 (0) 而且不是 (S) 的点),从 (t)(s) 连一条边权为 ( ext{inf}) 的边。

因为 (s) 只有这一条入边,(t) 只有这一条出边,所以如果流出 (s) 的与流入 (t) 的相等就能保证流量守恒。

3. 有源汇最大流

3.1. 问题

传送门

3.2. 解决方案

2.2. 解决方案 的基础上再跑一遍从 (s)(t)(mathtt{Dinic}) 的最大流。

因为如果满足可行流,关于 (S,T) 的边显然都不能再增广了。在这个基础上给予 (s) 无限流量就可以得到最大流。

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),'
');
    else puts("please go home to sleep");
    return 0;
}

4. 有源汇最小流

4.1. 问题

传送门

4.2. 解决方案

2.2. 解决方案 的基础上再跑一遍从 (t)(s)(mathtt{Dinic}) 的最大流(注意在判断之后要删掉 ((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),'
');
    }
    else puts("please go home to sleep");
    return 0;
}
原文地址:https://www.cnblogs.com/AWhiteWall/p/14381853.html