AcWing 2234:多源汇最大流 ← Dinic算法

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

【题目描述】
给定一个包含 n 个点 m 条边的有向图,并给定每条边的容量,边的容量非负。
其中有 Sc 个源点,Tc 个汇点。
图中可能存在重边和自环。
求整个网络的最大流。

【输入格式】
第一行包含四个整数 n,m,Sc,Tc。
第二行包含 Sc 个整数,表示所有源点的编号。
第三行包含 Tc 个整数,表示所有汇点的编号。
接下来 m 行,每行三个整数 u,v,c,表示从点 u 到点 v 存在一条有向边,容量为 c。
点的编号从 1 到 n。​​​​​​​

【输出格式】
输出一个整数表示整个网络的最大流。​​​​​​​

【输入样例】
4 5 2 2
2 4
1 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40

【输出样例】
70

【数据范围】
2≤n≤10000,
1≤m≤10^5,
0≤c≤10000,
保证源点集合和汇点集合没有交集。​​​​​​​

【算法分析】
● 快读函数:https://blog.csdn.net/hnjzsyjyj/article/details/120131534

int read() { //fast read
    int x=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') { //!isdigit(c)
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9') { //isdigit(c)
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}

● Dinic 算法:https://blog.csdn.net/hnjzsyjyj/article/details/161317988

【算法代码一】
网络流的 Dinic 算法代码中,表示边数的常量 M 通常取题设条件边数的 4 倍,可防止 TLE。

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

typedef long long LL;
const int N=1e4+5,M=4e5+5;
const int INF=0x3f3f3f3f;

int n,m,S,T;
int h[N],e[M],ne[M],f[M],idx;
int d[N],cur[N],q[N];

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);
    int sc,tc,s,t;
    cin>>n>>m>>sc>>tc;
    S=0,T=n+1;
    while(sc--) cin>>s,add(S,s,INF);
    while(tc--) cin>>t,add(t,T,INF);

    while(m--) {
        int a,b,c;
        cin>>a>>b>>c;
        add(a,b,c);
    }
    cout<<dinic()<<endl;

    return 0;
}

/*
in:
4 5 2 2
2 4
1 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40

out:
70
*/

【算法代码二:快读+Dinic算法】
实践证明,本题不用快读,只需如在算法代码一中,把表示边数的常量 M 取为题设条件边数的 4 倍,即可防止 TLE。

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

typedef long long LL;
const int N=1e4+5,M=4e5+5;
const int INF=0x3f3f3f3f;

int n,m,S,T;
int h[N],e[M],ne[M],f[M],idx;
int d[N],cur[N],q[N];

int read() { //fast read
    int x=0,f=1;
    char c=getchar();
    while(c<'0' || c>'9') { //!isdigit(c)
        if(c=='-') f=-1;
        c=getchar();
    }
    while(c>='0' && c<='9') { //isdigit(c)
        x=x*10+c-'0';
        c=getchar();
    }
    return x*f;
}

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);
    int sc,tc,s,t;
    n=read(),m=read(),sc=read(),tc=read();
    S=0,T=n+1;
    while(sc--) s=read(),add(S,s,INF);
    while(tc--) t=read(),add(t,T,INF);

    while(m--) {
        int a,b,c;
        a=read(),b=read(),c=read();
        add(a,b,c);
    }
    cout<<dinic()<<endl;

    return 0;
}

/*
in:
4 5 2 2
2 4
1 3
4 2 30
4 3 20
2 3 20
2 1 30
1 3 40

out:
70
*/



【参考文献】
https://blog.csdn.net/hnjzsyjyj/article/details/161317988
https://blog.csdn.net/hnjzsyjyj/article/details/161438841
https://blog.csdn.net/hnjzsyjyj/article/details/161441409
https://blog.csdn.net/hnjzsyjyj/article/details/161446211
 

​

posted @ 2026-05-28 20:14  Triwa  阅读(26)  评论(0)    收藏  举报