Problem p01199 (C++)

Buggy submission (status: Runtime Error):
#include <cstring>
#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)

#define debug(x) cout << #x << " is " << x << endl;

using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 29;

struct edge {
  int to, cap, rev;
  edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};

const int N = 101;
vector<vector<edge>> G;
int visit[N];

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

int dfs(int s, int g, int f) {
  if (s == g)
    return f;
  visit[s] = 1;
  rep(i, G[s].size()) {
    if (visit[G[s][i].to] || G[s][i].cap <= 0)
      continue;
    int d = dfs(G[s][i].to, g, min(f, G[s][i].cap));
    if (d > 0) {
      G[s][i].cap -= d;
      G[G[s][i].to][G[s][i].rev].cap += d;
      return d;
    }
  }
  return 0;
}

int ford_fulkerson(int s, int g) {
  int flow = 0;
  while (1) {
    memset(visit, 0, sizeof(visit));
    int f = dfs(s, g, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int v[N];
int dist[N][N];
int main() {
  int n, m, l;
  while (cin >> n >> m >> l) {
    rep(i, n) rep(j, n) dist[i][j] = INF;
    rep(i, m) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      dist[a][b] = dist[b][a] = d;
    }
    rep(i, n) rep(j, n) rep(k, n) dist[j][k] =
        min(dist[j][k], dist[j][i] + dist[i][k]);

    // rep(i, n){
    //	rep(j, n) cout << dist[i][j] << ' ';
    //	cout << endl;
    // }cout << endl;

    G.clear();
    G.resize(2 * n + 2);
    int s = 2 * n, g = 2 * n + 1;
    rep(i, n) {
      int p;
      cin >> p;
      add_edge(s, i, p);
    }
    rep(i, n) {
      int k;
      cin >> k;
      add_edge(n + i, g, k);
    }
    rep(i, n) rep(j, n) {
      if (i == j || dist[i][j] < l) {
        add_edge(i, j + n, INF);
      }
    }
    cout << ford_fulkerson(s, g) << endl;
  }

  return 0;
}

Fixed submission:
#include <cstring>
#include <iostream>
#include <vector>

#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, n) FOR(i, 0, n)

#define debug(x) cout << #x << " is " << x << endl;

using namespace std;

typedef pair<int, int> P;

const int INF = 1 << 29;

struct edge {
  int to, cap, rev;
  edge(int t, int c, int r) : to(t), cap(c), rev(r) {}
};

const int N = 101;
vector<vector<edge>> G;
int visit[3 * N];

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

int dfs(int s, int g, int f) {
  if (s == g)
    return f;
  visit[s] = 1;
  rep(i, G[s].size()) {
    if (visit[G[s][i].to] || G[s][i].cap <= 0)
      continue;
    int d = dfs(G[s][i].to, g, min(f, G[s][i].cap));
    if (d > 0) {
      G[s][i].cap -= d;
      G[G[s][i].to][G[s][i].rev].cap += d;
      return d;
    }
  }
  return 0;
}

int ford_fulkerson(int s, int g) {
  int flow = 0;
  while (1) {
    memset(visit, 0, sizeof(visit));
    int f = dfs(s, g, INF);
    if (f == 0)
      return flow;
    flow += f;
  }
}

int v[N];
int dist[N][N];
int main() {
  int n, m, l;
  while (cin >> n >> m >> l) {
    rep(i, n) rep(j, n) dist[i][j] = INF;
    rep(i, m) {
      int a, b, d;
      cin >> a >> b >> d;
      a--, b--;
      dist[a][b] = dist[b][a] = d;
    }
    rep(i, n) rep(j, n) rep(k, n) dist[j][k] =
        min(dist[j][k], dist[j][i] + dist[i][k]);

    // rep(i, n){
    //	rep(j, n) cout << dist[i][j] << ' ';
    //	cout << endl;
    // }cout << endl;

    G.clear();
    G.resize(2 * n + 2);
    int s = 2 * n, g = 2 * n + 1;
    rep(i, n) {
      int p;
      cin >> p;
      add_edge(s, i, p);
    }
    rep(i, n) {
      int k;
      cin >> k;
      add_edge(n + i, g, k);
    }
    rep(i, n) rep(j, n) {
      if (i == j || dist[i][j] < l) {
        add_edge(i, j + n, INF);
      }
    }
    cout << ford_fulkerson(s, g) << endl;
  }

  return 0;
}