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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool G[2000][2000];
int match[2000];
bool used[2000];

int V;

void add_edge(int v, int u) { G[v][u] = G[u][v] = true; }

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

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

typedef long long ll;
const ll INF = 1LL << 50;
ll wf[100][100];
int p[1000];
ll t[1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, L;
  while (cin >> N >> M >> L, N) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        wf[i][j] = i == j ? 0 : INF;

    for (int i = 0; i < M; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      wf[u][v] = wf[v][u] = d;
    }

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

    V = 2 * L;
    for (int i = 0; i < L; i++) {
      cin >> p[i] >> t[i];
    }
    for (int i = 0; i < V; i++) {
      for (int j = 0; j < V; j++) {
        G[i][j] = false;
      }
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (i == j)
          continue;
        int u = p[i], v = p[j];
        if (t[i] + wf[u][v] <= t[j]) {
          add_edge(i, j + L);
        }
      }
    }

    cout << L - calc() << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

bool G[1000][1000];
int match[1000];
bool used[1000];

int V;

void add_edge(int v, int u) { G[v][u] = G[u][v] = true; }

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

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

typedef long long ll;
const ll INF = 1LL << 50;
ll wf[100][100];
int p[1000];
ll t[1000];

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, L;
  while (cin >> N >> M >> L, N) {
    for (int i = 0; i < N; i++)
      for (int j = 0; j < N; j++)
        wf[i][j] = i == j ? 0 : INF;

    for (int i = 0; i < M; i++) {
      int u, v, d;
      cin >> u >> v >> d;
      wf[u][v] = wf[v][u] = d;
    }

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

    V = 2 * L;
    for (int i = 0; i < L; i++) {
      cin >> p[i] >> t[i];
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        G[i][j] = false;
      }
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (i == j)
          continue;
        int u = p[i], v = p[j];
        if (t[i] + wf[u][v] <= t[j]) {
          add_edge(i, j + L);
        }
      }
    }

    cout << L - calc() << endl;
  }
}