Problem p02889 (C++)

Buggy submission (status: Runtime Error):
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

#define all(x) (x).begin(), (x).end()
#define Sort(x) sort(all((x)))
#define X first
#define Y second
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << " = " << x << endl
#define SZ(x) ll(x.size())
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 1e2 + 10;
const ll LOG = 22;
const ll INF = 1e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n, m, L, dist1[MAXN][MAXN], dist2[MAXN][MAXN], q;

int main() {
  fast_io;
  for (ll i = 0; i < MAXN; i++) {
    for (ll j = 0; j < MAXN; j++) {
      dist1[i][j] = dist2[i][j] = INF;
    }
    dist1[i][i] = dist2[i][i] = 0;
  }

  cin >> n >> m >> L;
  for (ll i = 1; i <= m; i++) {
    ll v, u, w;
    cin >> v >> u >> w;
    dist1[v][u] = dist1[u][v] = w;
  }

  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]);
      }
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (dist1[i][j] <= L)
        dist2[i][j] = 1;
    }
  }

  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]);
      }
    }
  }

  cin >> q;
  while (q--) {
    ll v, u;
    cin >> v >> u;
    cout << (dist2[v][u] == INF ? -1 : dist2[v][u] - 1) << endl;
  }

  return 0;
}
/*

*/


Fixed submission:
/**
 * code generated by JHelper
 * More info: https://github.com/AlexeyDmitriev/JHelper
 * @author alireza_kaviani
 */

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

template <class T>
using Tree =
    tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;

typedef long long ll;
typedef long double ld;
typedef pair<ll, ll> pll;
typedef pair<double, double> pdd;

#define all(x) (x).begin(), (x).end()
#define Sort(x) sort(all((x)))
#define X first
#define Y second
#define Mp make_pair
#define sep ' '
#define endl '\n'
#define debug(x) cerr << #x << " = " << x << endl
#define SZ(x) ll(x.size())
#define fast_io                                                                \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define set_random                                                             \
  mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());

ll poww(ll a, ll b, ll md) {
  return (!b ? 1
             : (b & 1 ? a * poww(a * a % md, b / 2, md) % md
                      : poww(a * a % md, b / 2, md) % md));
}

const ll MAXN = 3e2 + 10;
const ll LOG = 22;
const ll INF = 1e18;
const ll MOD = 1e9 + 7; // 998244353; // 1e9 + 9;

ll n, m, L, dist1[MAXN][MAXN], dist2[MAXN][MAXN], q;

int main() {
  fast_io;
  for (ll i = 0; i < MAXN; i++) {
    for (ll j = 0; j < MAXN; j++) {
      dist1[i][j] = dist2[i][j] = INF;
    }
    dist1[i][i] = dist2[i][i] = 0;
  }

  cin >> n >> m >> L;
  for (ll i = 1; i <= m; i++) {
    ll v, u, w;
    cin >> v >> u >> w;
    dist1[v][u] = dist1[u][v] = w;
  }

  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        dist1[i][j] = min(dist1[i][j], dist1[i][k] + dist1[k][j]);
      }
    }
  }
  for (ll i = 1; i <= n; i++) {
    for (ll j = 1; j <= n; j++) {
      if (dist1[i][j] <= L)
        dist2[i][j] = 1;
    }
  }

  for (ll k = 1; k <= n; k++) {
    for (ll i = 1; i <= n; i++) {
      for (ll j = 1; j <= n; j++) {
        dist2[i][j] = min(dist2[i][j], dist2[i][k] + dist2[k][j]);
      }
    }
  }

  cin >> q;
  while (q--) {
    ll v, u;
    cin >> v >> u;
    cout << (dist2[v][u] == INF ? -1 : dist2[v][u] - 1) << endl;
  }

  return 0;
}
/*

*/