Problem p02889 (C++)

Buggy submission (status: Runtime Error):
///*****************************???????????????????

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Khela                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pf printf
#define sf scanf
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
const ll Max = 1e18 + 100;
const int sz = 111;
ll dist[2][sz][sz];
void Floyad(ll n, ll l, ll tar) {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dist[tar][i][k] + dist[tar][k][j] < dist[tar][i][j])
          dist[tar][i][j] = dist[tar][i][k] + dist[tar][k][j];
      }
    }
  }
}
int main() {
  Khela ll a, b, c, i, j, k, q, p, x, y, ct, ct1, m, l, r, x1, y1, mn, h, sum1,
      in, z, mid, n, mx;
  char ch;
  double d;
  string str1, str2, str;
  int t, cs;
  cin >> n >> m >> l;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        dist[0][i][j] = dist[1][i][j] = 0;
      else
        dist[0][i][j] = dist[1][i][j] = Max;
    }
  }

  for (i = 1; i <= m; i++) {
    cin >> p >> q >> c;
    dist[0][p][q] = dist[0][q][p] = c;
  }
  Floyad(n, l, 0ll);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == j)
        dist[1][i][j] = 0;
      else
        dist[1][i][j] = dist[1][j][i] = ((dist[0][i][j] <= l) ? 1ll : Max);
    }
  }
  Floyad(n, l, 1ll);

  cin >> x;
  while (x--) {
    cin >> p >> q;
    if (dist[1][p][q] >= Max)
      cout << "-1" << endl;
    else {
      cout << dist[1][p][q] - 1 << endl;
    }
  }
}

/*
   help taken from: https://codeforces.com/blog/entry/70703

*/


Fixed submission:
///*****************************???????????????????

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
using namespace std;
#define Khela                                                                  \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define ll long long int
#define llu long long unsigned int
#define pf printf
#define sf scanf
#define f first
#define s second
#define pb push_back
#define mk make_pair
#define pii pair<int, int>

int dx8[] = {0, 0, 1, 1, 1, -1, -1, -1};
int dy8[] = {1, -1, 1, -1, 0, 0, -1, 1};
int dx4[] = {0, 0, 1, -1};
int dy4[] = {1, -1, 0, 0};

typedef tree<pair<int, int>, null_type, less<pair<int, int>>, rb_tree_tag,
             tree_order_statistics_node_update>
    ordered_set;
int Set(int N, int pos) { return N = N | (1 << pos); }
int reset(int N, int pos) { return N = N & ~(1 << pos); }
bool check(int N, int pos) { return (bool)(N & (1 << pos)); }
const ll Max = 1e18 + 100;
const int sz = 310;
ll dist[2][sz][sz];
void Floyad(ll n, ll l, ll tar) {
  for (int k = 1; k <= n; k++) {
    for (int i = 1; i <= n; i++) {
      for (int j = 1; j <= n; j++) {
        if (dist[tar][i][k] + dist[tar][k][j] < dist[tar][i][j])
          dist[tar][i][j] = dist[tar][i][k] + dist[tar][k][j];
      }
    }
  }
}
int main() {
  Khela ll a, b, c, i, j, k, q, p, x, y, ct, ct1, m, l, r, x1, y1, mn, h, sum1,
      in, z, mid, n, mx;
  char ch;
  double d;
  string str1, str2, str;
  int t, cs;
  cin >> n >> m >> l;
  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      if (i == j)
        dist[0][i][j] = dist[1][i][j] = 0;
      else
        dist[0][i][j] = dist[1][i][j] = Max;
    }
  }

  for (i = 1; i <= m; i++) {
    cin >> p >> q >> c;
    dist[0][p][q] = dist[0][q][p] = c;
  }
  Floyad(n, l, 0ll);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= n; j++) {
      if (i == j)
        dist[1][i][j] = 0;
      else
        dist[1][i][j] = dist[1][j][i] = ((dist[0][i][j] <= l) ? 1ll : Max);
    }
  }
  Floyad(n, l, 1ll);

  cin >> x;
  while (x--) {
    cin >> p >> q;
    if (dist[1][p][q] >= Max)
      cout << "-1" << endl;
    else {
      cout << dist[1][p][q] - 1 << endl;
    }
  }
}

/*
   help taken from: https://codeforces.com/blog/entry/70703

*/