Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, M, L;
  cin >> N >> M >> L;
  vector<vector<pii>> A(N), F(N, vector<pii>(N, mp(INF, L)));
  rep(i, 0, M) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (L >= Z) {
      A[X - 1].pb(mp(Y - 1, Z));
      A[Y - 1].pb(mp(X - 1, Z));
    }
  }
  rep(i, 0, N) {
    priority_queue<pair<pii, int>, vector<pair<pii, int>>,
                   greater<pair<pii, int>>>
        Q;
    Q.push(mp(mp(0, 0), i));
    F[i][i] = mp(0, 0);
    while (Q.size() > 0) {
      int X = Q.top().second, Y = Q.top().first.first, Z = Q.top().first.second;
      Q.pop();
      rep(j, 0, A[X].size()) {
        int P = A[X][j].first, R = A[X][j].second;
        if (P != X) {
          int B = Z + R, C = Y;
          if (B > L) {
            B = R;
            C++;
          }
          if (F[i][P] > mp(C, B)) {
            F[i][P] = mp(C, B);
            Q.push(mp(mp(C, B), P));
          }
        }
      }
    }
  }
  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int X, Y;
    cin >> X >> Y;
    int ans = F[X - 1][Y - 1].first;
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define rep(i, j, n) for (int i = (int)(j); i < (int)(n); i++)
#define REP(i, j, n) for (int i = (int)(j); i <= (int)(n); i++)
#define MOD 1000000007
#define int long long
#define ALL(a) (a).begin(), (a).end()
#define vi vector<int>
#define vii vector<vi>
#define pii pair<int, int>
#define priq priority_queue<int>
#define disup(A, key) distance(A.begin(), upper_bound(ALL(A), (int)(key)))
#define dislow(A, key) distance(A.begin(), lower_bound(ALL(A), (int)(key)))
#define tii tuple<int, int, int>
#define Priq priority_queue<int, vi, greater<int>>
#define pb push_back
#define mp make_pair
#define INF (1ll << 60)
signed main() {
  int N, M, L;
  cin >> N >> M >> L;
  vector<vector<pii>> A(N), F(N, vector<pii>(N, mp(INF, L)));
  rep(i, 0, M) {
    int X, Y, Z;
    cin >> X >> Y >> Z;
    if (L >= Z) {
      A[X - 1].pb(mp(Y - 1, Z));
      A[Y - 1].pb(mp(X - 1, Z));
    }
  }
  rep(i, 0, N) {
    priority_queue<pair<pii, int>, vector<pair<pii, int>>,
                   greater<pair<pii, int>>>
        Q;
    Q.push(mp(mp(0, 0), i));
    F[i][i] = mp(0, 0);
    while (Q.size() > 0) {
      int X = Q.top().second, Y = Q.top().first.first, Z = Q.top().first.second;
      Q.pop();
      if (mp(Y, Z) > F[i][X])
        continue;
      rep(j, 0, A[X].size()) {
        int P = A[X][j].first, R = A[X][j].second;
        if (P != X) {
          int B = Z + R, C = Y;
          if (B > L) {
            B = R;
            C++;
          }
          if (F[i][P] > mp(C, B)) {
            F[i][P] = mp(C, B);
            Q.push(mp(mp(C, B), P));
          }
        }
      }
    }
  }
  int Q;
  cin >> Q;
  rep(i, 0, Q) {
    int X, Y;
    cin >> X >> Y;
    int ans = F[X - 1][Y - 1].first;
    if (ans == INF)
      ans = -1;
    cout << ans << endl;
  }
}