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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n, m, l;
int d[101][101];
int P[101];
int K[101];
const int INF = 1000000000;
const int MAX_V = 301;
int sp, gp;

struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
bool used[MAX_V];

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

// ÁpXðTõ
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    fill(used, used + MAX_V, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    else
      flow += f;
  }
  return 0;
}
void warshallFloyd() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
}

int main() {

  while (cin >> n >> m >> l) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
    for (int i = 0; i < m; i++) {
      int a, b, D;
      cin >> a >> b >> D;
      a--;
      b--;
      d[a][b] = d[b][a] = D;
    }
    warshallFloyd();
    for (int i = 0; i < n; i++)
      cin >> P[i];
    for (int i = 0; i < n; i++)
      cin >> K[i];
    sp = 2 * n;
    gp = 2 * n + 1;
    // ÅZ£ªLæè¬³¢ÌàÌðGbWÅÂÈ®
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (d[i][j] < l)
          add_edge(i, j + n, INF);
    // start©ç
    for (int i = 0; i < n; i++)
      add_edge(sp, i, P[i]);
    // goalÖ
    for (int i = 0; i < n; i++)
      add_edge(i + n, gp, K[i]);
    int res = max_flow(sp, gp);
    cout << res << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;

int n, m, l;
int d[101][101];
int P[101];
int K[101];
const int INF = 1000000000;
const int MAX_V = 101;
int sp, gp;

struct edge {
  int to, cap, rev;
};

vector<edge> G[MAX_V];
bool used[MAX_V];

void add_edge(int from, int to, int cap) {
  edge e;

  e.to = to;
  e.cap = cap;
  e.rev = G[to].size();
  G[from].push_back(e);

  e.to = from;
  e.cap = 0;
  e.rev = G[from].size() - 1;
  G[to].push_back(e);
}

// ÁpXðTõ
int dfs(int v, int t, int f) {
  if (v == t)
    return f;
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    edge &e = G[v][i];
    if (!used[e.to] && e.cap > 0) {
      int d = dfs(e.to, t, min(f, e.cap));
      if (d > 0) {
        e.cap -= d;
        G[e.to][e.rev].cap += d;
        return d;
      }
    }
  }
  return 0;
}

int max_flow(int s, int t) {
  int flow = 0;
  while (1) {
    fill(used, used + MAX_V, false);
    int f = dfs(s, t, INF);
    if (f == 0)
      return flow;
    else
      flow += f;
  }
  return 0;
}
void warshallFloyd() {
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++)
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
}

int main() {

  while (cin >> n >> m >> l) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    for (int i = 0; i < 101; i++)
      for (int j = 0; j < 101; j++)
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
    for (int i = 0; i < m; i++) {
      int a, b, D;
      cin >> a >> b >> D;
      a--;
      b--;
      d[a][b] = d[b][a] = D;
    }
    warshallFloyd();
    for (int i = 0; i < n; i++)
      cin >> P[i];
    for (int i = 0; i < n; i++)
      cin >> K[i];
    sp = 2 * n;
    gp = 2 * n + 1;
    // ÅZ£ªLæè¬³¢ÌàÌðGbWÅÂÈ®
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        if (d[i][j] < l)
          add_edge(i, j + n, INF);
    // start©ç
    for (int i = 0; i < n; i++)
      add_edge(sp, i, P[i]);
    // goalÖ
    for (int i = 0; i < n; i++)
      add_edge(i + n, gp, K[i]);
    int res = max_flow(sp, gp);
    cout << res << endl;
  }
  return 0;
}