Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

ll inf = 10000000000;

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

template <class T> void warshall_floyd(vector<vector<T>> &cost, int N) {
  for (ll k = 0; k < N; ++k) {
    for (ll i = 0; i < N; ++i) {
      for (ll j = 0; j < N; ++j) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}

/* END */

/*
int main() {
  int h,w;
  cin >> h >> w;

  vector< vector<long long> > cost(10, vector<long long>(10));

  for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j)
      cin >> cost[i][j];

  int wall[h][w];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> wall[i][j];

  floyd_wardhall(cost, 10);

  ll ans = 0;
  for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
    if (wall[i][j] == -1 or wall[i][j] == 1) continue;
    ans += cost[wall[i][j]][1];
  }

  cout << ans << endl;
}*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, L;
  cin >> N >> M >> L;

  vector<ll> a(N), b(N), c(N);
  vector<vector<ll>> fuel(N + 1, vector<ll>(N + 1, inf));
  vector<vector<ll>> dist(N + 1, vector<ll>(N + 1, inf));
  for (int i = 0; i < N + 1; ++i) {
    fuel[i][i] = 0;
    dist[i][i] = 0;
  }

  repr(i, M, 0) {
    cin >> a[i] >> b[i] >> c[i];

    fuel[a[i]][b[i]] = c[i];
    fuel[b[i]][a[i]] = c[i];
  }

  warshall_floyd(fuel, N + 1);
  for (int i = 0; i < N + 1; ++i) {
    for (int j = 0; j < N + 1; ++j) {
      if (fuel[i][j] <= L) {
        dist[i][j] = 1;
      }
    }
  }

  warshall_floyd(dist, N + 1);
  int Q;
  cin >> Q;
  repr(i, Q, 0) {
    int s, t;
    cin >> s >> t;
    if (dist[s][t] < inf) {
      cout << dist[s][t] - 1 << endl; // 最初から持っている燃料
    } else {
      cout << -1 << endl;
    }
  }
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (long long i = 0; i < n; i++)
#define repr(i, n, m) for (long long i = m; i < n; i++)
#define mod 1000000007
#define sorti(x) sort(x.begin(), x.end())
#define sortd(x) sort(x.begin(), x.end(), std::greater<long long>())
#define debug(x) std::cerr << (x) << std::endl;
#define roll(x)                                                                \
  for (auto &&itr : x) {                                                       \
    cerr << (itr) << " ";                                                      \
  }

ll inf = 10000000000;

template <class T> inline void chmax(T &ans, T t) {
  if (t > ans)
    ans = t;
}
template <class T> inline void chmin(T &ans, T t) {
  if (t < ans)
    ans = t;
}

template <class T> void warshall_floyd(vector<vector<T>> &cost, int N) {
  for (ll k = 0; k < N; ++k) {
    for (ll i = 0; i < N; ++i) {
      for (ll j = 0; j < N; ++j) {
        cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j]);
      }
    }
  }
}

/* END */

/*
int main() {
  int h,w;
  cin >> h >> w;

  vector< vector<long long> > cost(10, vector<long long>(10));

  for (int i = 0; i < 10; ++i)
    for (int j = 0; j < 10; ++j)
      cin >> cost[i][j];

  int wall[h][w];
  for (int i = 0; i < h; ++i)
    for (int j = 0; j < w; ++j)
      cin >> wall[i][j];

  floyd_wardhall(cost, 10);

  ll ans = 0;
  for (int i = 0; i < h; ++i) for (int j = 0; j < w; ++j) {
    if (wall[i][j] == -1 or wall[i][j] == 1) continue;
    ans += cost[wall[i][j]][1];
  }

  cout << ans << endl;
}*/

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M, L;
  cin >> N >> M >> L;

  vector<ll> a(M), b(M), c(M);
  vector<vector<ll>> fuel(N + 1, vector<ll>(N + 1, inf));
  vector<vector<ll>> dist(N + 1, vector<ll>(N + 1, inf));
  for (int i = 0; i < N + 1; ++i) {
    fuel[i][i] = 0;
    dist[i][i] = 0;
  }

  repr(i, M, 0) {
    cin >> a[i] >> b[i] >> c[i];

    fuel[a[i]][b[i]] = c[i];
    fuel[b[i]][a[i]] = c[i];
  }

  warshall_floyd(fuel, N + 1);
  for (int i = 0; i < N + 1; ++i) {
    for (int j = 0; j < N + 1; ++j) {
      if (fuel[i][j] <= L) {
        dist[i][j] = 1;
      }
    }
  }

  warshall_floyd(dist, N + 1);
  int Q;
  cin >> Q;
  repr(i, Q, 0) {
    int s, t;
    cin >> s >> t;
    if (dist[s][t] < inf) {
      cout << dist[s][t] - 1 << endl; // 最初から持っている燃料
    } else {
      cout << -1 << endl;
    }
  }
}