萌新ISAP不过样例求调

P3376 【模板】网络最大流

Lightwhite @ 2022-06-10 15:47:22

#include <iostream>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;
using ll = long long;
const ll kN = 1e4 + 5, kI = 1e18;

struct Edge {
  ll v, nxt, cap;
} ed[kN << 7];
ll n, m, s, t, d[kN], hd[kN], nhd[kN], gap[kN], tot = 1, ans;
void add (ll u, ll v, ll c) {
  ed[++tot] = {v, hd[u], c}, hd[u] = tot;
  ed[++tot] = {u, hd[v], 0}, hd[v] = tot;
}
queue <ll> q;
void BFS () {
  fill (d, d + n + 1, -1), d[t] = 0, gap[d[t]]++, q.push (t);
  while (!q.empty ()) {
    ll u = q.front (); q.pop ();
    for (ll i = hd[u], v; i; i = ed[i].nxt) {
      v = ed[i].v;
      if (d[v] == -1) {
        d[v] = d[u] + 1, gap[d[v]]++, q.push (v);
      }
    }
  }
}
ll DFS (ll u, ll f) {
  if (u == t) {
    return f;
  }
  ll use = 0;
  for (ll i = nhd[u], v; i; i = ed[i].nxt) {
    nhd[u] = i, v = ed[i].v;
      cerr << d[v] << ' ' << d[u] + 1 << '\n';
    if (ed[i].cap && d[v] == d[u] + 1) {
      ll r = DFS (u, min (ed[i].cap, f - use));
      if (r) {
        ed[i].cap -= r, ed[i ^ 1].cap += r, use += r;
        if (use == f) {
          return f;
        }
      }
    }
  }
  gap[d[u]]--;
  !gap[d[u]] ? d[s] = n + 1 : 0;
  d[u]++, gap[d[u]]++;
  return use;
}
int main () {
  ios :: sync_with_stdio (false);
  cin.tie (0), cout.tie (0);

  cin >> n >> m >> s >> t;
  for (int i = 1, u, v, c; i <= m; i++) {
    cin >> u >> v >> c, add (u, v, c);
  }
  for (BFS (); d[s] <= n; memcpy (nhd, hd, sizeof (hd)), ans += DFS (s, kI)) {
  }
  cout << ans << '\n';
  return 0;
}


by Lightwhite @ 2022-06-10 16:12:02

@After_glow 卧槽谢谢大佬!


by Aftglw @ 2022-06-10 16:13:47

@蒋金洲 /qd/qd/qd


by Phartial @ 2022-06-10 18:24:33

巨巨洲/bx/bx/bx


上一页 |