結果

問題 No.3668 Minimum Cut
コンテスト
ユーザー miie
提出日時 2026-09-05 00:23:57
言語 C++23
(gcc 15.3.0 + boost 1.92.0)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 33 ms / 2,000 ms
+ 457µs
コード長 11,502 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 2,501 ms
コンパイル使用メモリ 366,576 KB
実行使用メモリ 9,764 KB
最終ジャッジ日時 2026-09-05 00:24:03
合計ジャッジ時間 5,170 ms
ジャッジサーバーID
(参考情報)
judge2_0 / judge1_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx2")

// 数値型
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using P = pair<int,int>;
using Pll = pair<ll, ll>;
using Pli = pair<ll, int>;
using Pil = pair<int, ll>;

// vector関連
using vi = vector<int>;
using vvi = vector<vi>;
using vvvi = vector<vvi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vvvll = vector<vvll>;
template<typename T>
using vc = vector<T>;
template<typename T>
using vvc = vector<vc<T>>;
template<typename T>
using vvvc = vector<vvc<T>>;
template<typename T>
using vvvvc = vector<vvvc<T>>;

// priority_queue
template<typename T>
using pq = priority_queue<T>;
template<typename T>
using pqg = priority_queue<T, vc<T>, greater<T>>;

#define rep(i, n) for(int i = 0; i < (int)(n); i++)
#define FOR(i, a, b) for(int i = a; i < (int)(b); i++)
#define all(a) (a).begin(),(a).end()
#define rall(a) (a).rbegin(),(a).rend()
#define MIN(vec) *min_element(vec)
#define MAX(vec) *max_element(vec)
#define next_perm(vec) next_permutation((vec).begin(), (vec).end())
#define UNIQUE(vec) vec.erase(unique(vec.begin(), vec.end()), vec.end())
#define el "\n"
#define Yes cout << "Yes" << el
#define No cout << "No" << el
#define YES cout << "YES" << el
#define NO cout << "NO" << el
#define EPS 1e-8
#define Equal(a, b) (fabs((a)-(b)) < EPS) 
#define dbg(x) cerr << #x << "=" << x << el 

// 定数
const string abc = "abcdefghijklmnopqrstuvwxyz";
const string ABC = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
constexpr int INF = 1001001001;
constexpr ll LINF = 1001001001001001001ll;
constexpr int DX[] = {1, 0, -1, 0};
constexpr int DY[] = {0, 1, 0, -1};
constexpr int DX8[] = {1, 0, -1, 0, 1, 1, -1, -1};
constexpr int DY8[] = {0, 1, 0, -1, 1, -1, 1, -1};

template<typename T1, typename T2>
ostream &operator<< (ostream &os, pair<T1, T2> p) {
    os << "{" << p.first << "," << p.second << "}";
    return os;
}
template<typename T>
ostream &operator<< (ostream &os, vc<T> &vec) {
    int sz = vec.size();
    rep(i, sz){
        os << vec[i] << (i==sz-1?"":" ");
    }
    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>
istream &operator>> (istream &is, vc<T> &vec) {
    int sz = vec.size();
    rep(i, sz) { is >> vec[i]; }
    return is;
}
/// @brief aとbの最大値をaに格納。更新があったかbool値を返す
/// @tparam T1 
/// @tparam T2 
/// @param a 
/// @param b 
/// @return bool
template<typename T1, typename T2>
inline bool chmax(T1 &a, T2 b){
    bool ret = a<b;
    if(ret) a = b;
    return ret;
}

/// @brief aとbの最小値をaに格納。更新があったかbool値を返す
/// @tparam T1 
/// @tparam T2 
/// @param a 
/// @param b 
/// @return bool
template<typename T1, typename T2>
inline bool chmin(T1 &a, T2 b){
    bool ret = a>b;
    if(ret) {a = b;}
    return ret;
}

inline void YesNo(bool flag){
    if(flag) {Yes;}
    else {No;}
    return;
}

inline void YESNO(bool flag){
    if(flag) {YES;}
    else {NO;}
    return;
}

inline bool outof(ll x, ll xlim){
    return (x<0 || x>=xlim);
}

template<typename T>
inline T sqnorm(T x, T y){
    return x*x+y*y;
}

/// @brief char->int
/// @param c 
/// @return int
inline int ctoi(char c){
    return c-'0';
}

/// @brief xを素因数分解
/// @param x 
/// @return vector<Pli>, 素因数の昇順に {p, cnt}
vector<Pli> prime_fact(ll x){
    vector<Pli> ret;
    for(ll i=2; i*i<=x; i++){
        if(x%i == 0){
            ret.emplace_back(i, 0);
            while(x%i == 0){
                ret.back().second++;
                x /= i;
            }
        }
    }
    if(x != 1) ret.emplace_back(x, 1);
    return ret;
}

/// @brief xの約数列挙
/// @param x 
/// @return vll, 約数の昇順
vll divisor_enum(ll x){
    vector<ll> ret;
    for(ll i=1; i*i<=x; i++){
        if(x%i == 0){
            ret.push_back(x/i);
            ret.push_back(i);
        }
    }
    sort(all(ret));
    UNIQUE(ret);
    return ret;
}

/// @brief 繰り返し二乗法。
/// @tparam T 
/// @param x 
/// @param k 
/// @param op 
/// @param e 
/// @return 
template<typename T>
T pow_t(T x, ll k, T (*op)(T, T), T (*e)()){
    T ret = e();
    while(k){
        if(k&1) ret = op(ret, x);
        x = op(x, x);
        k >>= 1;
    }
    return ret;
}

ll powll(ll x, ll k){
    return pow_t<ll>(x, k, [](ll a, ll b) -> ll{return a*b;}, []() -> ll{return 1;});
}

inline int pop_cnt(ll x) { return __builtin_popcountll(x); }
inline int top_bit(ll x) { return (x==0?-1:63-__builtin_clzll(x));}

void main2();

int main(){
    ios::sync_with_stdio(false);
    std::cin.tie(nullptr);
    main2();
}
// template< typename flow_t >
// struct PushRelabel {
//   const flow_t INF;

//   struct edge {
//     int to;
//     flow_t cap;
//     int rev;
//     bool isrev;
//     int idx;
//   };
//   vector< vector< edge > > graph;
//   vector< flow_t > ex;
//   int relabels, high;
//   vector< int > cnt, h;
//   vector< vector< int > > hs;

//   PushRelabel(int V) : graph(V), INF(numeric_limits< flow_t >::max()), hs(V + 1), high(0) {
//     cout << INF << endl;
//   }


//   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});
//   }

//   void update_height(int idx, int nxt_height) {
//     ++relabels;
//     if(h[idx] != graph.size() + 1) {
//       --cnt[h[idx]];
//     }
//     h[idx] = nxt_height;
//     if(h[idx] != graph.size() + 1) {
//       high = nxt_height;
//       ++cnt[nxt_height];
//       if(ex[idx] > 0) hs[nxt_height].emplace_back(idx);
//     }
//   }

//   void global_relabel(int idx) {
//     for(int i = 0; i <= high; i++) hs[i].clear();
//     relabels = 0;
//     high = 0;
//     h.assign(graph.size(), graph.size() + 1);
//     cnt.assign(graph.size(), 0);
//     queue< int > que;
//     que.emplace(idx);
//     h[idx] = 0;
//     while(que.size()) {
//       int p = que.front();
//       que.pop();
//       for(auto &e : graph[p]) {
//         if(h[e.to] == graph.size() + 1 && graph[e.to][e.rev].cap > 0) {
//           que.emplace(e.to);
//           high = h[p] + 1;
//           update_height(e.to, high);
//         }
//       }
//     }
//   }


//   void push(int idx, edge &e) {
//     if(h[e.to] == graph.size() + 1) return;
//     if(ex[e.to] == 0) {
//       hs[h[e.to]].emplace_back(e.to);
//     }
//     flow_t df = min(ex[idx], e.cap);
//     e.cap -= df;
//     graph[e.to][e.rev].cap += df;
//     ex[idx] -= df;
//     ex[e.to] += df;
//   }

//   void discharge(int idx) {
//     int next_height = (int) graph.size() + 1;
//     for(auto &&e : graph[idx]) {
//       if(e.cap > 0) {
//         if(h[idx] == h[e.to] + 1) {
//           push(idx, e);
//           if(ex[idx] <= 0) return;
//         } else {
//           next_height = min(next_height, h[e.to] + 1);
//         }
//       }
//     }
//     if(cnt[h[idx]] > 1) {
//       update_height(idx, next_height);
//     } else {
//       for(; high >= h[idx]; hs[high--].clear()) {
//         for(int j : hs[high]) update_height(j, graph.size() + 1);
//       }
//     }
//   }

//   flow_t max_flow(int s, int t) {
//     ex.assign(graph.size(), 0);
//     ex[s] = INF;
//     ex[t] = -INF;
//     global_relabel(t);
//     for(auto &e : graph[s]) push(s, e);
//     for(; high >= 0; high--) {
//       while(!hs[high].empty()) {
//         int idx = hs[high].back();
//         hs[high].pop_back();
//         discharge(idx);
//         if(relabels >= graph.size() * 4) global_relabel(t);
//       }
//     }
//     return ex[t] + INF;
//   }

//   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;
//       }
//     }
//   }
// };
// // https://ei1333.github.io/luzhiled/snippets/graph/push-relabel.html

#define N 2005

class PushRelabel {
  const ll inf = 1e18;
  int n;
  struct Edge {
    int to;
    ll cap;
    int rev;
  };
  vector<Edge> g[N];

  int qs[N];
  // height, distance label, excess flow
  int hs[N], ds[N];
  ll fs[N];
  // active node
  bool active[N];
  // bucket
  vector<int> bs[N];
  int cur;

public:
  PushRelabel() {}
  PushRelabel(int n) { init(n); }
  inline void init(int n) { this->n = n; }

  inline void add_edge(int fr, int to, ll cap) {
    g[fr].emplace_back(Edge{to, cap, (int) g[to].size()});
    g[to].emplace_back(Edge{fr, 0, (int) g[fr].size()-1});
  }

  // Global labeling
  inline int bfs(int t) {
    int a = 0, b = 1;
    for(int i=0; i<n; ++i) hs[i] = n, ds[i] = 0, bs[i].clear();
    qs[0] = t;
    hs[t] = 0; ds[0] = 1;
    cur = 0;
    int d = 0;
    while(a < b) {
      int v = qs[a++];
      d = hs[v];
      for(Edge &e : g[v]) {
        int w = e.to;
        if(hs[w] <= d + 1 || g[w][e.rev].cap == 0) continue;
        hs[w] = d + 1;
        if(active[w] && d+1 < n) {
          bs[d+1].push_back(w);
          cur = d+1;
        }
        if(d+1 < n) ++ds[d+1];
        qs[b++] = w;
      }
    }
    return d+1;
  }

  inline ll flow(int s, int t) {
    for(int i=0; i<n; ++i) fs[i] = 0, active[i] = false;

    fs[s] = inf;
    active[s] = true;

    // initialize hs, bs, ds
    int gap = bfs(t);
    bs[cur].push_back(s);

    int cnt = 0;
    while(1) {
      while(cur >= 0 && bs[cur].size() == 0) --cur;
      if(cur < 0) break;

      int v = bs[cur].back(); bs[cur].pop_back();
      if(v == t) continue;

      int hv = hs[v];
      // Gap-relabeling
      if(hv > gap) {
        if(hv < n) --ds[hv];
        hs[v] = n;
        continue;
      }


      // push
      ll rest = fs[v];
      for(Edge &e : g[v]) {
        int w = e.to;
        if(e.cap > 0 && hv > hs[w] && hs[w] < gap) {
          ll d = min(rest, e.cap);
          e.cap -= d;
          g[w][e.rev].cap += d;
          rest -= d;
          fs[w] += d;
          if(!active[w]) {
            int hw = hs[w];
            bs[hw].push_back(w);
            if(cur < hw) cur = hw;
            active[w] = true;
          }
          if(rest == 0) break;
        }
      }
      fs[v] = rest;

      if(rest == 0) {
        active[v] = false;
        continue;
      }

      // relabel
      int h0 = hs[v];
      hv = n;
      for(Edge &e : g[v]) {
        int w = e.to;
        if(e.cap > 0 && hv > hs[w] + 1 && hs[w] < gap) {
          hv = hs[w] + 1;
        }
      }
      if(h0 != hv) {
        --ds[h0];
        if(ds[h0] == 0 && h0 < gap) {
          gap = h0;
          hv = n;
        } else if(hv == gap) {
          ++gap;
        }
        if(hv < n) ++ds[hv];
      }

      hs[v] = hv;
      if(hv < n) {
        bs[hv].push_back(v);
        if(cur < hv) cur = hv;
      } else {
        active[v] = false;
      }

      if((++cnt) % n == 0) {
        gap = bfs(t);
      }
    }
    return fs[t];
  }
};
// https://tjkendev.github.io/procon-library/cpp/max_flow/push-relabel-highest.html

void main2(){
    int n, m, s, t;
    cin >> n >> m >> s >> t;
    PushRelabel mf(n);
    rep(i, m) {
        int u, v, c;
        cin >> u >> v >> c;
        mf.add_edge(u-1, v-1, c);
    }
    cout << mf.flow(s-1, t-1) << el;
}
0