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

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

#define INF 100000000

using namespace std;

int V, match[2001];
bool used[2001];
vector<int> G[2001];

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 res = 0;
  fill(match, match + 2001, -1);
  for (int v = 0; v < V; v++) {
    if (match[v] < 0) {
      fill(used, used + 2001, false);
      if (dfs(v))
        res++;
    }
  }
  return res;
}

int main(void) {

  int n, m, l, u, v, d, graph[101][101], p[1001], t[1001];

  while (cin >> n >> m >> l, n | m | l) {

    for (int i = 0; i < 2001; i++)
      G[i].clear();

    fill(graph[0], graph[101], INF);
    for (int i = 0; i < 101; i++)
      graph[i][i] = 0;
    for (int i = 0; i < m; i++) {
      cin >> u >> v >> d;
      graph[u][v] = graph[v][u] = d;
    }

    for (int i = 0; i < l; i++) {
      cin >> p[i] >> t[i];
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);

    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i != j && graph[p[i]][p[j]] + t[i] <= t[j])
          add_edge(i, j + l);
      }
    }
    V = 2 * l;
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

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

#define INF 100000000

using namespace std;

int V, match[202];
bool used[202];
vector<int> G[202];

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 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;
}

int main(void) {

  int n, m, l, u, v, d, graph[101][101], p[1001], t[1001];

  while (cin >> n >> m >> l, n | m | l) {

    for (int i = 0; i < 2 * n; i++)
      G[i].clear();

    fill(graph[0], graph[101], INF);
    for (int i = 0; i < 101; i++)
      graph[i][i] = 0;
    for (int i = 0; i < m; i++) {
      cin >> u >> v >> d;
      graph[u][v] = graph[v][u] = d;
    }

    for (int i = 0; i < l; i++) {
      cin >> p[i] >> t[i];
    }

    for (int k = 0; k < n; k++)
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          graph[i][j] = min(graph[i][j], graph[i][k] + graph[k][j]);

    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i != j && graph[p[i]][p[j]] + t[i] <= t[j])
          add_edge(i, j + l);
      }
    }
    V = 2 * l;
    cout << l - bipartite_matching() << endl;
  }
  return 0;
}