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

#include <algorithm>
#include <climits>
#include <cstdio>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 99999999
using namespace std;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])
Weight augment(const Graph &g, const Matrix &capacity, Matrix &flow,
               const vector<int> &level, vector<bool> &finished, int u, int t,
               Weight cur) {
  if (u == t || cur == 0)
    return cur;
  if (finished[u])
    return 0;
  finished[u] = true;
  FOR(e, g[u]) if (level[e->dst] > level[u]) {
    Weight f = augment(g, capacity, flow, level, finished, e->dst, t,
                       min(cur, RESIDUE(u, e->dst)));
    if (f > 0) {
      flow[u][e->dst] += f;
      flow[e->dst][u] -= f;
      finished[u] = false;
      return f;
    }
  }
  return 0;
}
Weight maximumFlow(const Graph &g, int s, int t) {
  int n = g.size();
  Matrix flow(n, Array(n)), capacity(n, Array(n)); // adj. matrix
  REP(u, n) FOR(e, g[u]) capacity[e->src][e->dst] += e->weight;

  Weight total = 0;
  for (bool cont = true; cont;) {
    cont = false;
    vector<int> level(n, -1);
    level[s] = 0; // make layered network
    queue<int> Q;
    Q.push(s);
    for (int d = n; !Q.empty() && level[Q.front()] < d;) {
      int u = Q.front();
      Q.pop();
      if (u == t)
        d = level[u];
      FOR(e, g[u])
      if (RESIDUE(u, e->dst) > 0 && level[e->dst] == -1) Q.push(e->dst),
          level[e->dst] = level[u] + 1;
    }
    vector<bool> finished(n); // make blocking flows
    for (Weight f = 1; f > 0;) {
      f = augment(g, capacity, flow, level, finished, s, t, INF);
      if (f == 0)
        break;
      total += f;
      cont = true;
    }
  }
  return total;
}

int main() {
  int i, V, E, s, t, e;
  // for(scanf("%d",&T);T;putchar(--T?' ':'\\n')){
  scanf("%d%d", &V, &E);
  Graph g(V);
  for (; E--;)
    scanf("%d%d%d", &s, &t, &e), g[s].push_back(Edge(s, t, e)),
        g[t].push_back(Edge(t, s, 0));
  printf("%d\\n", maximumFlow(g, 0, V - 1));
  //}
}

Error: -11

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>
#include <vector>
#define REP(i, n) for (int i = 0; i < (int)n; ++i)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ALL(c) (c).begin(), (c).end()
#define INF 99999999
using namespace std;

typedef int Weight;
struct Edge {
  int src, dst;
  Weight weight;
  Edge(int src, int dst, Weight weight) : src(src), dst(dst), weight(weight) {}
};
bool operator<(const Edge &e, const Edge &f) {
  return e.weight != f.weight ? e.weight > f.weight : // !!INVERSE!!
             e.src != f.src ? e.src < f.src
                            : e.dst < f.dst;
}
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;
typedef vector<Weight> Array;
typedef vector<Array> Matrix;

#define RESIDUE(s, t) (capacity[s][t] - flow[s][t])

#define GLOBAL_RELABELING()                                                    \\
  {                                                                            \\
    queue<int> Q;                                                              \\
    Q.push(t);                                                                 \\
    fill(ALL(d), INF);                                                         \\
    d[t] = 0;                                                                  \\
    while (!Q.empty()) {                                                       \\
      int u = Q.front();                                                       \\
      Q.pop();                                                                 \\
      FOR(e, g[u])                                                             \\
      if (RESIDUE(e->dst, u) > 0 && d[u] + 1 < d[e->dst]) Q.push(e->dst),      \\
          d[e->dst] = d[u] + 1;                                                \\
    }                                                                          \\
  }
#define PUSH(u, v)                                                             \\
  {                                                                            \\
    Weight delta = min(excess[u], RESIDUE(u, v));                              \\
    flow[u][v] += delta;                                                       \\
    flow[v][w] -= delta;                                                       \\
    excess[u] -= delta;                                                        \\
    excess[v] += delta;                                                        \\
  }
Weight maximumFlow(const Graph &g, int s, int t) {
  int n = g.size(), count = 0;
  Matrix flow(n, Array(n)), capacity(n, Array(n)); // adj. matrix
  REP(u, n) FOR(e, g[u]) capacity[e->src][e->dst] += e->weight;

  vector<Weight> excess(n);
  excess[s] = INF; // initialize step
  vector<int> d(n);
  GLOBAL_RELABELING();
  vector<queue<int>> B(n);
  B[d[s]].push(s);

  for (int b = d[s]; b >= 0;) {
    if (B[b].empty()) {
      --b;
      continue;
    }
    int v = B[b].front();
    B[b].pop();
    if (excess[v] == 0 || v == t)
      continue;

    FOR(e, g[v]) {
      int w = e->dst;                              // e is the current edge of v
      if (RESIDUE(v, w) > 0 && d[v] == d[w] + 1) { // (w,v) is admissible
        PUSH(v, w);
        if (excess[w] > 0 && w != t)
          B[d[w]].push(w);
      }
    }
    if (excess[v] == 0)
      continue;
    d[v] = n;
    FOR(e, g[v]) if (RESIDUE(v, e->dst) > 0) d[v] = min(d[v], d[e->dst] + 1);
    if (d[v] < n)
      B[b = d[v]].push(v);

    if (++count % n == 0)
      GLOBAL_RELABELING(); // !!HEURISTICS
  }
  return excess[t];
}

int main() {
  int i, V, E, s, t, e;
  // for(scanf("%d",&T);T;putchar(--T?' ':'\\n')){
  scanf("%d%d", &V, &E);
  Graph g(V);
  for (; E--;)
    scanf("%d%d%d", &s, &t, &e), g[s].push_back(Edge(s, t, e));
  printf("%d\\n", maximumFlow(g, 0, V - 1));
  //}
}