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

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

#define MAX_V 10000
#define INF 1000000000

void add_edge(int, int);
bool dfs(int);
int bipartite_matching(int);
void warshall_floyd(int);

using namespace std;

vector<int> G[MAX_V];
bool used[MAX_V];
int match[MAX_V], d[MAX_V][MAX_V];

int main(void) {
  int n, m, l, p[MAX_V], t[MAX_V];

  while (scanf("%d %d %d", &n, &m, &l), n, m, l) {
    for (int i = 0; i < MAX_V * 2; i++)
      G[i].clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = i == j ? 0 : INF;

    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d %d %d", &a, &b, &c);
      d[a][b] = d[b][a] = c;
    }
    warshall_floyd(n);

    for (int i = 0; i < l; i++)
      scanf("%d %d", &p[i], &t[i]);

    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i == j)
          continue;
        else if (t[i] + d[p[i]][p[j]] <= t[j])
          add_edge(i, j + l);
      }
    }

    cout << l - bipartite_matching(2 * l) << endl;
  }
  return 0;
}

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching(int V) {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v))
        res++;
    }
  }
  return res;
}

void warshall_floyd(int V) {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }
}

Error: 0

Fixed submission:

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

#define MAX_V 1001
#define INF 1000000000

void add_edge(int, int);
bool dfs(int);
int bipartite_matching(int);
void warshall_floyd(int);

using namespace std;

vector<int> G[MAX_V];
bool used[MAX_V];
int match[MAX_V], d[MAX_V][MAX_V];

int main(void) {
  int n, m, l, p[MAX_V], t[MAX_V];

  while (scanf("%d %d %d", &n, &m, &l), n, m, l) {
    for (int i = 0; i < MAX_V * 2; i++)
      G[i].clear();
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        d[i][j] = i == j ? 0 : INF;

    for (int i = 0; i < m; i++) {
      int a, b, c;
      scanf("%d %d %d", &a, &b, &c);
      d[a][b] = d[b][a] = c;
    }
    warshall_floyd(n);

    for (int i = 0; i < l; i++)
      scanf("%d %d", &p[i], &t[i]);

    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i == j)
          continue;
        else if (t[i] + d[p[i]][p[j]] <= t[j])
          add_edge(i, j + l);
      }
    }

    cout << l - bipartite_matching(2 * l) << endl;
  }
  return 0;
}

void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}

bool dfs(int v) {
  used[v] = true;
  for (int i = 0; i < G[v].size(); i++) {
    int u = G[v][i], w = match[u];
    if (w < 0 || !used[w] && dfs(w)) {
      match[v] = u;
      match[u] = v;
      return true;
    }
  }
  return false;
}

int bipartite_matching(int V) {
  int res = 0;
  memset(match, -1, sizeof(match));
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      memset(used, 0, sizeof(used));
      if (dfs(v))
        res++;
    }
  }
  return res;
}

void warshall_floyd(int V) {
  for (int i = 0; i < V; i++) {
    for (int j = 0; j < V; j++) {
      for (int k = 0; k < V; k++) {
        d[j][k] = min(d[j][k], d[j][i] + d[i][k]);
      }
    }
  }
}