Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
ll const mod = 998244353;
// ll const mod=1e9+7;
const ll MAX = 300000;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////////////////

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  vvi dp(n, vi(n, INF));
  rep(i, n) dp[i][i] = 0;
  rep(i, m) {
    int a, b, c;
    a--, b--;
    dp[a][b] = c;
    dp[b][a] = c;
  }
  rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]);
  vvi dp2(n, vi(n, INF));
  rep(i, n) rep(j, n) {
    if (dp[i][j] <= l)
      dp2[i][j] = 1;
  }
  rep(k, n) rep(i, n) rep(j, n) chmin(dp2[i][j], dp2[i][k] + dp2[k][j]);
  int q;
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    int ans = dp2[s][t];
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans - 1 << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define arep(i, x, n) for (int i = int(x); i < (int)(n); i++)
#define rep(i, n) for (long long i = 0; i < n; ++i)
#define rrep(i, n) for (int i = int(n - 1); i >= 0; i--)
#define fs first
#define sc second
#define all(x) (x).begin(), (x).end()
#define pi 3.141592653589793
#define eps 0.00000001
#define INF 1e9 + 7
using ll = long long;
using P = pair<int, int>;
using lP = pair<ll, ll>;
using fP = pair<double, double>;
using PPI = pair<P, int>;
ll const mod = 998244353;
// ll const mod=1e9+7;
const ll MAX = 300000;
using vi = vector<int>;
using vl = vector<ll>;
using vc = vector<char>;
using vd = vector<double>;
using vs = vector<string>;
using vp = vector<P>;
using vb = vector<bool>;
using vvi = vector<vector<int>>;
using vvd = vector<vector<double>>;
using vvc = vector<vector<char>>;
using vvp = vector<vector<P>>;
using vvb = vector<vector<bool>>;
template <typename T> bool chmax(T &a, const T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, const T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

//////////////////////////////////////

int main() {
  int n, m, l;
  cin >> n >> m >> l;
  vvi dp(n, vi(n, INF));
  rep(i, n) dp[i][i] = 0;
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;

    a--, b--;
    dp[a][b] = c;
    dp[b][a] = c;
  }
  rep(k, n) rep(i, n) rep(j, n) chmin(dp[i][j], dp[i][k] + dp[k][j]);
  vvi dp2(n, vi(n, INF));
  rep(i, n) rep(j, n) {
    if (dp[i][j] <= l)
      dp2[i][j] = 1;
  }
  rep(k, n) rep(i, n) rep(j, n) chmin(dp2[i][j], dp2[i][k] + dp2[k][j]);
  int q;
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    --s;
    --t;
    int ans = dp2[s][t];
    if (ans == INF)
      cout << -1 << endl;
    else
      cout << ans - 1 << endl;
  }
  return 0;
}