Luzhiled's Library

This documentation is automatically generated by online-judge-tools/verification-helper

View the Project on GitHub ei1333/library

:heavy_check_mark: Dinic(最大流)
(graph/flow/dinic.hpp)

概要

最大流を求めるアルゴリズム.

残余グラフ上の辺数最小のパスを BFS により探して, DFS により増加パスがとれなくなるまでフローを流すことを繰り返し, 流せなくなったら終了する.

使い方

計算量

$O(EV^2)$

実測では高速に動作することが多いが, ネットワークが直接入力で与えられるなど最悪ケースを構成できる場合は遅いのでその場合は Push-Relabel などの別の最大流アルゴリズムを検討する必要がある.

$2$ 部グラフの最大マッチングのとき $O(E \sqrt V)$, 辺容量が全て同じ場合 $O(\min (V^{\frac {2} {3}}, E^{\frac {1} {2}}) E)$ となる.

Required by

Verified with

Code

/**
 * @brief Dinic(最大流)
 * @docs docs/dinic.md
 */
template< typename flow_t >
struct Dinic {
  const flow_t INF;

  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
    int idx;
  };

  vector< vector< edge > > graph;
  vector< int > min_cost, iter;

  explicit Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}

  void add_edge(int from, int to, flow_t cap, int idx = -1) {
    graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});
    graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});
  }

  bool build_augment_path(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue< int > que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty() && min_cost[t] == -1) {
      int p = que.front();
      que.pop();
      for(auto &e: graph[p]) {
        if(e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return min_cost[t] != -1;
  }

  flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow) {
    if(idx == t) return flow;
    for(int &i = iter[idx]; i < (int) graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  flow_t max_flow(int s, int t) {
    flow_t flow = 0;
    while(build_augment_path(s, t)) {
      iter.assign(graph.size(), 0);
      flow_t f;
      while((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f;
    }
    return flow;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e: graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }

  vector< bool > min_cut(int s) {
    vector< bool > used(graph.size());
    queue< int > que;
    que.emplace(s);
    used[s] = true;
    while(not que.empty()) {
      int p = que.front();
      que.pop();
      for(auto &e: graph[p]) {
        if(e.cap > 0 and not used[e.to]) {
          used[e.to] = true;
          que.emplace(e.to);
        }
      }
    }
    return used;
  }
};
#line 1 "graph/flow/dinic.hpp"
/**
 * @brief Dinic(最大流)
 * @docs docs/dinic.md
 */
template< typename flow_t >
struct Dinic {
  const flow_t INF;

  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
    int idx;
  };

  vector< vector< edge > > graph;
  vector< int > min_cost, iter;

  explicit Dinic(int V) : INF(numeric_limits< flow_t >::max()), graph(V) {}

  void add_edge(int from, int to, flow_t cap, int idx = -1) {
    graph[from].emplace_back((edge) {to, cap, (int) graph[to].size(), false, idx});
    graph[to].emplace_back((edge) {from, 0, (int) graph[from].size() - 1, true, idx});
  }

  bool build_augment_path(int s, int t) {
    min_cost.assign(graph.size(), -1);
    queue< int > que;
    min_cost[s] = 0;
    que.push(s);
    while(!que.empty() && min_cost[t] == -1) {
      int p = que.front();
      que.pop();
      for(auto &e: graph[p]) {
        if(e.cap > 0 && min_cost[e.to] == -1) {
          min_cost[e.to] = min_cost[p] + 1;
          que.push(e.to);
        }
      }
    }
    return min_cost[t] != -1;
  }

  flow_t find_min_dist_augment_path(int idx, const int t, flow_t flow) {
    if(idx == t) return flow;
    for(int &i = iter[idx]; i < (int) graph[idx].size(); i++) {
      edge &e = graph[idx][i];
      if(e.cap > 0 && min_cost[idx] < min_cost[e.to]) {
        flow_t d = find_min_dist_augment_path(e.to, t, min(flow, e.cap));
        if(d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          return d;
        }
      }
    }
    return 0;
  }

  flow_t max_flow(int s, int t) {
    flow_t flow = 0;
    while(build_augment_path(s, t)) {
      iter.assign(graph.size(), 0);
      flow_t f;
      while((f = find_min_dist_augment_path(s, t, INF)) > 0) flow += f;
    }
    return flow;
  }

  void output() {
    for(int i = 0; i < graph.size(); i++) {
      for(auto &e: graph[i]) {
        if(e.isrev) continue;
        auto &rev_e = graph[e.to][e.rev];
        cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/" << e.cap + rev_e.cap << ")" << endl;
      }
    }
  }

  vector< bool > min_cut(int s) {
    vector< bool > used(graph.size());
    queue< int > que;
    que.emplace(s);
    used[s] = true;
    while(not que.empty()) {
      int p = que.front();
      que.pop();
      for(auto &e: graph[p]) {
        if(e.cap > 0 and not used[e.to]) {
          used[e.to] = true;
          que.emplace(e.to);
        }
      }
    }
    return used;
  }
};
Back to top page