Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
// 2020-07-24 07:52:55
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

using P = pair<int, int>;
const int INF = 1e9 + 5;
int G[300][300]; // cost
P dp[300][300];  // supply, used
int n, m, l;
void bfs(int s) {
  using T = tuple<int, int, int>; // supply, used, to
  priority_queue<T, vector<T>, greater<T>> que;
  que.emplace(T(0, 0, s));
  while (!que.empty()) {
    int supply, used, to;
    tie(supply, used, to) = que.top();
    que.pop();
    if (chmin(dp[s][to], P(supply, used))) {
      rep(v, n) if (G[to][v] <= l) {
        if (used + G[to][v] <= l) {
          que.emplace(T(supply, used + G[to][v], v));
        } else {
          que.emplace(T(supply + 1, G[to][v], v));
        }
      }
    }
  }
}
void answer() {
  cin >> n >> m >> l;
  rep(i, n) rep(j, n) {
    G[i][j] = INF;
    dp[i][j] = P(INF, 0);
    if (i == j)
      G[i][j] = 0;
  }
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a][b] = c;
    G[b][a] = c;
  }
  rep(i, n) bfs(i);
  int q;
  cin >> q;
  rep(qi, q) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (dp[s][t].first == INF)
      cout << -1 << '\n';
    else
      cout << dp[s][t].first << '\n';
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}

Fixed submission:
// 2020-07-24 07:52:55
#include <bits/stdc++.h>
#ifdef LOCAL
#include "lib/debug.hpp"
#else
#define debug(...) 1
#endif
#define ALL(a) (a).begin(), (a).end()
#define rep(i, n) REP(i, 0, (n))
#define repc(i, n) REPC(i, 0, (n))
#define REP(i, n, m) for (int i = (int)(n); i < (int)(m); i++)
#define REPC(i, n, m) for (int i = (int)(n); i <= (int)(m); i++)
#define REPCM(i, n, m) for (int i = (int)(n); i >= (int)(m); i--)
using namespace std;
using ll = long long;
using ld = long double;
using pr = pair<ll, ll>;
using vll = vector<ll>;
using vpr = vector<pr>;
template <class T> inline bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> inline bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

using P = pair<int, int>;
const int INF = 1e9 + 5;
int G[300][300]; // cost
P dp[300][300];  // supply, used
int n, m, l;
void bfs(int s) {
  using T = tuple<int, int, int>; // supply, used, to
  priority_queue<T, vector<T>, greater<T>> que;
  que.emplace(T(0, 0, s));
  while (!que.empty()) {
    int supply, used, to;
    tie(supply, used, to) = que.top();
    que.pop();
    if (chmin(dp[s][to], P(supply, used))) {
      rep(v, n) if (G[to][v] <= l) {
        int nsupply = supply, nused = used + G[to][v];
        if (nused > l) {
          nsupply++;
          nused = G[to][v];
        }
        if (dp[s][v] > P(nsupply, nused)) {
          que.emplace(T(nsupply, nused, v));
        }
      }
    }
  }
}
void answer() {
  cin >> n >> m >> l;
  rep(i, n) rep(j, n) {
    G[i][j] = INF;
    dp[i][j] = P(INF, 0);
    if (i == j)
      G[i][j] = 0;
  }
  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    G[a][b] = c;
    G[b][a] = c;
  }
  rep(i, n) bfs(i);
  int q;
  cin >> q;
  rep(qi, q) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (dp[s][t].first == INF)
      cout << -1 << '\n';
    else
      cout << dp[s][t].first << '\n';
  }
}
int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  answer();
  return 0;
}