結果

問題 No.3668 Minimum Cut
コンテスト
ユーザー TKTYI
提出日時 2026-08-24 18:16:34
言語 C++23
(gcc 15.3.0 + boost 1.92.0 + ACL)
コンパイル:
g++-15 -O2 -lm -std=c++23 -Wuninitialized -DONLINE_JUDGE -o a.out _filename_
実行:
./a.out
結果
AC  
実行時間 537 ms / 2,000 ms
+ 23µs
コード長 4,826 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 4,508 ms
コンパイル使用メモリ 382,340 KB
実行使用メモリ 6,272 KB
最終ジャッジ日時 2026-09-04 22:36:55
合計ジャッジ時間 8,376 ms
ジャッジサーバーID
(参考情報)
judge1_1 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 39
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

#include<bits/stdc++.h>
#include<atcoder/all>
using namespace std;
using namespace atcoder;
typedef long long int ll;
typedef long double ld;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<vl> vvl;
typedef vector<vvl> vvvl;
typedef vector<vvvl> vvvvl;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vvb> vvvb;
typedef vector<vvvb> vvvvb;
typedef pair<ll,ll> pl;
typedef pair<ll,pl> ppl;
typedef pair<ll,ppl> pppl;
typedef pair<ll,pppl> pppppl;
#define rep(i,a,b) for(int i=(a);i<(b);i++)
#define rrep(i,a,b) for(int i=(b)-1;i>=(a);i--)
#define all(a) begin(a),end(a)
#define sz(a) (int)(a).size()
#define F first
#define S second
#define bs(A,x) binary_search(all(A),x)
#define lb(A,x) (ll)(lower_bound(all(A),x)-A.begin())
#define ub(A,x) (ll)(upper_bound(all(A),x)-A.begin())
#define cou(A,x) (ll)(upper_bound(all(A),x)-lower_bound(all(A),x))
template<typename T>using min_priority_queue=priority_queue<T,vector<T>,greater<T>>;
template<class T>bool chmax(T&a,T b){if(a<b){a=b;return 1;}return 0;}
template<class T>bool chmin(T&a,T b){if(b<a){a=b;return 1;}return 0;}
//*
using mint=modint998244353;
const ll mod=998244353;
//*/
/*
using mint=modint1000000007;
const ll mod=1000000007;
//*/
//using mint=modint;
//*
typedef vector<mint> vm;
typedef vector<vm> vvm;
typedef vector<vvm> vvvm;
typedef vector<vvvm> vvvvm;
ostream&operator<<(ostream&os,mint a){os<<a.val();return os;}
istream&operator>>(istream&is,mint&a){int x;is>>x;a=mint(x);return is;}
//*/
template<typename T1,typename T2>ostream&operator<<(ostream&os,pair<T1,T2>p){os<<p.F<<" "<<p.S;return os;}
template<typename T1,typename T2>istream&operator>>(istream&is,pair<T1,T2>&p){is>>p.F>>p.S;return is;}
template<typename T>ostream&operator<<(ostream&os,vector<T>v){rep(i,0,sz(v))os<<v[i]<<(i+1!=sz(v)?" ":"");return os;}
template<typename T>istream&operator>>(istream&is,vector<T>&v){for(T&in:v)is>>in;return is;}

// https://ei1333.github.io/library/graph/flow/dinic-capacity-scaling.hpp
/**
 * @brief Dinic Capacity Scaling(最大流)
 *
 */
template <typename flow_t>
struct DinicCapacityScaling {
  static_assert(std::is_integral<flow_t>::value,
                "template parameter flow_t must be integral type");

  const flow_t INF;

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

  std::vector<std::vector<edge> > graph;
  std::vector<int> min_cost, iter;
  flow_t max_cap;

  explicit DinicCapacityScaling(int V)
      : INF(std::numeric_limits<flow_t>::max()), graph(V), max_cap(0) {}

  void add_edge(int from, int to, flow_t cap, int idx = -1) {
    max_cap = std::max(max_cap, cap);
    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, const flow_t& base) {
    min_cost.assign(graph.size(), -1);
    std::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 >= base && 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_augment_path(int idx, const int t, flow_t base, flow_t flow) {
    if (idx == t) return flow;
    flow_t sum = 0;
    for (int& i = iter[idx]; i < (int)graph[idx].size(); i++) {
      edge& e = graph[idx][i];
      if (e.cap >= base && min_cost[idx] < min_cost[e.to]) {
        flow_t d =
            find_augment_path(e.to, t, base, std::min(flow - sum, e.cap));
        if (d > 0) {
          e.cap -= d;
          graph[e.to][e.rev].cap += d;
          sum += d;
          if (flow - sum < base) break;
        }
      }
    }
    return sum;
  }

  flow_t max_flow(int s, int t) {
    if (max_cap == flow_t(0)) return flow_t(0);
    flow_t flow = 0;
    for (int i = 63 - __builtin_clzll(max_cap); i >= 0; i--) {
      flow_t now = flow_t(1) << i;
      while (build_augment_path(s, t, now)) {
        iter.assign(graph.size(), 0);
        flow += find_augment_path(s, t, now, INF);
      }
    }
    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];
        std::cout << i << "->" << e.to << " (flow: " << rev_e.cap << "/"
                  << e.cap + rev_e.cap << ")" << std::endl;
      }
    }
  }
};

int main(){
  cin.tie(0)->sync_with_stdio(0);
  cin.exceptions(cin.failbit);
  ll N,M,S,T;cin>>N>>M>>S>>T;S--;T--;
  DinicCapacityScaling<ll>G(N);
  rep(i,0,M){
  	ll u,v,c;cin>>u>>v>>c;u--;v--;
  	G.add_edge(u,v,c);
  }
  cout<<G.max_flow(S,T)<<endl;
  return 0;
}
0