模板分享:网络最大流

Code

改自 jiangly 的模板,使用 Dinic 算法.

template<class T>
struct MaxFlow {
    struct _Edge {
        int to;
        T cap;
        _Edge(int to, T cap) : to(to), cap(cap) {}
    };
    
    int n;
    T inf;
    std::vector<_Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<int> cur, h;
    
    MaxFlow() {}
    MaxFlow(int n) {
        init(n);
        inf = numeric_limits<T>::max();
    }
    
    void init(int n) {
        this->n = n;
        e.clear();
        g.assign(n, {});
        cur.resize(n);
        h.resize(n);
    }
    
    bool bfs(int s, int t) {
        h.assign(n, -1);
        std::queue<int> que;
        h[s] = 0;
        que.push(s);
        while (!que.empty()) {
            const int u = que.front();
            que.pop();
            for (int i : g[u]) {
                int v = e[i].to;
                T c = e[i].cap;
                if (c > 0 && h[v] == -1) {
                    h[v] = h[u] + 1;
                    if (v == t) {
                        return true;
                    }
                    que.push(v);
                }
            }
        }
        return false;
    }
    
    T dfs(int u, int t, T f) {
        if (u == t) {
            return f;
        }
        auto r = f;
        for (int i = cur[u]; i < int(g[u].size()); ++i) {
            cur[u] = i;
            const int j = g[u][i];
            int v = e[j].to;
            T c = e[j].cap;
            if (c > 0 && h[v] == h[u] + 1) {
                auto a = dfs(v, t, std::min(r, c));
                e[j].cap -= a;
                e[j ^ 1].cap += a;
                r -= a;
                if (r == 0) {
                    return f;
                }
            }
        }
        return f - r;
    }
    void addEdge(int u, int v, T c) {
        g[u].push_back(e.size());
        e.emplace_back(v, c);
        g[v].push_back(e.size());
        e.emplace_back(u, 0);
    }
    T flow(int s, int t) {
        T ans = 0;
        while (bfs(s, t)) {
            cur.assign(n, 0);
            ans += dfs(s, t, std::numeric_limits<T>::max());
        }
        return ans;
    }
    
    std::vector<bool> minCut() {
        std::vector<bool> c(n);
        for (int i = 0; i < n; i++) {
            c[i] = (h[i] != -1);
        }
        return c;
    }
    
    struct Edge {
        int from;
        int to;
        T cap;
        T flow;
    };
    std::vector<Edge> edges() {
        std::vector<Edge> a;
        for (int i = 0; i < e.size(); i += 2) {
            Edge x;
            x.from = e[i + 1].to;
            x.to = e[i].to;
            x.cap = e[i].cap + e[i + 1].cap;
            x.flow = e[i + 1].cap;
            a.push_back(x);
        }
        return a;
    }
};

Usage

MaxFlow<T>::MaxFlow(int n);

创建一个网络图,有 n n n 个点,没有弧,边权类型为 T(只支持整数)

void MaxFlow<T>::addEdge(int u, int v, T c);

在图中加入一条 u → v u\to v uv,流量为 c c c 的有向弧.
0 ≤ u , v < n 0\le u,v< n 0u,v<n c c c 不超过 T 的范围.

T MaxFlow<T>::flow(int s, int t);

求出 s s s t t t 的最大流(图会变为残量网络)
需要保证答案在 T 范围内.
0 ≤ s , t < n 0\le s,t< n 0s,t<n

vector<bool> MaxFlow<T>::minCut();

在跑完最小割的图上,返回每个点属于哪个集合(属于 S S S 0 0 0,属于 T T T 1 1 1

vector<Edge> MaxFlow<T>::edges();

按照加边顺序,返回图中的所有弧的列表,以 ( u , v , cap , flow ) (u,v,\textit{cap},\textit{flow}) (u,v,cap,flow) 形式保存.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值