跳到正文

算法·理论

网络流模板

de_mosyt约 6 分钟
原文
文章信息与版本

文章信息

发布
2025年2月24日
收录
2026年1月25日
更新
2026年11月14日
篇幅
约 6 分钟

版本

  1. v1当前

那啥解释先不写了

1.Edminds-Karp

C++
#include<bits/stdc++.h>
using namespace std;
#define next dfiughdgrkkj
#define int long long
#define N 2010
#define M 20010
const int inf=1<<29;
int n,m,s,t,incf[N],pre[N];
int head[M],cnt,vis[N];
int maxflow=0;
struct node{
    int next,to,w;
}e[M];
void add(int from,int to,int w){
    cnt++; e[cnt].next=head[from];
    e[cnt].to=to; e[cnt].w=w;
    head[from]=cnt;
}
bool bfs(){
    memset(vis,0,sizeof(vis));
    queue<int> q;
    q.push(s); vis[s]=1;
    incf[s]=inf;
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=head[u];i;i=e[i].next){
            if(e[i].w!=0){
                int v=e[i].to;
                if(vis[v]) continue;
                incf[v]=min(incf[u],e[i].w);
                pre[v]=i;
                q.push(v); vis[v]=1;
                if(v==t) return 1;
            }
        }
    }
    return 0;
}
void update(){
    int x=t;
    while(x!=s){
        int i=pre[x];
        e[i].w-=incf[t];
        e[i^1].w+=incf[t];
        x=e[i^1].to;
    }
    maxflow+=incf[t];
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>s>>t;
    memset(head,0,sizeof(head));
    cnt=1;
    for(int i=1;i<=m;i++){
        int x,y,w;
        cin>>x>>y>>w;
        add(x,y,w); add(y,x,0);
    }
    while(bfs()) update();
    cout<<maxflow<<'\n';
    return 0;
}

2.dinic

C++
#include<bits/stdc++.h>
using namespace std;
#define next dfiughdgrkkj
#define int long long
#define N 50010
#define M 300010
const int inf=1<<29;
int n,m,s,t,now[M],d[N]; //d:depth
int head[M],cnt,vis[N];
int maxflow=0;
struct node{
    int next,to,w;
}e[M];
int min(int a,int b){if(a>b) return b; return a;}
void add(int from,int to,int w){
    cnt++; e[cnt].next=head[from];
    e[cnt].to=to; e[cnt].w=w;
    head[from]=cnt;
}
bool bfs(){
    memset(d,inf,sizeof(d));
    queue<int> q;
    q.push(s); d[s]=1; now[s]=head[s];
    while(!q.empty()){
        int u=q.front(); q.pop();
        for(int i=head[u];i;i=e[i].next){
            int v=e[i].to;
            if(e[i].w!=0&&d[v]==0){
                q.push(v);
                now[v]=head[v];
                d[v]=d[u]+1;
                if(v==t) return 1;
            }
        }
    }
    return 0;
}
int dinic(int x,int flow){
    if(x==t) return flow;
    int rest=flow,k;
    for(int i=now[x];i&&rest;i=e[i].next){
        now[x]=i; int v=e[i].to;
        if(e[i].w!=0&&(d[v]==d[x]+1)){
            k=dinic(v,min(rest,e[i].w));
            if(!k) d[v]=0;
            e[i].w-=k;
            e[i^1].w+=k;
            rest-=k;
        }
    }
    return flow-rest;
}
signed main(){
    ios::sync_with_stdio(false);
    cin.tie(0),cout.tie(0);
    cin>>n>>m>>s>>t;
    cnt=1;
    for(int i=1;i<=m;i++){
        int x,y,w;
        cin>>x>>y>>w;
        add(x,y,w); add(y,x,0);
    }
    int flow=0;
    while(bfs()){
        while(flow=dinic(s,inf)) maxflow+=flow;
    }
    cout<<maxflow;
    return 0;
}
原文