个人对网络流的理解

· · 个人记录

网络瘤

前言

咕了很久的网络瘤终于开写了。(背着LQ偷偷卷)

之前卷网络瘤犯了“左”的错误,想要可以的去做网络瘤24题。

但是现在决定一点一点慢慢卷。

简介

网络

网络就是一张有向图 G=(V,E)。

每条边有个容量,没有的边容量为 0,同时有一个源点和一个汇点。

性质

对于任意一个时刻,设 f(u,v) 实际流量,则整个图G的流网络满足3个性质:

1.容量限制:对于每条边,流经该边的流量不得超过该边的容量。

2.斜对称性:每条边的流量与其相反边的流量之和为 0。

3.流守恒性:从源点流出的流量等于汇点流入的流量。

常见问题

包括最大流,最小割,费用流三种问题。

最大瘤

好,直接来到最大流。

概念:最大流就是从源点流向汇点的最大流量(可以有很多条路到达汇点)。

好了,剩下的后面慢慢写。

EK 增广路算法:

#include<bits/stdc++.h>
using namespace std;
inline void read(int &x){
    char c=getchar();
    x=0;
    bool f=0;
    while(c<'0' || c>'9'){
        if(c=='-') f=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    if(f) x=-x;
}
inline void read(long long &x){
    char c=getchar();
    x=0ll;
    bool f=0;
    while(c<'0' || c>'9'){
        if(c=='-') f=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1ll)+(x<<3ll)+(c^48ll);
        c=getchar();
    }
    if(f) x=-x;
    return ;
}
inline void read(unsigned long long &x){
    char c=getchar();
    x=0ull;
    while(c<'0' || c>'9'){
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1ull)+(x<<3ull)+(c^48ull);
        c=getchar();
    }
    return ;
}
inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) write(x/10);
    putchar(x%10+48);
    return ;
}
inline void write(long long x){
    if(x<0ll){
        putchar('-');
        x=-x;
    }
    if(x>9ll) write(x/10ll);
    putchar(x%10ll+48ll);
    return ;
}
inline void write(unsigned long long x){
    if(x>9ull) write(x/10ull);
    putchar(x%10ull+48ull);
    return ;
}
#define int long long
const int maxn=205,maxm=5005<<1,INF=1ll<<60ll;
int n,m,s,t;
int head[maxn];
struct edge{
    int to,w,nex;
}e[maxm];
int tot=-1;
inline void add(int x,int y,int w){
    e[++tot].to=y,e[tot].w=w,e[tot].nex=head[x],head[x]=tot;
    e[++tot].to=x,e[tot].w=0,e[tot].nex=head[y],head[y]=tot;
}
int v[maxn],incf[maxn],pre[maxn];
inline bool bfs(){
    memset(v,0,sizeof v);
    queue<int> q;
    q.push(s); v[s]=1;
    incf[s]=INF;
    while(!q.empty()){
        int x=q.front(); q.pop();
        for(int i=head[x]; ~i ; i=e[i].nex){
            if(e[i].w){
                int y=e[i].to;
                if(v[y]) continue;
                incf[y]=min(incf[x],e[i].w);
                pre[y]=i;
                q.push(y); v[y]=1;
                if(y==t) return 1;
            }
        }
    }
    return 0;
}
int ans;
inline void up_data(){
    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;
    }
    ans+=incf[t];
}
signed main(){
    memset(head,-1,sizeof head);
    read(n),read(m),read(s),read(t);
    for(int i=1,u,v,w;i<=m;i++){
        read(u),read(v),read(w);
        add(u,v,w);
    }
    while(bfs()){
        up_data();
    }
    write(ans);
    return 0;
}

dinic 算法

#include<bits/stdc++.h>
using namespace std;
inline void read(int &x){
    char c=getchar();
    x=0;
    bool f=0;
    while(c<'0' || c>'9'){
        if(c=='-') f=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1)+(x<<3)+(c^48);
        c=getchar();
    }
    if(f) x=-x;
}
inline void read(long long &x){
    char c=getchar();
    x=0ll;
    bool f=0;
    while(c<'0' || c>'9'){
        if(c=='-') f=1;
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1ll)+(x<<3ll)+(c^48ll);
        c=getchar();
    }
    if(f) x=-x;
    return ;
}
inline void read(unsigned long long &x){
    char c=getchar();
    x=0ull;
    while(c<'0' || c>'9'){
        c=getchar();
    }
    while(c>='0' && c<='9'){
        x=(x<<1ull)+(x<<3ull)+(c^48ull);
        c=getchar();
    }
    return ;
}
inline void write(int x){
    if(x<0){
        putchar('-');
        x=-x;
    }
    if(x>9) write(x/10);
    putchar(x%10+48);
    return ;
}
inline void write(long long x){
    if(x<0ll){
        putchar('-');
        x=-x;
    }
    if(x>9ll) write(x/10ll);
    putchar(x%10ll+48ll);
    return ;
}
inline void write(unsigned long long x){
    if(x>9ull) write(x/10ull);
    putchar(x%10ull+48ull);
    return ;
}
#define int long long
const int maxn=205,maxm=5005<<1,INF=1ll<<60ll;
int n,m,s,t;
int head[maxn],d[maxn],now[maxn];
struct edge{
    int to,w,nex;
}e[maxm];
int tot=-1;
inline void add(int x,int y,int w){
    e[++tot].to=y,e[tot].w=w,e[tot].nex=head[x],head[x]=tot;
    e[++tot].to=x,e[tot].w=0,e[tot].nex=head[y],head[y]=tot;
}
inline bool bfs(){
    memset(d,0,sizeof d);
    queue<int> q;
    q.push(s);
    d[s]=1,now[s]=head[s];
    while(!q.empty()){
        int x=q.front(); q.pop();
        for(int i=head[x];~i;i=e[i].nex){
            if(e[i].w && !d[e[i].to]){
                q.push(e[i].to);
                now[e[i].to]=head[e[i].to];
                d[e[i].to]=d[x]+1;
                if(e[i].to==t) return 1;
            }
        }
    }
    return 0;
}
inline int dinic(int x,int flow){
    if(x==t) return flow;
    int rest=flow;
    for(int i=now[x];(~i) && rest;i=e[i].nex){
        now[x]=i;
        if(e[i].w && d[e[i].to]==d[x]+1){
            int k=dinic(e[i].to,min(rest,e[i].w));
            if(!k) d[e[i].to]=0;
            e[i].w-=k;
            e[i^1].w+=k;
            rest-=k;
        }
    }
    return flow-rest;
}
signed main(){
    memset(head,-1,sizeof head);
    read(n),read(m),read(s),read(t);
    for(int i=1,u,v,w;i<=m;i++){
        read(u),read(v),read(w);
        add(u,v,w);
    }
    int ans=0,flow=0;
    while(bfs()) while(flow=dinic(s,INF)) ans+=flow;
    write(ans);
    return 0;
}