Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll inf = 1001001001;
ll n, m, l, q;

class CompareDist {
public:
  bool operator()(pair<pair<ll, ll>, ll> n1, pair<pair<ll, ll>, ll> n2) {
    if (n1.first.first == n2.first.first)
      return n1.first.second > n2.first.second;
    return n1.first.first < n2.first.first;
  }
};

int main() {
  cin >> n >> m >> l;
  vector<vector<pair<ll, ll>>> route(n + 1);
  vector<vector<ll>> is(n + 1, vector<ll>(n + 1, 0));
  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    if (c > l)
      continue;
    route[a].push_back({b, c});
    route[b].push_back({a, c});
    is[a][b] = c;
    is[b][a] = c;
  }
  cin >> q;
  vector<vector<ll>> memo(n + 1, vector<ll>(n + 1, -10));
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    if (memo[s][t] != -10) {
      cout << memo[s][t] << endl;
      continue;
    }
    if (is[s][t] != 0) {
      cout << 0 << endl;
      memo[s][t] = 0;
      continue;
    }
    vector<pair<ll, ll>> cost(n + 1, {inf, 0});
    cost[s] = {0, l};
    priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>,
                   CompareDist>
        que;
    que.push({{0, l}, s});
    while (!que.empty()) {
      pair<pair<ll, ll>, ll> tmp = que.top();
      que.pop();
      int x = tmp.first.first, y = tmp.first.second, z = tmp.second;
      if (cost[z].first < x || (cost[z].first == x && cost[z].second > y))
        continue;
      for (pair<ll, ll> j : route[z]) {
        if (y >= j.second) {
          if (cost[j.first].first > x ||
              (cost[j.first].first == x &&
               cost[j.first].second < y - j.second)) {
            cost[j.first] = {x, y - j.second};
            que.push({{x, y - j.second}, j.first});
          }
        } else {
          if (cost[j.first].first > x + 1 ||
              (cost[j.first].first == x + 1 &&
               cost[j.first].second < l - j.second)) {
            cost[j.first] = {x + 1, l - j.second};
            que.push({{x + 1, l - j.second}, j.first});
          }
        }
      }
    }
    rep(j, n + 1) {
      if (cost[j].first == inf)
        cost[j].first = -1;
      memo[s][j] = cost[j].first;
      memo[j][s] = cost[j].first;
    }
    cout << cost[t].first << endl;
  }
}


Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

ll inf = 1001001001;
ll n, m, l, q;

class CompareDist {
public:
  bool operator()(pair<pair<ll, ll>, ll> n1, pair<pair<ll, ll>, ll> n2) {
    if (n1.first.first == n2.first.first)
      return n1.first.second < n2.first.second;
    return n1.first.first > n2.first.first;
  }
};

int main() {
  cin >> n >> m >> l;
  vector<vector<pair<ll, ll>>> route(n + 1);
  vector<vector<ll>> is(n + 1, vector<ll>(n + 1, 0));
  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    if (c > l)
      continue;
    route[a].push_back({b, c});
    route[b].push_back({a, c});
    is[a][b] = c;
    is[b][a] = c;
  }
  cin >> q;
  vector<vector<ll>> memo(n + 1, vector<ll>(n + 1, -10));
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    if (memo[s][t] != -10) {
      cout << memo[s][t] << endl;
      continue;
    }
    if (is[s][t] != 0) {
      cout << 0 << endl;
      memo[s][t] = 0;
      continue;
    }
    vector<pair<ll, ll>> cost(n + 1, {inf, 0});
    cost[s] = {0, l};
    priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>,
                   CompareDist>
        que;
    que.push({{0, l}, s});
    while (!que.empty()) {
      pair<pair<ll, ll>, ll> tmp = que.top();
      que.pop();
      int x = tmp.first.first, y = tmp.first.second, z = tmp.second;
      if (cost[z].first < x || (cost[z].first == x && cost[z].second > y))
        continue;
      for (pair<ll, ll> j : route[z]) {
        if (y >= j.second) {
          if (cost[j.first].first > x ||
              (cost[j.first].first == x &&
               cost[j.first].second < y - j.second)) {
            cost[j.first] = {x, y - j.second};
            que.push({{x, y - j.second}, j.first});
          }
        } else {
          if (cost[j.first].first > x + 1 ||
              (cost[j.first].first == x + 1 &&
               cost[j.first].second < l - j.second)) {
            cost[j.first] = {x + 1, l - j.second};
            que.push({{x + 1, l - j.second}, j.first});
          }
        }
      }
    }
    rep(j, n + 1) {
      if (cost[j].first == inf)
        cost[j].first = -1;
      memo[s][j] = cost[j].first;
      memo[j][s] = cost[j].first;
    }
    cout << cost[t].first << endl;
  }
}