网络流

fsjhhh Lv2

最大流最小割

  • Dinic算法

    时间复杂度

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    template<class T>
    struct MaxFlow {
    struct _Edge {
    int to;
    T cap;
    _Edge(int to, T cap) : to(to), cap(cap) {}
    };

    int n;
    std::vector<_Edge> e;
    std::vector<std::vector<int>> g;
    std::vector<int> cur, h;

    MaxFlow() {}
    MaxFlow(int n) {
    init(n);
    }

    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]) {
    auto [v, c] = e[i];
    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) {
    const int j = g[u][i];
    auto [v, c] = e[j];
    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;
    }
    };
  • 标题: 网络流
  • 作者: fsjhhh
  • 创建于 : 2024-08-09 20:32:07
  • 更新于 : 2024-08-09 20:42:22
  • 链接: https://fsjhhh.github.io/2024/08/09/网络流/
  • 版权声明: 本文章采用 CC BY-NC-SA 4.0 进行许可。
评论
此页目录
网络流