Problem p02889 (C++)

Buggy submission (status: Runtime Error):
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MAX_N 100000000
#define INF 100000000000000

int main() {
  int n, m;
  long long l;
  cin >> n >> m >> l;
  vector<pair<int, long long>> adj[m];
  int a, b;
  long long c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a - 1].push_back(make_pair(b - 1, c));
    adj[b - 1].push_back(make_pair(a - 1, c));
  }
  long long d[n][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;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      b = adj[i][j].first;
      d[i][b] = adj[i][j].second;
    }
  }

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

  long long rd[n][n];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        rd[i][j] = 0;
      else if (d[i][j] <= l)
        rd[i][j] = 1;
      else
        rd[i][j] = INF;
    }
  }

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

  int q;
  cin >> q;
  int s[q];
  int t[q];
  for (int i = 0; i < q; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 0; i < q; i++) {
    if (rd[s[i] - 1][t[i] - 1] == INF)
      cout << -1 << endl;
    else
      cout << rd[s[i] - 1][t[i] - 1] - 1 << endl;
  }

  return 0;
}


Fixed submission:
// #include<bits/stdc++.h>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define MAX_N 100000000
#define INF 100000000000000

int main() {
  int n, m;
  long long l;
  cin >> n >> m >> l;
  vector<pair<int, long long>> adj[n];
  int a, b;
  long long c;
  for (int i = 0; i < m; i++) {
    cin >> a >> b >> c;
    adj[a - 1].push_back(make_pair(b - 1, c));
    adj[b - 1].push_back(make_pair(a - 1, c));
  }
  long long d[n][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;
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < adj[i].size(); j++) {
      b = adj[i][j].first;
      d[i][b] = adj[i][j].second;
    }
  }

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

  long long rd[n][n];

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j)
        rd[i][j] = 0;
      else if (d[i][j] <= l)
        rd[i][j] = 1;
      else
        rd[i][j] = INF;
    }
  }

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

  int q;
  cin >> q;
  int s[q];
  int t[q];
  for (int i = 0; i < q; i++) {
    cin >> s[i] >> t[i];
  }
  for (int i = 0; i < q; i++) {
    if (rd[s[i] - 1][t[i] - 1] == INF)
      cout << -1 << endl;
    else
      cout << rd[s[i] - 1][t[i] - 1] - 1 << endl;
  }

  return 0;
}