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

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define range(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) range(i, 0, b)

typedef int Capacity;
struct Edge {
  int src, dst;
  Capacity cap;
  Edge(int src_, int dst_, int cap_) : src(src_), dst(dst_), cap(cap_) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<int> Array;
typedef vector<Array> Matrix;

int const inf = 1e9;

template <class Capacity = int> struct FordFulkerson {
  typedef vector<vector<Capacity>> Matrix;
  size_t n;
  Matrix cap, flow;
  vector<bool> vis;
  Capacity res;
  vector<vector<int>> g;
  FordFulkerson(const int s, const int t, const Graph &graph) {
    n = graph.size();
    cap.assign(n, Array(n));
    flow.assign(n, Array(n));
    g.assign(n, {});
    res = 0;
    rep(i, n) rep(j, graph[i].size()) {
      int u = graph[i][j].src, v = graph[i][j].dst;
      Capacity c = graph[i][j].cap;
      cap[u][v] += c;
      cap[v][u] += c;
      flow[v][u] += c;
      // cap[u][v] = cap[v][u] = graph[i][j].cap;
      // flow[v][u] = graph[i][j].cap;
    }
    rep(i, n) rep(j, n) if (cap[i][j]) g[i].push_back(j), g[j].push_back(i);
    Capacity f = -1;
    while (f)
      vis.assign(n, false), res += (f = augment(s, t, inf));
  }
  Capacity augment(const int v, const int t, Capacity lim) {
    vis[v] = true;
    if (v == t)
      return lim;
    for (const int &i : g[v]) {
      if (vis[i] || flow[v][i] == cap[v][i])
        continue;
      Capacity f = augment(i, t, min(lim, cap[v][i] - flow[v][i]));
      flow[v][i] += f;
      flow[i][v] -= f;
      // cout << flow[v][i] << " " << flow[i][v] << endl;
      assert(0 <= flow[v][i]);
      assert(0 <= flow[i][v]);
      if (f)
        return f;
    }
    return 0;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  Graph g(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(a, b, c);
  }
  cout << FordFulkerson<>(0, N - 1, g).res << endl;
}

Error: 0

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS
// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define range(i, a, b) for (int i = a; i < (int)(b); i++)
#define rep(i, b) range(i, 0, b)

typedef int Capacity;
struct Edge {
  int src, dst;
  Capacity cap;
  Edge(int src_, int dst_, int cap_) : src(src_), dst(dst_), cap(cap_) {}
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<int> Array;
typedef vector<Array> Matrix;

int const inf = 1e9;

template <class Capacity = int> struct FordFulkerson {
  typedef vector<vector<Capacity>> Matrix;
  size_t n;
  Matrix cap, flow;
  vector<bool> vis;
  Capacity res;
  vector<vector<int>> g;
  FordFulkerson(const int s, const int t, const Graph &graph) {
    n = graph.size();
    cap.assign(n, Array(n));
    flow.assign(n, Array(n));
    g.assign(n, {});
    res = 0;
    rep(i, n) rep(j, graph[i].size()) {
      int u = graph[i][j].src, v = graph[i][j].dst;
      cap[u][v] = graph[i][j].cap;
      flow[v][u] = graph[i][j].cap;
    }
    rep(i, n) rep(j, n) if (cap[i][j]) g[i].push_back(j), g[j].push_back(i);
    Capacity f = -1;
    while (f)
      vis.assign(n, false), res += (f = augment(s, t, inf));
  }
  Capacity augment(const int v, const int t, Capacity lim) {
    vis[v] = true;
    if (v == t)
      return lim;
    for (const int &i : g[v]) {
      if (vis[i] || flow[v][i] == cap[v][i])
        continue;
      Capacity f = augment(i, t, min(lim, cap[v][i] - flow[v][i]));
      flow[v][i] += f;
      flow[i][v] -= f;
      // cout << flow[v][i] << " " << flow[i][v] << endl;
      assert(0 <= flow[v][i]);
      assert(0 <= flow[i][v]);
      if (f)
        return f;
    }
    return 0;
  }
};

int main() {
  int N, M;
  cin >> N >> M;
  Graph g(N);
  rep(i, M) {
    int a, b, c;
    cin >> a >> b >> c;
    g[a].emplace_back(a, b, c);
  }
  cout << FordFulkerson<>(0, N - 1, g).res << endl;
}