Dinic 算法

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using p = pair<int, int>;
const int inf(0x3f3f3f3f);
const int maxn(1e4 + 10);
const int maxm(2e5 + 10);
int ecnt, head[maxn];
int dep[maxn], cur[maxn];

struct edge {
    int to, flow, nxt;
} edges[maxm];

template<typename T = int>
inline const T read()
{
    T x = 0, f = 1;
    char ch = getchar();
    while (ch < '0' or ch > '9') {
        if (ch == '-') f = -1;
        ch = getchar();
    }
    while (ch >= '0' and ch <= '9') {
        x = (x << 3) + (x << 1) + ch - '0';
        ch = getchar();
    }
    return x * f;
}

template<typename T>
inline void write(T x, bool ln)
{
    if (x < 0) {
        putchar('-');
        x = -x;
    }
    if (x > 9) write(x / 10, false);
    putchar(x % 10 + '0');
    if (ln) putchar(10);
}

inline void addEdge(int u, int v, int w)
{
    edges[ecnt].to = v;
    edges[ecnt].flow = w;
    edges[ecnt].nxt = head[u];
    head[u] = ecnt++;
}

bool bfs(int src, int des)
{
    queue<int> q;
    memset(dep, -1, sizeof(dep));
    dep[src] = 0;
    q.push(src);
    while (not q.empty()) {
        int u = q.front();
        q.pop();
        for (int i = head[u]; compl i; i = edges[i].nxt) {
            int v = edges[i].to, w = edges[i].flow;
            if (dep[v] == -1 and w) {
                q.push(v);
                dep[v] = dep[u] + 1;
            }
        }
    }
    return compl dep[des];
}

int dfs(int u, int des, int rflow)
{
    if (u == des) return rflow;
    int res = 0;
    for (int i = cur[u]; compl i and rflow; i = edges[i].nxt) {
        int v = edges[i].to;
        if (dep[v] not_eq dep[u] + 1 or not edges[i].flow) continue;
        cur[u] = i;
        int flow = dfs(v, des, min(edges[i].flow, rflow));
        edges[i].flow -= flow;
        edges[i xor 1].flow += flow;
        rflow -= flow;
        res += flow;
    }
    if (not res) dep[u] = -1;
    return res;
}

ll dinic(int src, int des)
{
    ll res = 0;
    while (bfs(src, des)) {
        memcpy(cur, head, sizeof(head));
        res += dfs(src, des, inf);
    }
    return res;
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
#endif
    int n = read(), m = read(), s = read(), t = read();
    memset(head, -1, sizeof(head));
    while (m--) {
        int u = read(), v = read(), w = read();
        addEdge(u, v, w);
        addEdge(v, u, 0);
    }
    write(dinic(s, t), true);
    return 0;
}

最后更新于