AcWing 2190:有源汇上下界最小流 ← Dinic算法

​【题目来源】
https://www.acwing.com/problem/content/2192/

【题目描述】
给定一个包含 n 个点 m 条边的有向图,每条边都有一个流量下界和流量上界。
给定源点 S 和汇点 T,求源点到汇点的最小流。
注意,为了方便,本题做出如下约定:可行流流量 = 从源点流出的流量 - 流入源点的流量,可以为负值。
例如,当 S→T 的流量为 0,T→S 的流量为 10 时,S→T 的流量为 −10。

【输入格式】
第一行包含四个整数 n,m,S,T。
接下来 m 行,每行包含四个整数 a,b,c,d,表示点 a 和 b 之间存在一条有向边,该边的流量下界为 c,流量上界为 d。
点编号从 1 到 n。

【输出格式】
输出一个整数表示最大流。
如果无解,则输出 No Solution。

【输入样例】
7 12 6 7
6 1 0 2147483647
1 7 0 2147483647
6 2 0 2147483647
2 7 0 2147483647
6 3 0 2147483647
3 7 0 2147483647
6 4 0 2147483647
4 7 0 2147483647
6 5 0 2147483647
5 7 0 2147483647
5 1 1 2147483647
3 4 1 2147483647

【输出样例】
2

【数据范围】
1≤n≤50003,
1≤m≤125003,
1≤a,b≤n,
0≤c≤d≤2147483647,
数据保证答案不超过 int 范围。

【算法分析】
Dinic算法:https://blog.csdn.net/hnjzsyjyj/article/details/161317988

【算法代码】
● 由于本题数据很大,连 0x3f3f3f3f 都表示不了,所以 INF 用题设的 2147483647 表示。

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;
const int INF=2147483647;
const int N=5e4+5,M=2e6+5;
int h[N],e[M],f[M],ne[M],idx;
int q[N],d[N],cur[N],A[N];
int n,m,s,t,S,T;

void add(int a,int b,int c) {
    f[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
    f[idx]=0,e[idx]=a,ne[idx]=h[b],h[b]=idx++;
}

bool bfs() {
    memset(d,-1,sizeof d);
    int hh=0,tt=0;
    q[0]=S,d[S]=0;

    while(hh<=tt) {
        int u=q[hh++];
        for(int i=h[u]; ~i; i=ne[i]) {
            int v=e[i];
            if(d[v]==-1 && f[i]) {
                d[v]=d[u]+1;
                q[++tt]=v;
            }
        }
    }
    return d[T]!=-1;
}

int dfs(int u,int lim) {
    if(u==T) return lim;
    int flow=0;
    for(int i=cur[u]; ~i && flow<lim; i=ne[i]) {
        cur[u]=i;
        int v=e[i];
        if(d[v]==d[u]+1 && f[i]) {
            int t=dfs(v,min(f[i],lim-flow));
            if(!t) d[v]=-1;
            f[i]-=t,f[i^1]+=t,flow+=t;
        }
    }
    return flow;
}

LL dinic() {
    LL r=0,flow=0;
    while(bfs()) { //0~T
        for(int i=0; i<=T; i++) cur[i]=h[i];
        while(flow=dfs(S,INF)) r+=flow;
    }
    return r;
}

int main() {
    memset(h,-1,sizeof h);
    cin>>n>>m>>s>>t;
    S=0,T=n+1;
    while(m--) {
        int a,b,c,d;
        cin>>a>>b>>c>>d;
        add(a,b,d-c);
        A[a]-=c,A[b]+=c;
    }

    int tot=0;
    for(int i=1; i<=n; i++) {
        if(A[i]>0) add(S,i,A[i]),tot+=A[i];
        else if(A[i]<0) add(i,T,-A[i]);
    }

    add(t,s,INF);

    if(dinic()==tot) {
        int res=f[idx-1];
        f[idx-1]=f[idx-2]=0;
        S=t,T=s;
        cout<<res-dinic()<<endl;
    } else cout<<"No Solution\n";

    return 0;
}

/*
in:
7 12 6 7
6 1 0 2147483647
1 7 0 2147483647
6 2 0 2147483647
2 7 0 2147483647
6 3 0 2147483647
3 7 0 2147483647
6 4 0 2147483647
4 7 0 2147483647
6 5 0 2147483647
5 7 0 2147483647
5 1 1 2147483647
3 4 1 2147483647

out:
2
*/

备注:修改“AcWing 2190:有源汇上下界最小流”的如下两行代码, 便得到“AcWing 2189. 有源汇上下界最大流”的代码:https://blog.csdn.net/hnjzsyjyj/article/details/161441409
(1)将第 80 行的 S=t,T=s; 修改为 S=s,T=t; 
(2)将第 81 行的 cout<<res-dinic()<<endl; 修改为 cout<<res+dinic()<<endl;





【参考文献】
https://www.acwing.com/solution/content/123871/
https://blog.csdn.net/hnjzsyjyj/article/details/161441409
https://blog.csdn.net/hnjzsyjyj/article/details/161317988
https://blog.csdn.net/hnjzsyjyj/article/details/127179286
 

​

posted @ 2026-05-27 22:44  Triwa  阅读(26)  评论(0)    收藏  举报