結果

問題 No.3668 Minimum Cut
コンテスト
ユーザー ei1333333
提出日時 2026-09-06 13:26:39
言語 C++23(gcc16)
(gcc 16.1.0 + boost 1.92.0 + ACL)
コンパイル:
g++-16 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 53 ms / 2,000 ms
+ 618µs
コード長 7,459 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 7,001 ms
コンパイル使用メモリ 393,540 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-09-06 13:26:50
合計ジャッジ時間 10,154 ms
ジャッジサーバーID
(参考情報)
judge1_0 / judge3_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#line 1 "template/template.hpp"
#include <bits/stdc++.h>

#if __has_include(<atcoder/all>)
#include <atcoder/all>

#endif

using namespace std;

using int64 = long long;

const int64 infll = (1LL << 62) - 1;
const int inf = (1 << 30) - 1;

struct IoSetup {
  IoSetup() {
    cin.tie(nullptr);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
    cerr << fixed << setprecision(10);
  }
} iosetup;

template <typename T1, typename T2>
ostream& operator<<(ostream& os, const pair<T1, T2>& p) {
  os << p.first << " " << p.second;
  return os;
}

template <typename T1, typename T2>
istream& operator>>(istream& is, pair<T1, T2>& p) {
  is >> p.first >> p.second;
  return is;
}

template <typename T>
ostream& operator<<(ostream& os, const vector<T>& v) {
  for (size_t i = 0; i < v.size(); i++) {
    os << v[i] << (i + 1 != v.size() ? " " : "");
  }
  return os;
}

template <typename T>
istream& operator>>(istream& is, vector<T>& v) {
  for (T& in : v) is >> in;
  return is;
}

template <typename T1, typename T2>
bool chmax(T1& a, T2 b) {
  return a < b && (a = b, true);
}

template <typename T1, typename T2>
bool chmin(T1& a, T2 b) {
  return a > b && (a = b, true);
}

template <typename T = int64>
vector<T> make_v(size_t a) {
  return vector<T>(a);
}

template <typename T, typename... Ts>
auto make_v(size_t a, Ts... ts) {
  return vector<decltype(make_v<T>(ts...))>(a, make_v<T>(ts...));
}

template <typename T, typename V>
enable_if_t<is_class_v<T> == 0> fill_v(T& t, const V& v) {
  t = v;
}

template <typename T, typename V>
enable_if_t<is_class_v<T> != 0> fill_v(T& t, const V& v) {
  for (auto& e : t) fill_v(e, v);
}

template <typename F>
struct FixPoint : F {
  explicit FixPoint(F&& f) : F(std::forward<F>(f)) {}

  template <typename... Args>
  decltype(auto) operator()(Args&&... args) const {
    return F::operator()(*this, std::forward<Args>(args)...);
  }
};

template <typename F>
decltype(auto) MFP(F&& f) {
  return FixPoint<F>{std::forward<F>(f)};
}

#line 2 "graph/flow/push-relabel.hpp"

#include <algorithm>
#include <iterator>
#include <numeric>
#include <queue>
#include <vector>

class Stack {
 private:
  const int N, H;
  std::vector<int> node;

 public:
  Stack(const int N, const int H) : N(N), H(H), node(N + H) { clear(); }

  inline bool empty(const int h) const { return node[N + h] == N + h; }

  inline int top(const int h) const { return node[N + h]; }

  inline void pop(const int h) { node[N + h] = node[node[N + h]]; }

  inline void push(const int h, const int u) {
    node[u] = node[N + h], node[N + h] = u;
  }

  inline void clear() { std::iota(node.begin() + N, node.end(), N); }
};

class List {
 public:
  struct node {
    int prev, next;
  };
  const int N, H;
  std::vector<node> dat;

  List(const int N, const int H) : N(N), H(H), dat(N + H) { clear(); }

  inline bool empty(const int h) const { return (dat[N + h].next == N + h); }

  inline bool more_one(const int h) const {
    return dat[N + h].prev != dat[N + h].next;
  }

  inline void insert(const int h, const int u) {
    dat[u].prev = dat[N + h].prev, dat[u].next = N + h;
    dat[dat[N + h].prev].next = u, dat[N + h].prev = u;
  }

  inline void erase(const int u) {
    dat[dat[u].prev].next = dat[u].next, dat[dat[u].next].prev = dat[u].prev;
  }

  inline void clear() {
    for (int i = N; i < N + H; ++i) dat[i].prev = dat[i].next = i;
  }
};

template <typename flow_t>
struct PushRelabel {
  struct edge {
    int to;
    flow_t cap;
    int rev;
    bool isrev;
    int idx;
  };

  int V, height, relabels;
  std::vector<flow_t> ex;
  std::vector<int> potential, cur_edge;
  List all_ver;
  Stack act_ver;
  std::vector<std::vector<edge> > graph;

  PushRelabel(int V)
      : V(V),
        height(-1),
        relabels(0),
        ex(V, flow_t(0)),
        potential(V, 0),
        cur_edge(V),
        all_ver(V, V),
        act_ver(V, V),
        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});
  }

  int calc_active(int t) {
    height = -1;
    for (int i = 0; i < V; i++) {
      if (potential[i] < V) {
        cur_edge[i] = 0;
        height = std::max(height, potential[i]);
        all_ver.insert(potential[i], i);
        if (ex[i] > 0 && i != t) act_ver.push(potential[i], i);
      } else {
        potential[i] = V + 1;
      }
    }
    return height;
  }

  void bfs(int t) {
    for (int i = 0; i < V; i++) {
      potential[i] = std::max(potential[i], V);
    }
    potential[t] = 0;
    std::queue<int> que;
    que.emplace(t);
    while (!que.empty()) {
      int p = que.front();
      que.pop();
      for (auto& e : graph[p]) {
        if (potential[e.to] == V && graph[e.to][e.rev].cap > 0) {
          potential[e.to] = potential[p] + 1;
          que.emplace(e.to);
        }
      }
    }
  }

  int init(int s, int t) {
    potential[s] = V + 1;
    bfs(t);
    for (auto& e : graph[s]) {
      if (potential[e.to] < V) {
        graph[e.to][e.rev].cap = e.cap;
        ex[s] -= e.cap;
        ex[e.to] += e.cap;
      }
      e.cap = 0;
    }
    return calc_active(t);
  }

  bool push(int u, int t, edge& e) {
    flow_t f = std::min(e.cap, ex[u]);
    int v = e.to;
    e.cap -= f, ex[u] -= f;
    graph[v][e.rev].cap += f, ex[v] += f;
    if (ex[v] == f && v != t) act_ver.push(potential[v], v);
    return ex[u] == 0;
  }

  int discharge(int u, int t) {
    for (int& i = cur_edge[u]; i < (int)graph[u].size(); i++) {
      auto& e = graph[u][i];
      if (potential[u] == potential[e.to] + 1 && e.cap > 0) {
        if (push(u, t, e)) return potential[u];
      }
    }
    return relabel(u);
  }

  int global_relabel(int t) {
    bfs(t);
    all_ver.clear(), act_ver.clear();
    return calc_active(t);
  }

  void gap_relabel(const int u) {
    for (int i = potential[u]; i <= height; ++i) {
      for (int id = all_ver.dat[V + i].next; id < V;
           id = all_ver.dat[id].next) {
        potential[id] = V + 1;
      }
      all_ver.dat[V + i].prev = all_ver.dat[V + i].next = V + i;
    }
  }

  int relabel(const int u) {
    ++relabels;
    int prv = potential[u], cur = V;
    for (int i = 0; i < (int)graph[u].size(); ++i) {
      const edge& e = graph[u][i];
      if (cur > potential[e.to] + 1 && e.cap > 0) {
        cur_edge[u] = i;
        cur = potential[e.to] + 1;
      }
    }
    if (all_ver.more_one(prv)) {
      all_ver.erase(u);
      if ((potential[u] = cur) == V) return potential[u] = V + 1, prv;
      act_ver.push(cur, u);
      all_ver.insert(cur, u);
      height = std::max(height, cur);
    } else {
      gap_relabel(u);
      return height = prv - 1;
    }
    return cur;
  }

  flow_t max_flow(int s, int t) {
    int level = init(s, t);
    while (level >= 0) {
      if (act_ver.empty(level)) {
        --level;
        continue;
      }
      int u = act_ver.top(level);
      act_ver.pop(level);
      level = discharge(u, t);
      if (relabels * 2 >= V) {
        level = global_relabel(t);
        relabels = 0;
      }
    }
    return ex[t];
  }
};


int main() {
  int N, M, S, T;
  cin >> N >> M >> S >> T;
  PushRelabel< int64 > g(N);
  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    --a, --b;
    g.add_edge(a, b, c);
  }
  cout << g.max_flow(S - 1, T - 1) << endl;
}
0