Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

pair<ll, ll> alldp[303][303];
bool allused[303][303];
vector<pair<ll, ll>> toc[303];

const ll inf = (1LL << 50);

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n, m, l;
  cin >> n >> m >> l;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    toc[a].emplace_back(b, c);
    toc[b].emplace_back(a, c);
  }

  rep(i, n) {
    int s = i;
    bool *used = (allused[i]);
    pair<ll, ll> *dp = (alldp[i]);
    rep(k, n) dp[k] = make_pair(inf, inf);

    rep(j, n) { used[j] = false; }

    auto comp = [](const tuple<ll, ll, ll> &a, const tuple<ll, ll, ll> &b) {
      ll a1, a2, a3, b1, b2, b3;
      tie(a1, a2, a3) = a;
      tie(b1, b2, b3) = b;
      if (a1 == b1) {
        return b2 > a2;
      } else
        return a1 > b1;
    };

    priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, decltype(comp)>
        que(comp);
    que.emplace(0, l, s);

    while (!que.empty()) {
      int x, y, z;
      tie(x, y, z) = que.top();
      que.pop();
      if (used[z])
        continue;
      used[z] = true;
      dp[z] = make_pair(x, y);

      for (auto &p : toc[z]) {
        int dist = p.second;
        int j = p.first;
        int nx = x, ny = y;
        if (dist > l)
          continue;
        else if (dist > ny) {
          nx++;
          ny = l;
        }
        ny -= dist;
        que.emplace(nx, ny, j);
      }
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    s--, t--;

    if (alldp[s][t].second == inf) {
      cout << -1 << endl;
    } else {
      cout << alldp[s][t].first << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

#define rep(i, j) for (int i = 0; i < j; i++)
#define all(obj) (obj).begin(), (obj).end()
#define rall(obj) (obj).rbegin(), (obj).rend()
typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vi> vvi;
typedef vector<pii> vpii;

pair<ll, ll> alldp[303][303];
bool allused[303][303];
vector<pair<ll, ll>> toc[303];

const ll inf = (1LL << 50);

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.precision(10);
  cout << fixed;

  int n, m, l;
  cin >> n >> m >> l;

  rep(i, m) {
    int a, b, c;
    cin >> a >> b >> c;
    a--, b--;
    toc[a].emplace_back(b, c);
    toc[b].emplace_back(a, c);
  }

  rep(i, n) {
    int s = i;
    bool *used = (allused[i]);
    pair<ll, ll> *dp = (alldp[i]);
    rep(k, n) dp[k] = make_pair(inf, inf);

    rep(j, n) { used[j] = false; }

    auto comp = [](const tuple<ll, ll, ll> &a, const tuple<ll, ll, ll> &b) {
      ll a1, a2, a3, b1, b2, b3;
      tie(a1, a2, a3) = a;
      tie(b1, b2, b3) = b;
      if (a1 == b1) {
        return b2 > a2;
      } else
        return a1 > b1;
    };

    priority_queue<tuple<ll, ll, ll>, vector<tuple<ll, ll, ll>>, decltype(comp)>
        que(comp);
    que.emplace(0, l, s);

    while (!que.empty()) {
      int x, y, z;
      tie(x, y, z) = que.top();
      que.pop();
      if (used[z])
        continue;
      used[z] = true;
      dp[z] = make_pair(x, y);

      for (auto &p : toc[z]) {
        int dist = p.second;
        int j = p.first;
        if (used[j])
          continue;
        int nx = x, ny = y;
        if (dist > l)
          continue;
        else if (dist > ny) {
          nx++;
          ny = l;
        }
        ny -= dist;
        que.emplace(nx, ny, j);
      }
    }
  }

  int q;
  cin >> q;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    s--, t--;

    if (alldp[s][t].second == inf) {
      cout << -1 << endl;
    } else {
      cout << alldp[s][t].first << endl;
    }
  }

  return 0;
}