Loj#116-[模板]有源汇有上下界最大流

正题

题目链接:https://loj.ac/p/116


题目大意

(n)个点(m)条边的一张图,每条边有流量上下限制,求源点到汇点的最大流。


解题思路

先别急着求上面那个,考虑一下怎么求无源点汇点的上下界可行流。

可以考虑先把下限流满,这样就会出现有的点流量不均衡的问题,考虑每个点除了下限以外还有附加流量,这些附加流量会最多占能每条边(r-l)这么多的流量,可以先建立一张每条流量都是(r-l)的图。

定义一个点的(d_i)为该点的入度减去出度(流入的流量减去流出的流量),然后对于一个点如果它的(d_i)大于(0),那么它需要向其他点补充流量,建立一个超级源点(S)向它连边,流量为(d_i)。同理如果一个点的(d_i)小于(0)就连向超级汇点(T)

这样就搞定了无源点汇点的上下界可行流问题了。

然后考虑有源汇点(s,t)怎么办,那么也就是(t)可以无限接受,(s)可以无限输送。那么如果(t)(s)连一条(inf)的边,那么就可以保证(s,t)的功能又能保证流量守恒了。
之后直接和无源点汇点的一样做就好了。

然后要求最大流,先跑一次有没有可行的再考虑流量能够浮动的范围,此时我们需要在刚刚的残量网络上找从(s)(t)的增广路来增大(s)(t)的流量,那么删掉刚刚(t->s)的边然后跑(s->t)的最大流就好了。

最小流的话就是从(t->s)跑最大流


code

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
const int N=210,inf=1e9;
struct node{
    int to,next,w;
}a[41000];
int n,m,tot,in[N],out[N],d[N];
int ls[N],cur[N],dep[N];
queue<int> q;
void addl(int x,int y,int w){
    a[++tot].to=y;a[tot].next=ls[x];ls[x]=tot;a[tot].w=w;
    a[++tot].to=x;a[tot].next=ls[y];ls[y]=tot;a[tot].w=0;
    return;
}
bool bfs(int s,int t){
    while(!q.empty())q.pop();q.push(s);
    memset(dep,0,sizeof(dep));dep[s]=1;
    for(int i=1;i<=t;i++)cur[i]=ls[i];
    while(!q.empty()){
        int x=q.front();q.pop();
        for(int i=ls[x];i;i=a[i].next){
            int y=a[i].to;
            if(dep[y]||!a[i].w)continue;
            q.push(y);dep[y]=dep[x]+1;
            if(y==t)return 1;
        }
    }
    return 0;
}
int dinic(int x,int flow,int t){
    if(x==t)return flow;
    int rest=0,k;
    for(int &i=cur[x];i;i=a[i].next){
        int y=a[i].to;
        if(dep[x]+1!=dep[y]||!a[i].w)continue;
        rest+=(k=dinic(y,min(flow-rest,a[i].w),t));
        a[i].w-=k;a[i^1].w+=k;
        if(rest==flow)return rest;
    }
    if(!rest)dep[x]=0;
    return rest;
}
int main()
{
    int ans=0,sum=0,s,t,S,T;
    scanf("%d%d%d%d",&n,&m,&S,&T);
    s=n+1;t=s+1;tot=1;
    for(int i=1;i<=m;i++){
        int x,y,l,u;
        scanf("%d%d%d%d",&x,&y,&l,&u);
        addl(x,y,u-l);d[y]+=l;d[x]-=l;
    }
    for(int i=1;i<=n;i++)
        if(d[i]>0)addl(s,i,d[i]),sum+=d[i];
        else addl(i,t,-d[i]);
    addl(T,S,inf);
    while(bfs(s,t))
        ans+=dinic(s,inf,t);
    if(ans!=sum)
        return puts("please go home to sleep");
    ans=a[tot].w;a[tot].w=a[tot^1].w=0;
    while(bfs(S,T))
        ans+=dinic(S,inf,T);
    printf("%d
",ans);
}
原文地址:https://www.cnblogs.com/QuantAsk/p/14304815.html