上下界可行流留档

· · 个人记录

之前学的时候就好一顿理解不了,现在又忘了。于是记一下。

无源汇

Code

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long

using namespace std;

inline int read(){
    register int x = 0, f = 1; register char ch = getchar();
    for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
    return x * f;
}

const int Inf = 2147483647;
const int N = 3e4 + 10;
const int s = 3e4 + 3;
const int t = 3e4 + 4;

int n, m;
int ans;
int fl[N];
int low[N];

int head[N << 1], Next[N << 1], e[N << 1], idx = 1, f[N << 1];
inline void add(int a, int b, int c, int d){
    e[++ idx] = b; Next[idx] = head[a]; head[a] = idx; f[idx] = d - c; low[idx] = c;
    e[++ idx] = a; Next[idx] = head[b]; head[b] = idx; f[idx] = 0;
}

int d[N], q[N], cur[N], hh, tt;
bool bfs(){
    tt = hh = 0;
    memset(d, -1, sizeof d);
    q[0] = s; d[s] = 0; cur[s] = head[s];
    while(hh <= tt){
        int tp = q[hh ++];
        for(int i = head[tp]; i; i = Next[i]){
            int j = e[i];
            if(d[j] == -1 && f[i]){
                d[j] = d[tp] + 1;
                cur[j] = head[j];
                if(j == t) return true;
                q[++ tt] = j;
            }
        }
    }
    return false;
}

int dfs(int now, int limit){
    if(now == t) return limit;
    int flow = 0;
    for(int i = cur[now]; i && flow < limit; i = Next[i]){
        cur[now] = i;
        int j = e[i];
        if(d[j] == d[now] + 1 && f[i]){
            int t = dfs(j, min(f[i], limit - flow));
            if(!t) d[j] = -1;
            f[i] -= t; f[i ^ 1] += t; flow += t;
        }
    }
    return flow;
}

void dinic(){
    int flow;
    while(bfs()) while(flow = dfs(s, Inf)) ans += flow;
}

signed main(){
    n = read(), m = read();
    for(int i = 1; i <= m; ++ i){
        int a = read(), b = read(), c = read(), d = read();
        fl[a] -= c; fl[b] += c;
        add(a, b, c, d);
    }
    int sum = 0;
    for(int i = 1; i <= n; ++ i){
        if(fl[i] < 0) add(i, t, fl[i], 0);
        else add(s, i, 0, fl[i]), sum += fl[i];
    }
    dinic(); if(ans < sum) puts("NO");
    else{
        puts("YES");
        for(int i = 2; i <= (m << 1) + 1; i += 2) printf("%lld\n", f[i ^ 1] + low[i]);
    }
    return 0;
}

有源汇最大流


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#define int long long

using namespace std;

inline int read(){
    register int x = 0, f = 1; register char ch = getchar();
    for(; ch > '9' || ch < '0'; ch = getchar()) if(ch == '-') f = -1;
    for(; ch >= '0' && ch <= '9'; ch = getchar()) x = (x << 1) + (x << 3) + (ch ^ '0');
    return x * f;
}

const int Inf = 2147483647;
const int N = 3e4 + 10;
int s = 3e4 + 3;
int t = 3e4 + 4;

int n, m, S, T;
int ans;
int fl[N];
int low[N];

int head[N << 1], Next[N << 1], e[N << 1], idx = 1, f[N << 1];
inline void add(int a, int b, int c, int d){
    e[++ idx] = b; Next[idx] = head[a]; head[a] = idx; f[idx] = d - c; low[idx] = c;
    e[++ idx] = a; Next[idx] = head[b]; head[b] = idx; f[idx] = 0;
}

int d[N], q[N], cur[N], hh, tt;
bool bfs(){
    tt = hh = 0;
    memset(d, -1, sizeof d);
    q[0] = s; d[s] = 0; cur[s] = head[s];
    while(hh <= tt){
        int tp = q[hh ++];
        for(int i = head[tp]; i; i = Next[i]){
            int j = e[i];
            if(d[j] == -1 && f[i]){
                d[j] = d[tp] + 1;
                cur[j] = head[j];
                if(j == t) return true;
                q[++ tt] = j;
            }
        }
    }
    return false;
}

int dfs(int now, int limit){
    if(now == t) return limit;
    int flow = 0;
    for(int i = cur[now]; i && flow < limit; i = Next[i]){
        cur[now] = i;
        int j = e[i];
        if(d[j] == d[now] + 1 && f[i]){
            int t = dfs(j, min(f[i], limit - flow));
            if(!t) d[j] = -1;
            f[i] -= t; f[i ^ 1] += t; flow += t;
        }
    }
    return flow;
}

void dinic(){
    int flow; ans = 0; 
    while(bfs()) while(flow = dfs(s, Inf)) ans += flow;
}

signed main(){
    n = read(), m = read(); S = read(); T = read();
    for(int i = 1; i <= m; ++ i){
        int a = read(), b = read(), c = read(), d = read();
        fl[a] -= c; fl[b] += c;
        add(a, b, c, d);
    }
    int sum = 0;
    for(int i = 1; i <= n; ++ i){
        if(fl[i] < 0) add(i, t, fl[i], 0);
        else add(s, i, 0, fl[i]), sum += fl[i];
    }
    add(T, S, 0, Inf); 
    dinic();

    if(ans < sum) puts("No Solution");
    else{
        int res = f[idx]; 
        f[idx] = f[idx ^ 1] = 0;
        s = S; t = T;
        dinic();
        printf("%lld\n", ans + res);
    }
    return 0;
}

有源汇最小流

改为从 T 向 S 跑,res - ans 即可。