Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(x, y) '(' << x << ',' << y << ')'
ll MOD = 1000000007;
ll INF = 214748364700000000;
double EPS = 1e-12;
double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

ll N, M, L;
Graph G(310);
pair<ll, ll> fuel[310][310];

void dijkstra(ll s, ll e = L) {
  rep(i, N) {
    fuel[s][i].first = INF;
    fuel[s][i].second = -1;
  }
  ll cnt = 0;
  map<pair<ll, ll>, ll> mp;
  priority_queue<tuple<ll, ll, ll>> Q;
  bool done[310];
  fill(done, done + 310, false);
  done[s] = true;
  Q.emplace(0, e, s);
  while (Q.size()) {
    // cnt++;
    if (cnt > N) {
      break;
    }
    ll C, E, X;
    tie(C, E, X) = Q.top();
    Q.pop();
    ll n = G[X].size();
    done[X] = true;
    rep(i, n) {
      ll Y = G[X][i].first, D = G[X][i].second;
      if (done[Y])
        continue;
      if (E >= D) {
        if (fuel[s][Y].first > -C) {
          Q.emplace(C, E - D, Y);
          fuel[s][Y].first = min(fuel[s][Y].first, -C);
          fuel[s][Y].second = E - D;
        } else if (fuel[s][Y].first == -C && fuel[s][Y].second < E - D) {
          Q.emplace(C, E - D, Y);
          fuel[s][Y].second = E - D;
          fuel[s][Y].first = min(fuel[s][Y].first, -C);
        }
      } else {
        if (fuel[s][Y].first > -C + 1) {
          Q.emplace(C - 1, L - D, Y);
          fuel[s][Y].first = min(fuel[s][Y].first, -C + 1);
          fuel[s][Y].second = L - D;
        } else if (fuel[s][Y].first == -C + 1 && fuel[s][Y].second < L - D) {
          Q.emplace(C - 1, L - D, Y);
          fuel[s][Y].second = L - D;
          fuel[s][Y].first = min(fuel[s][Y].first, -C + 1);
        }
      }
    }
  }
  return;
}

int main() {
  cin >> N >> M >> L;
  rep(i, M) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c > L)
      continue;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  rep(i, N) { dijkstra(i); }
  ll q;
  cin >> q;
  vector<ll> ans(q);
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    ans[i] = fuel[s][t].first;
    if (fuel[s][t].first == INF)
      ans[i] = -1;
  }
  rep(i, q) { cout << ans[i] << endl; }
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; i++)
#define RFOR(i, a, n) for (ll i = (ll)n - 1; i >= (ll)a; i--)
#define rep(i, n) FOR(i, 0, n)
#define rrep(i, n) RFOR(i, 0, n)
#define ALL(v) v.begin(), v.end()
#define bra(x, y) '(' << x << ',' << y << ')'
ll MOD = 1000000007;
ll INF = 214748364700000000;
double EPS = 1e-12;
double PI = 3.141592653589793238;
template <typename T> void remove(std::vector<T> &vector, unsigned int index) {
  vector.erase(vector.begin() + index);
}

using Graph = vector<vector<pair<ll, ll>>>;

ll N, M, L;
Graph G(310);
pair<ll, ll> fuel[310][310];

void dijkstra(ll s, ll e = L) {
  rep(i, N) {
    fuel[s][i].first = INF;
    fuel[s][i].second = -1;
  }
  ll cnt = 0;
  map<pair<ll, ll>, ll> mp;
  priority_queue<tuple<ll, ll, ll>> Q;
  bool done[310];
  fill(done, done + 310, false);
  done[s] = true;
  Q.emplace(0, e, s);
  while (Q.size()) {
    cnt++;
    if (cnt > 3 * N) {
      break;
    }
    ll C, E, X;
    tie(C, E, X) = Q.top();
    Q.pop();
    ll n = G[X].size();
    done[X] = true;
    rep(i, n) {
      ll Y = G[X][i].first, D = G[X][i].second;
      if (done[Y])
        continue;
      if (E >= D) {
        if (fuel[s][Y].first > -C) {
          Q.emplace(C, E - D, Y);
          fuel[s][Y].first = min(fuel[s][Y].first, -C);
          fuel[s][Y].second = E - D;
        } else if (fuel[s][Y].first == -C && fuel[s][Y].second < E - D) {
          Q.emplace(C, E - D, Y);
          fuel[s][Y].second = E - D;
          fuel[s][Y].first = min(fuel[s][Y].first, -C);
        }
      } else {
        if (fuel[s][Y].first > -C + 1) {
          Q.emplace(C - 1, L - D, Y);
          fuel[s][Y].first = min(fuel[s][Y].first, -C + 1);
          fuel[s][Y].second = L - D;
        } else if (fuel[s][Y].first == -C + 1 && fuel[s][Y].second < L - D) {
          Q.emplace(C - 1, L - D, Y);
          fuel[s][Y].second = L - D;
          fuel[s][Y].first = min(fuel[s][Y].first, -C + 1);
        }
      }
    }
  }
  return;
}

int main() {
  cin >> N >> M >> L;
  rep(i, M) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c > L)
      continue;
    G[a].emplace_back(b, c);
    G[b].emplace_back(a, c);
  }
  rep(i, N) { dijkstra(i); }
  ll q;
  cin >> q;
  vector<ll> ans(q);
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    ans[i] = fuel[s][t].first;
    if (fuel[s][t].first == INF)
      ans[i] = -1;
  }
  rep(i, q) { cout << ans[i] << endl; }
}