P7173 【模板】有负圈的费用流 题解

· · 题解

Description

给出一张有向图,求最小费用最大流。费用可能为负。

Solution

如果费用为负,SSP 类算法在求最短路的时候就会因为负环卡死。因此可以这么想。

对于一条边 (u,v,f,c)\in E,f 是容量,c 是费用。如果 c<0,等于说流从这条边流过,不仅不花费,反而还盈利,那肯定是从这条边流得越多越好。因此我们预先让这种费用为负的边满流。但因为还要保持流量平衡,我们还需要设置一种机制,让预先流出去的流量可以退回来。

具体的,可以使用上下界网络流。对于新添的的一条边 (u,v,f,c)。如果费用为负,就先建一条 u\to v 的上下界均为 f 费用为 c 的边,(因为上下界相同,这条费用为负的边本质并不在残量网络中,建边只是代表预先满流),再连一条 v\to u 的下界为 0 上界为 f 费用为 -c 的边,这条边流过多少就表示退回来多少流量。如果边权非负,就像费用非负的普通费用流,直接连一条 u\to v,下界为 0 上界为 f 费用为 c 的边即可。

上下界网络流

先让边的下界满流,虚拟超级源汇 S',T',再遍历每个点 i,如果推进来的流量多 W_i,W_i>0,连边 (S',i,W_i,0);如果流量少 W_i,W_i<0,连边 (i,T',-W_i,0)。连一条边 (T,S,+\infty,0)。跑一遍用最小费用最大流,用以平衡流量。删掉边 (T,S,+\infty,0),再跑一遍最小费用最大流,得到答案。

Code

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

class Dinic {
    public:
        typedef int TYPE;
        static const int maxn = 1e4, inf = 0x7f7f7f7f, maxm = inf;
        class edge {
            public:
                int to, rev;
                TYPE flow, cost;
                edge(int to, TYPE flow, TYPE cost, int rev): to(to), flow(flow), cost(cost), rev(rev) {}
                edge() {}
        };
        std::vector<edge>vec[maxn];
        TYPE MinCost, MaxFlow, dist[maxn], W[maxn];
        int cur[maxn], n, m, s, t;
        bool vis[maxn];
        Dinic(int n, int m, int s, int t): n(n), m(m), s(s), t(t),
            MinCost(0), MaxFlow(0), cur({0}), dist({0}), vis({false}) {}
        Dinic() {}
        inline void _Add_Edge_(int from, int to, TYPE flow, TYPE cost) {
            vec[from].push_back(edge(to, flow, cost, vec[to].size()));
            vec[to].push_back(edge(from, 0, -cost, vec[from].size() - 1));
        }
        inline void Add_Edge(int from, int to, TYPE flowL, TYPE flowR, TYPE cost) {
            W[from] -= flowL, W[to] += flowL;
            MinCost += flowL * cost;
            _Add_Edge_(from, to, flowR - flowL, cost);
        }
        inline bool SPFA() {
            std::memset(dist, inf, sizeof(dist));
            dist[t] = 0;
            std::memset(cur, 0, sizeof(cur));
            std::queue<int>que;
            for (que.push(t), vis[t] = true; !((bool)  que.empty());) {
                int u = que.front();
                vis[u] = false;
                que.pop();
                for (edge &e : vec[u]) {
                    int v = e.to;
                    if (vec[v][e.rev].flow && dist[u] + vec[v][e.rev].cost < dist[v]) {
                        dist[v] = dist[u] + vec[v][e.rev].cost;
                        if (!vis[v]) {
                            que.push(v);
                            vis[v] = true;
                        }
                    }
                }
            }
            return dist[s] != inf;
        }
        inline TYPE dfs(int u, TYPE flow) {
            if (u == t || !flow) {
                return flow;
            }
            vis[u] = true;
            TYPE res = 0;
            int sz = vec[u].size();
            for (int &i = cur[u]; i < sz && flow; ++i) {
                edge &e = vec[u][i];
                int v = e.to;
                if (e.flow && dist[u] == dist[v] + e.cost && !vis[v]) {
                    TYPE Preflow = dfs(v, std::min(flow, e.flow));
                    res += Preflow, flow -= Preflow, MinCost += Preflow * (e.cost);
                    e.flow -= Preflow, vec[v][e.rev].flow += Preflow;
                }
            }
            vis[u] = false;
            return res;
        }
        inline TYPE Get_MaxFlow_MinCost() {
            MaxFlow = 0;
            while (SPFA()) {
                std::memset(vis, false, sizeof(vis));
                TYPE Preflow = dfs(s, inf);
                MaxFlow += Preflow;
            }
            return MaxFlow;
        }
        inline TYPE clac(int S, int T) {
            Add_Edge(T, S, 0, inf, 0);
            int PreS = n + 1, PreT = n + 2;
            this->s = PreS, this->t = PreT;
            TYPE sum = 0;
            for (int i = 0; i <= n; ++i) {
                if (W[i] > 0) {
                    Add_Edge(PreS, i, 0, W[i], 0);
                    sum += W[i];
                } else if (W[i] < 0) {
                    Add_Edge(i, PreT, 0, -W[i], 0);
                }
            }
            if (Get_MaxFlow_MinCost() < sum) {
                return -1;
            } else {
                TYPE res = vec[S].back().flow;
                vec[S].pop_back();
                vec[T].pop_back();
                this->s = S, this->t = T;
                res = Get_MaxFlow_MinCost();
                return (MaxFlow = res);
            }
        }
        inline void Solve() {
            int N, M, S, T;
            scanf("%d%d%d%d", &N, &M, &S, &T);
            this[0] = Dinic(N, M, S, T);
            for (int i = 1; i <= M; ++i) {
                int u, v;
                TYPE flow, cost;
                scanf("%d%d%d%d", &u, &v, &flow, &cost);
                if (cost < 0) {
                    W[u] -= flow, W[v] += flow;
                    MinCost += cost * flow;
                    Add_Edge(v, u, 0, flow, -cost);
                } else {
                    Add_Edge(u, v, 0, flow, cost);
                }
            }
            clac(S, T);
            printf("%d %d\n", MaxFlow, MinCost);
        }
};
Dinic Main;

int main() {
    Main.Solve();
    return 0;
}