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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define FIRST first
#define SECOND second
#define FRONT front
#define PUSH_BACK push_back
#define MAKE_PAIR make_pair
#define EMPTY empty
#define size(x) ((int)(x).size())
#define PRIORITY_QUEUE priority_queue
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
// head

const int maxn = 1001;
const int inf = 5e8;
int n, m;
int d[111][111];
int p[maxn], w[maxn];
int vis[maxn];
std::vector<int> G[maxn];

void init(int x) {
  for (int i = 0; i < x; i++) {
    G[i].clear();
  }
}

int dfs(int x) {
  for (int i = 0; i < size(G[x]); i++) {
    int u = G[x][i];
    if (!vis[u]) {
      vis[u] = 1;
      if (p[u] < 0 || dfs(p[u])) {
        p[u] = x;
        return 1;
      }
    }
  }
  return 0;
}

int match(int x) {
  int num = 0;
  memset(p, -1, sizeof(p));
  for (int i = 0; i < x; i++) {
    memset(vis, 0, sizeof(vis));
    if (dfs(i)) {
      num++;
    }
  }
  return num;
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  // freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int L;
  while (cin >> n >> m >> L && n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = inf;
      }
    }
    init(L);
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      d[x][y] = z;
      d[y][x] = z;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (int i = 0; i < L; i++) {
      cin >> p[i] >> w[i];
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (d[p[i]][p[j]] + w[i] <= w[j] && i != j) {
          G[i].push_back(j);
        }
      }
    }
    cout << L - match(L) << endl;
  }
  return 0;
}


Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>
#include <vector>
#define all(x) (x).begin(), (x).end()
#define FIRST first
#define SECOND second
#define FRONT front
#define PUSH_BACK push_back
#define MAKE_PAIR make_pair
#define EMPTY empty
#define size(x) ((int)(x).size())
#define PRIORITY_QUEUE priority_queue
using namespace std;
typedef long long LL;
typedef unsigned int uii;
typedef pair<int, int> pii;
typedef unsigned long long uLL;
// head

const int maxn = 1123;
const int inf = 5e8;
int n, m;
int d[111][111];
int p[maxn], w[maxn];
int vis[maxn];
std::vector<int> G[maxn];

void init(int x) {
  for (int i = 0; i < x; i++) {
    G[i].clear();
  }
}

int dfs(int x) {
  for (int i = 0; i < size(G[x]); i++) {
    int u = G[x][i];
    if (!vis[u]) {
      vis[u] = 1;
      if (p[u] < 0 || dfs(p[u])) {
        p[u] = x;
        return 1;
      }
    }
  }
  return 0;
}

int match(int x) {
  int num = 0;
  memset(p, -1, sizeof(p));
  for (int i = 0; i < x; i++) {
    memset(vis, 0, sizeof(vis));
    if (dfs(i)) {
      num++;
    }
  }
  return num;
}

int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
  freopen("in.txt", "r", stdin);
  // freopen("out.txt", "w", stdout);
#endif
  ios::sync_with_stdio(false);
  cin.tie(0);
  int L;
  while (cin >> n >> m >> L && n) {
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = inf;
      }
    }
    init(L);
    for (int i = 0; i < m; i++) {
      int x, y, z;
      cin >> x >> y >> z;
      d[x][y] = z;
      d[y][x] = z;
    }
    for (int k = 0; k < n; k++) {
      for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
          d[i][j] = min(d[i][j], d[i][k] + d[k][j]);
        }
      }
    }
    for (int i = 0; i < L; i++) {
      cin >> p[i] >> w[i];
    }
    for (int i = 0; i < L; i++) {
      for (int j = 0; j < L; j++) {
        if (d[p[i]][p[j]] + w[i] <= w[j] && i != j) {
          G[i].push_back(j);
        }
      }
    }
    cout << L - match(L) << endl;
  }
  return 0;
}