Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
#define pi acos(-1)
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD;
#define MODSUB(SUB_X, SUB_Y)  (SUB_X-SUB_Y)+MOD)%MOD;
#define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD;
#define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y);
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
const ll INF = 1e17;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
ll dist[500][500], gas[500][500];
ll a[500], b[500], c[500];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      ll n,
      m, l;
  cin >> n >> m >> l;
  for (int i = 1; i <= m; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dist[i][j] = INF, gas[i][j] = INF;
    gas[i][i] = 0;
    dist[i][i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    gas[a[i]][b[i]] = min(gas[a[i]][b[i]], c[i]);
    gas[b[i]][a[i]] = min(gas[b[i]][a[i]], c[i]);
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        gas[i][j] = min(gas[i][j], gas[i][k] + gas[k][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (gas[i][j] <= l)
        dist[i][j] = min(dist[i][j], 1ll);
    }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    if (dist[x][y] == INF)
      cout << "-1" << endl;
    else
      cout << dist[x][y] - 1 << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
#define all(q) (q).begin(), (q).end()
#define pi acos(-1)
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define MODADD(ADD_X, ADD_Y) (ADD_X + ADD_Y) % MOD;
#define MODSUB(SUB_X, SUB_Y)  (SUB_X-SUB_Y)+MOD)%MOD;
#define MODMUL(MUL_X, MUL_Y) (MUL_X * MUL_Y) % MOD;
#define LCM(LCM_X, LCM_Y) (LCM_X * LCM_Y) / __gcd(LCM_X, LCM_Y);
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
const ll INF = 1e17;
int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, -1, -1, 1};
ll dist[500][500], gas[500][500];
ll a[MAX], b[MAX], c[MAX];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);
      ll n,
      m, l;
  cin >> n >> m >> l;
  for (int i = 1; i <= m; i++)
    cin >> a[i] >> b[i] >> c[i];
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++)
      dist[i][j] = INF, gas[i][j] = INF;
    gas[i][i] = 0;
    dist[i][i] = 0;
  }
  for (int i = 1; i <= m; i++) {
    gas[a[i]][b[i]] = min(gas[a[i]][b[i]], c[i]);
    gas[b[i]][a[i]] = min(gas[b[i]][a[i]], c[i]);
  }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        gas[i][j] = min(gas[i][j], gas[i][k] + gas[k][j]);
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++) {
      if (i == j)
        continue;
      if (gas[i][j] <= l)
        dist[i][j] = min(dist[i][j], 1ll);
    }
  for (int k = 1; k <= n; k++)
    for (int i = 1; i <= n; i++)
      for (int j = 1; j <= n; j++)
        dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j]);
  int q;
  cin >> q;
  while (q--) {
    int x, y;
    cin >> x >> y;
    if (dist[x][y] == INF)
      cout << "-1" << endl;
    else
      cout << dist[x][y] - 1 << endl;
  }

  return 0;
}