Problem p02376 (C++) — buggy submission, verdict: Runtime Error

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Flow = int;
struct FlowEdge {
  int src, dst;
  Flow cap;
  int rev;
  FlowEdge(const int s = 0, const int d = 0, const Flow c = 0, const int r = 0)
      : src(s), dst(d), cap(c), rev(r) {}
};

using FlowEdges = std::vector<FlowEdge>;

class FlowGraph {
  std::vector<FlowEdges> g;

public:
  FlowGraph(const int size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  FlowEdges &operator[](const int i) & { return g[i]; }

  void addEdge(const int src, const int dst, const Flow c = 1) {
    g[src].emplace_back(src, dst, c, g[dst].size());
    g[dst].emplace_back(dst, src, 0, g[src].size() - 1);
  }
};

template <Flow inf = std::numeric_limits<Flow>::max()>
Flow dinic(FlowGraph g, const int source, const int sink) {
  std::vector<int> level(g.size()), iter(g.size());
  auto bfs = [&] {
    std::fill(level.begin(), level.end(), -1);
    std::queue<int> q;
    level[source] = 0;
    q.push(source);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (auto &e : g[v]) {
        if (e.cap <= 0 || level[e.dst] >= 0)
          continue;
        level[e.dst] = level[v] + 1;
        q.push(e.dst);
      }
    }
    return level[sink] != -1;
  };
  std::function<Flow(int, Flow)> dfs = [&](int v, Flow f) -> Flow {
    if (v == sink)
      return f;
    Flow s = 0;
    for (; iter[v] < g[v].size(); iter[v]++) {
      auto &e = g[v][iter[v]];
      if (level[v] >= level[e.dst] || e.cap <= 0)
        continue;
      Flow d = dfs(e.dst, min(f, e.cap));
      e.cap -= d;
      g[e.dst][e.rev].cap += d;
      s += d;
      f -= d;
      if (f == 0)
        break;
    }
    return s;
  };
  Flow s = 0;
  while (bfs()) {
    std::fill(iter.begin(), iter.end(), 0);
    s += dfs(source, inf);
  }
  return s;
}

main {
  int n, m;
  cin >> n >> m;
  FlowGraph g(n);
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    g.addEdge(a, b, c);
  }
  cout << dinic(g, 0, n - 1) << endl;
}

Error: -11

Fixed submission:

// clang-format off
#include <bits/stdc++.h>
#define int long long
#define main signed main()
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#define rep(i, n) loop(i, 0, n)
#define forever for (;;)
#define all(v) (v).begin(), (v).end()
#define rall(v) (v).rbegin(), (v).rend()
#define prec(n) fixed << setprecision(n)
template<typename A> using V = std::vector<A>;
template<typename A> using F = std::function<A>;
template<typename A, typename B> using P = std::pair<A, B>;
using pii = P<int, int>;
using vi = V<int>;
using vd = V<double>;
using vs = V<std::string>;
using vpii = V<pii>;
using vvi = V<vi>;
using vvpii = V<vpii>;
constexpr int INF = sizeof(int) == sizeof(long long) ? 1000000000000000000LL : 1000000000;
constexpr int MOD = 1000000007;
constexpr double PI = 3.14159265358979;
template<typename A, typename B> bool cmin(A &a, const B &b) { return a > b ? (a = b, true) : false; }
template<typename A, typename B> bool cmax(A &a, const B &b) { return a < b ? (a = b, true) : false; }
constexpr bool odd(const int n) { return n & 1; }
constexpr bool even(const int n) { return ~n & 1; }
template<typename T> std::istream &operator>>(std::istream &is, std::vector<T> &v) { for (T &x : v) is >> x; return is; }
template<typename A, typename B> std::istream &operator>>(std::istream &is, std::pair<A, B> &p) { is >> p.first; is >> p.second; return is; }
using namespace std;
// clang-format on

using Flow = int;
struct FlowEdge {
  int src, dst;
  Flow cap;
  int rev;
  FlowEdge(const int s = 0, const int d = 0, const Flow c = 0, const int r = 0)
      : src(s), dst(d), cap(c), rev(r) {}
};

using FlowEdges = std::vector<FlowEdge>;

class FlowGraph {
  std::vector<FlowEdges> g;

public:
  FlowGraph(const int size = 0) : g(size) {}
  size_t size() const { return g.size(); }
  FlowEdges &operator[](const int i) & { return g[i]; }

  void addEdge(const int src, const int dst, const Flow c = 1) {
    g[src].emplace_back(src, dst, c, g[dst].size());
    g[dst].emplace_back(dst, src, 0, g[src].size() - 1);
  }
};

template <Flow inf = std::numeric_limits<Flow>::max()>
Flow dinic(FlowGraph g, const int source, const int sink) {
  std::vector<int> level(g.size()), iter(g.size());
  auto bfs = [&] {
    std::fill(level.begin(), level.end(), -1);
    std::queue<int> q;
    level[source] = 0;
    q.push(source);
    while (q.size()) {
      int v = q.front();
      q.pop();
      for (auto &e : g[v]) {
        if (e.cap <= 0 || level[e.dst] >= 0)
          continue;
        level[e.dst] = level[v] + 1;
        q.push(e.dst);
      }
    }
    return level[sink] != -1;
  };
  std::function<Flow(int, Flow)> dfs = [&](int v, Flow f) -> Flow {
    if (v == sink)
      return f;
    Flow s = 0;
    for (; iter[v] < g.size(); iter[v]++) {
      auto &e = g[v][iter[v]];
      if (level[v] >= level[e.dst] || e.cap <= 0)
        continue;
      Flow d = dfs(e.dst, min(f, e.cap));
      e.cap -= d;
      g[e.dst][e.rev].cap += d;
      s += d;
      f -= d;
      if (f == 0)
        break;
    }
    return s;
  };
  Flow s = 0;
  while (bfs()) {
    std::fill(iter.begin(), iter.end(), 0);
    s += dfs(source, inf);
  }
  return s;
}

main {
  int n, m;
  cin >> n >> m;
  FlowGraph g(n);
  while (m--) {
    int a, b, c;
    cin >> a >> b >> c;
    g.addEdge(a, b, c);
  }
  cout << dinic(g, 0, n - 1) << endl;
}