Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define MP make_pair
#define mset(x) memset(x, 0, sizeof(x));
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define inf 10000000000000007
const int N = 304;
ll ans[N][N];
ll vis[N];
vector<pair<ll, ll>> adj[N];
priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>,
               greater<pair<pair<ll, ll>, ll>>>
    pq;
void run() {
  ll n, m, k, u, v, w, i, q;
  cin >> n >> m >> k;
  while (m--) {
    cin >> u >> v >> w;
    adj[u].pb(MP(v, w));
    adj[v].pb(MP(u, w));
  }

  memset(ans, -1, sizeof ans);
  for (i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    pq.push(MP(MP(0, -k), i));
    while (!pq.empty()) {
      pair<pair<ll, ll>, ll> p = pq.top();
      u = p.S;
      pq.pop();
      if (vis[u])
        continue;
      vis[u] = 1;
      ans[i][u] = p.F.F;
      for (auto it : adj[u]) {
        v = it.F;
        w = it.S;
        if (w <= -p.F.S)
          pq.push(MP(MP(p.F.F, -(-p.F.S - w)), v));
        else if (w <= k)
          pq.push(MP(MP(p.F.F + 1, -(k - w)), v));
      }
    }
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    cout << ans[u][v] << "\n";
  }
}

void setup() {
  ios;
  // cout << setprecision(20);
  //  #ifndef ONLINE_JUDGE
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
  //  #endif
}

signed main() {
  setup();
  int tests = 1;
  // cin >> tests;
  for (int i = 1; i <= tests; i++)
    run();
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
#define ll int
#define ld long double
#define pii pair<int, int>
#define pld pair<ld, ld>
#define all(v) v.begin(), v.end()
#define F first
#define S second
#define mod 1000000007
#define pb push_back
#define MP make_pair
#define mset(x) memset(x, 0, sizeof(x));
#define ios                                                                    \
  ios_base::sync_with_stdio(false);                                            \
  cin.tie(0);                                                                  \
  cout.tie(0);
#define inf 10000000000000007
const int N = 304;
ll ans[N][N];
ll vis[N];
vector<pair<ll, ll>> adj[N];
priority_queue<pair<pair<ll, ll>, ll>, vector<pair<pair<ll, ll>, ll>>,
               greater<pair<pair<ll, ll>, ll>>>
    pq;
void run() {
  ll n, m, k, u, v, w, i, q;
  cin >> n >> m >> k;
  while (m--) {
    cin >> u >> v >> w;
    adj[u].pb(MP(v, w));
    adj[v].pb(MP(u, w));
  }

  memset(ans, -1, sizeof ans);
  for (i = 1; i <= n; i++) {
    memset(vis, 0, sizeof vis);
    pq.push(MP(MP(0, -k), i));
    while (!pq.empty()) {
      pair<pair<ll, ll>, ll> p = pq.top();
      u = p.S;
      pq.pop();
      if (vis[u])
        continue;
      vis[u] = 1;
      ans[i][u] = p.F.F;
      for (auto it : adj[u]) {
        v = it.F;
        w = it.S;
        if (vis[v])
          continue;
        if (w <= -p.F.S)
          pq.push(MP(MP(p.F.F, -(-p.F.S - w)), v));
        else if (w <= k)
          pq.push(MP(MP(p.F.F + 1, -(k - w)), v));
      }
    }
  }
  cin >> q;
  while (q--) {
    cin >> u >> v;
    cout << ans[u][v] << "\n";
  }
}

void setup() {
  ios;
  // cout << setprecision(20);
  //  #ifndef ONLINE_JUDGE
  //  freopen("input", "r", stdin);
  //  freopen("output", "w", stdout);
  //  #endif
}

signed main() {
  setup();
  int tests = 1;
  // cin >> tests;
  for (int i = 1; i <= tests; i++)
    run();
  return 0;
}