Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
const ll INF = 100000000000ll;
const long INF64 = 1000000000000000ll;
const ll MOD = 1000000007ll;

int main() {
  ll n, m, l;
  std::cin >> n >> m >> l;
  vector<ll> a(m), b(m), c(m);
  rep(i, m) std::cin >> a[i] >> b[i] >> c[i];
  ll q;
  std::cin >> q;
  vector<ll> s(q), t(q);
  rep(i, q) std::cin >> s[i] >> t[i];

  std::vector<std::vector<ll>> d(n, vector<ll>(n, INF));
  std::vector<std::vector<ll>> dd(n, vector<ll>(n, INF));
  rep(i, n) d[i][i] = 0ll;
  rep(i, m) {
    if (c[i] <= l) {
      d[a[i] - 1][b[i] - 1] = 1ll;
      d[b[i] - 1][a[i] - 1] = 1ll;
    }
  }

  rep(i, n) dd[i][i] = 0ll;
  rep(i, m) {
    if (c[i] <= l) {
      dd[a[i] - 1][b[i] - 1] = c[i];
      dd[b[i] - 1][a[i] - 1] = c[i];
    }
  }
  for (int i = 0; i < n; i++)     // 経由す頂点
    for (int j = 0; j < n; j++)   // 開始頂点
      for (int k = 0; k < n; k++) // 終端
        dd[j][k] = min(dd[j][k], dd[j][i] + dd[i][k]);
  rep(ii, 300) {
    for (int i = 0; i < n; i++)     // 経由す頂点
      for (int j = 0; j < n; j++)   // 開始頂点
        for (int k = 0; k < n; k++) // 終端
          d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

    rep(i, n) rep(j, n) {
      if (d[i][j] < INF && d[i][j] >= 1 && dd[i][j] <= l)
        d[i][j] = 1;
    }
  }

  rep(i, q) {
    if (d[s[i] - 1][t[i] - 1] == INF)
      std::cout << -1 << std::endl;
    else
      std::cout << max(d[s[i] - 1][t[i] - 1] - 1, 0ll) << std::endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define lb lower_bound
#define ub upper_bound
typedef long long ll;
const ll INF = 100000000000ll;
const long INF64 = 1000000000000000ll;
const ll MOD = 1000000007ll;

int main() {
  ll n, m, l;
  std::cin >> n >> m >> l;
  vector<ll> a(m), b(m), c(m);
  rep(i, m) std::cin >> a[i] >> b[i] >> c[i];
  ll q;
  std::cin >> q;
  vector<ll> s(q), t(q);
  rep(i, q) std::cin >> s[i] >> t[i];

  std::vector<std::vector<ll>> d(n, vector<ll>(n, INF));
  std::vector<std::vector<ll>> dd(n, vector<ll>(n, INF));
  rep(i, n) d[i][i] = 0ll;
  rep(i, m) {
    if (c[i] <= l) {
      d[a[i] - 1][b[i] - 1] = 1ll;
      d[b[i] - 1][a[i] - 1] = 1ll;
    }
  }

  rep(i, n) dd[i][i] = 0ll;
  rep(i, m) {
    if (c[i] <= l) {
      dd[a[i] - 1][b[i] - 1] = c[i];
      dd[b[i] - 1][a[i] - 1] = c[i];
    }
  }
  for (int i = 0; i < n; i++)     // 経由す頂点
    for (int j = 0; j < n; j++)   // 開始頂点
      for (int k = 0; k < n; k++) // 終端
        dd[j][k] = min(dd[j][k], dd[j][i] + dd[i][k]);
  rep(ii, 2) {
    for (int i = 0; i < n; i++)     // 経由す頂点
      for (int j = 0; j < n; j++)   // 開始頂点
        for (int k = 0; k < n; k++) // 終端
          d[j][k] = min(d[j][k], d[j][i] + d[i][k]);

    rep(i, n) rep(j, n) {
      if (d[i][j] < INF && d[i][j] >= 1 && dd[i][j] <= l)
        d[i][j] = 1;
    }
  }

  rep(i, q) {
    if (d[s[i] - 1][t[i] - 1] == INF)
      std::cout << -1 << std::endl;
    else
      std::cout << max(d[s[i] - 1][t[i] - 1] - 1, 0ll) << std::endl;
  }
}