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

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

using namespace std;

const int MAX_V = 1001;
typedef pair<int, int> pii;
// d[u][v]Íu©çvÖÌRXgBßÉ×Ú·éGbWÌRXgðüêÄ¨¢½è·êÎæ¢Í¸B
int d[MAX_V][MAX_V];
pii works[1001];
vector<int> G[MAX_V * 2];
int match[MAX_V * 2];
bool used[MAX_V * 2];

const int INF = 1000000000;

// S_ÎÅZoHâèðÆ­O(V^3)AÓª ÁÄà®ì
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]);
      }
    }
  }
}

int n, m, l;

// uÆvðÔÓðOtÉÇÁ
void add_edge(int u, int v) {
  G[u].push_back(v);
  G[v].push_back(u);
}
// ÁpXðdfsÅT·
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;
}

// ñOtÌÅå}b`Oðßé
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;
}

int main() {

  while (cin >> n >> m >> l && (n | m | l)) {
    for (int i = 0; i < MAX_V * 2; i++)
      G[i].clear();
    for (int i = 0; i < MAX_V; i++) {
      for (int j = 0; j < MAX_V; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
      }
    }
    for (int i = 0; i < m; i++) {
      int u, v, c;
      cin >> u >> v >> c;
      d[u][v] = c;
      d[v][u] = c;
    }
    warshall_floyd(n);
    for (int i = 0; i < l; i++)
      cin >> works[i].first >> works[i].second;
    for (int i = 0; i < l; i++) {
      for (int j = 0; j < l; j++) {
        if (i == j)
          continue;
        else if (works[i].second + d[works[i].first][works[j].first] <=
                 works[j].second)
          add_edge(i, j + l);
      }
    }
    // OtGÉÂ¢ÄAñ}b`Oðs¤
    int res = bipartite_matching(2 * l);
    cout << l - res << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

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

using namespace std;

typedef pair<int, int> P;
int n, m, l;
const int INF = 1000000000;
const int MAX_V = 300;
// d[u][v]Íu©çvÖÌRXgBßÉ×Ú·éGbWÌRXgðüêÄ¨¢½è·êÎæ¢Í¸B
int d[MAX_V][MAX_V];
// ¸_
int V;
// S_ÎÅZoHâèðÆ­O(V^3)AÓª ÁÄà®ì
void warshall_floyd() {
  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]);
}

int main() {
  while (cin >> n >> m >> l && !(n == 0 && m == 0 && l == 0)) {
    for (int i = 0; i < MAX_V; i++) {
      for (int j = 0; j < MAX_V; j++) {
        if (i == j)
          d[i][j] = 0;
        else
          d[i][j] = INF;
      }
    }
    for (int i = 0; i < m; i++) {
      int from, to, cost;
      cin >> from >> to >> cost;
      d[from][to] = d[to][from] = cost;
    }
    V = n;
    warshall_floyd();
    vector<P> v;
    for (int i = 0; i < l; i++) {
      pair<int, int> p;
      cin >> p.second >> p.first;
      v.push_back(p);
    }
    sort(v.begin(), v.end());
    int cnt = 0;
    bool used[MAX_V];
    fill(used, used + MAX_V, false);
    bool b = false;
    while (1) {
      b = false;
      int ct, node;
      ct = node = 0;
      // àÁÆàãëÌ¨©çÔÉÆÁÄ¢­
      for (int i = v.size() - 1; i >= 0; i--) {
        if (used[i])
          continue;
        used[i] = true;
        node = v[i].second;
        ct = v[i].first;
        b = true;
        break;
      }
      // æêÈ­ÈÁ½çIíè
      if (!b)
        break;
      cnt++;
      while (1) {
        int minTime = -1;
        int idx = 0;
        // »ÝÌê©çeêÖÚ®µAeNGÌI¹ÔÈãÌÔÅI¦çêéêÌÈ©ÅAàÁÆàÔªß¢àÌðIÔ
        for (int i = v.size() - 1; i >= 0; i--) {
          if (used[i])
            continue;
          int to = v[i].second;
          int time = v[i].first;
          int cost = d[node][to];
          // ðð½·
          if (ct - cost >= time) {
            if (minTime < time) {
              minTime = ct - cost;
              idx = i;
            }
          }
        }
        // ðð½·àÌª³¯êÎbreak
        if (minTime == -1)
          break;
        //  êÎAóÔðXV
        ct = v[idx].first;
        node = v[idx].second;
        used[idx] = true;
      }
    }
    cout << cnt << endl;
  }
  return 0;
}