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

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 3050
#define INF 1 << 28
vector<int> G[MAX_V];
int match[MAX_V], V;
bool used[MAX_V];
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 < (int)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 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;
}
typedef pair<int, int> P;
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    int e[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        e[i][j] = (i != j) * INF;
    for (int i = 0; i < m; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      e[u][v] = e[v][u] = d;
    }
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
    vector<P> p(l);
    for (int i = 0; i < l; i++)
      cin >> p[i].first >> p[i].second;
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i == j)
          continue;
        int u = p[i].first, v = p[j].first;
        if (p[j].second - p[i].second >= e[u][v])
          add_edge(i, l + j);
      }
    }
    V = l * 2;
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define MAX_V 1050
#define INF 1 << 28
vector<int> G[MAX_V];
int match[MAX_V], V;
bool used[MAX_V];
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 < (int)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 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;
}
typedef pair<int, int> P;
int main() {
  int n, m, l;
  while (cin >> n >> m >> l, n || m || l) {
    for (int i = 0; i < MAX_V; i++)
      G[i].clear();
    int e[n][n];
    for (int i = 0; i < n; i++)
      for (int j = 0; j < n; j++)
        e[i][j] = (i != j) * INF;
    for (int i = 0; i < m; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      e[u][v] = e[v][u] = d;
    }
    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          e[i][j] = min(e[i][j], e[i][k] + e[k][j]);
    vector<P> p(l);
    for (int i = 0; i < l; i++)
      cin >> p[i].first >> p[i].second;
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i == j)
          continue;
        int u = p[i].first, v = p[j].first;
        if (p[j].second - p[i].second >= e[u][v])
          add_edge(u, l + v);
      }
    }
    V = l * 2;
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}