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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
struct Pox {
  int to, cap, rev;
  Pox(int to = -1, int cap = -1, int rev = -1) : to(to), cap(cap), rev(rev) {}
};
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<Pox> VP;
typedef vector<VP> VVP;

int N, M, L;

void printVI(VI &a) {
  cout << "printVI =====" << endl;
  rep(i, a.size()) cout << a[i] << " ";
  cout << endl << endl;
}

void printVVI(VVI &a) {
  cout << "ptinrVVI =====" << endl;
  rep(i, a.size()) {
    rep(j, a[i].size()) { cout << a[i][j] << " "; }
    cout << endl;
  }
  cout << endl;
}

void printVVP(VVP &g) {
  cout << "printVVP ====" << endl;
  rep(i, g.size()) {
    cout << "i: " << i << ": ";
    rep(j, g[i].size()) {
      cout << "(" << g[i][j].to << "," << g[i][j].cap << "," << g[i][j].rev
           << ") ";
    }
    cout << endl;
  }
  cout << endl;
}

void add_edge(VVP &G, int from, int to, int cap) {
  G[from].push_back(Pox(to, cap, G[to].size()));
  G[to].push_back(Pox(from, 0, G[from].size() - 1));
}

void bfs(int s, VI &level, VVP &G) {
  rep(i, level.size()) level[i] = -1;
  level[s] = 0;
  deque<int> deq;
  deq.push_back(s);
  while (!deq.empty()) {
    int p = deq.front();
    deq.pop_front();
    for (int i = 0; i < G[p].size(); i++) {
      Pox &pox = G[p][i];
      if (pox.cap > 0 && level[pox.to] < 0) {
        level[pox.to] = level[p] + 1;
        deq.push_back(pox.to);
      }
    }
  }
}

int dfs(VVP &G, VI &level, VI &iter, int v, int t, int f) {
  // cout << "v = " << v << endl;
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    Pox &pox = G[v][i];
    if (pox.cap <= 0)
      continue;
    if (level[pox.to] <= level[v])
      continue;
    // cout << "goto " << pox.to << endl;
    int flow = dfs(G, level, iter, pox.to, t, min(f, pox.cap));
    // assert(flow <= pox.cap);
    if (flow > 0) {
      pox.cap -= flow;
      G[pox.to][pox.rev].cap += flow;
      return flow;
    }
  }
  return 0;
}

int maximum_flow(VVP &G, int s, int t) {
  int SIZE = 2 * N + 3;
  VI level(G.size());
  VI iter(G.size());

  int f = 0;
  while (true) {
    rep(i, G.size()) iter[i] = 0;
    bfs(s, level, G);
    // cout << "N = " << N << endl;
    // printVI(level);

    if (level[t] == -1)
      return f;
    int flow = dfs(G, level, iter, s, t, (1 << 29));
    // cout << "return f = " << f  << endl;
    assert(flow >= 0);
    if (!flow)
      return f;
    if (flow > 0)
      f += flow;
  }
}

int main() {
  while (cin >> N >> M >> L) {
    int SIZE1 = N + 1;
    int SIZE2 = 2 * N + 2;
    int total = 0;
    VVI FWG(SIZE1, VI(SIZE1));
    VVP G(SIZE2);
    VI P(SIZE1), K(SIZE1);
    rep(i, SIZE1) rep(j, SIZE1) FWG[i][j] = i == j ? 0 : (1 << 29);
    rep(i, M) {
      int A, B, D;
      cin >> A >> B >> D;
      FWG[A][B] = D;
      FWG[B][A] = D;
    }

    rep(k, SIZE1) rep(i, SIZE1) rep(j, SIZE1) FWG[i][j] =
        min(FWG[i][j], FWG[i][k] + FWG[k][j]);

    REP(i, 1, SIZE1) cin >> P[i];
    REP(i, 1, SIZE1) cin >> K[i], total += K[i];

    REP(i, 1, N + 1) add_edge(G, 0, i, P[i]);
    REP(i, 1, N + 1) add_edge(G, i + N, 2 * N + 1, K[i]);
    REP(i, 1, N + 1)
    REP(j, 1, N + 1)
    if (FWG[i][j] < L)
      add_edge(G, i, j + N, (1 << 29));

    /*
    printVVI(FWG);
    rep(i,G.size())
    cout << G[i].size() << endl;
    cout << endl;
    printVVP(G);
    */
    int ret = maximum_flow(G, 0, 2 * N + 1);
    assert(0 <= ret && ret <= total);
    cout << ret << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <deque>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
using namespace std;
struct Pox {
  int to, cap, rev;
  Pox(int to = -1, int cap = -1, int rev = -1) : to(to), cap(cap), rev(rev) {}
};
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<Pox> VP;
typedef vector<VP> VVP;

int N, M, L;

void printVI(VI &a) {
  cout << "printVI =====" << endl;
  rep(i, a.size()) cout << a[i] << " ";
  cout << endl << endl;
}

void printVVI(VVI &a) {
  cout << "ptinrVVI =====" << endl;
  rep(i, a.size()) {
    rep(j, a[i].size()) { cout << a[i][j] << " "; }
    cout << endl;
  }
  cout << endl;
}

void printVVP(VVP &g) {
  cout << "printVVP ====" << endl;
  rep(i, g.size()) {
    cout << "i: " << i << ": ";
    rep(j, g[i].size()) {
      cout << "(" << g[i][j].to << "," << g[i][j].cap << "," << g[i][j].rev
           << ") ";
    }
    cout << endl;
  }
  cout << endl;
}

void add_edge(VVP &G, int from, int to, int cap) {
  G[from].push_back(Pox(to, cap, G[to].size()));
  G[to].push_back(Pox(from, 0, G[from].size() - 1));
}

void bfs(int s, VI &level, VVP &G) {
  rep(i, level.size()) level[i] = -1;
  level[s] = 0;
  deque<int> deq;
  deq.push_back(s);
  while (!deq.empty()) {
    int p = deq.front();
    deq.pop_front();
    for (int i = 0; i < G[p].size(); i++) {
      Pox &pox = G[p][i];
      if (pox.cap > 0 && level[pox.to] < 0) {
        level[pox.to] = level[p] + 1;
        deq.push_back(pox.to);
      }
    }
  }
}

int dfs(VVP &G, VI &level, VI &iter, int v, int t, int f) {
  // cout << "v = " << v << endl;
  if (v == t)
    return f;
  for (int &i = iter[v]; i < G[v].size(); i++) {
    Pox &pox = G[v][i];
    if (pox.cap <= 0)
      continue;
    // cout << "goto " << pox.to << endl;
    int flow = dfs(G, level, iter, pox.to, t, min(f, pox.cap));
    assert(flow <= pox.cap);
    if (flow > 0 && level[pox.to] > level[v]) {
      pox.cap -= flow;
      G[pox.to][pox.rev].cap += flow;
      return flow;
    }
  }
  return 0;
}

int maximum_flow(VVP &G, int s, int t) {
  int SIZE = 2 * N + 3;
  VI level(G.size());
  VI iter(G.size());

  int f = 0;
  while (true) {
    rep(i, G.size()) iter[i] = 0;
    bfs(s, level, G);
    // cout << "N = " << N << endl;
    // printVI(level);

    if (level[t] == -1)
      return f;
    int flow = dfs(G, level, iter, s, t, (1 << 29));
    // cout << "return f = " << f  << endl;
    assert(flow >= 0);
    if (!flow)
      return f;
    if (flow > 0)
      f += flow;
  }
}

int main() {
  while (cin >> N >> M >> L) {
    int SIZE1 = N + 1;
    int SIZE2 = 2 * N + 2;
    int total = 0;
    VVI FWG(SIZE1, VI(SIZE1));
    VVP G(SIZE2);
    VI P(SIZE1), K(SIZE1);
    rep(i, SIZE1) rep(j, SIZE1) FWG[i][j] = i == j ? 0 : (1 << 29);
    rep(i, M) {
      int A, B, D;
      cin >> A >> B >> D;
      FWG[A][B] = D;
      FWG[B][A] = D;
    }

    rep(k, SIZE1) rep(i, SIZE1) rep(j, SIZE1) FWG[i][j] =
        min(FWG[i][j], FWG[i][k] + FWG[k][j]);

    REP(i, 1, SIZE1) cin >> P[i];
    REP(i, 1, SIZE1) cin >> K[i], total += K[i];

    REP(i, 1, N + 1) add_edge(G, 0, i, P[i]);
    REP(i, 1, N + 1) add_edge(G, i + N, 2 * N + 1, K[i]);
    REP(i, 1, N + 1)
    REP(j, 1, N + 1)
    if (FWG[i][j] < L)
      add_edge(G, i, j + N, (1 << 29));

    /*
    printVVI(FWG);
    rep(i,G.size())
    cout << G[i].size() << endl;
    cout << endl;
    printVVP(G);
    */
    int ret = maximum_flow(G, 0, 2 * N + 1);
    assert(0 <= ret && ret <= total);
    cout << ret << endl;
  }
  return 0;
}