Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define PI 3.1415926535897932384626433832795
#define MOD (1000000007)
#define EPS (1e-15)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define bet(x, a, b) (a) <= (x) && (x) < (b)
typedef long long ll;
typedef unsigned long long ull;

#define NMAX (300)
#define p(a, b) make_pair(a, b)
typedef pair<int, int> pi;

struct pc {
  int chg;
  int rem;
  int idx;
};

int N, M, L, Q;
int s[NMAX * (NMAX - 1)];
int t[NMAX * (NMAX - 1)];

vector<pi> G[NMAX + 1];
bool done[NMAX + 1] = {false};
int ans[NMAX + 1][NMAX + 1];

void setAns(int root) {
  auto comp = [](pc &a, pc &b) {
    return (a.chg != b.chg ? a.chg > b.chg : a.rem < b.rem);
  };

  priority_queue<pc, vector<pc>, decltype(comp)> pq{comp};
  pc item;

  repc(i, 1, N) done[i] = false;

  item.chg = 0, item.rem = L, item.idx = root;
  pq.push(item);

  while (!pq.empty()) {
    item = pq.top();
    pq.pop();
    int chg = item.chg;
    int rem = item.rem;
    int current = item.idx;
    int size = G[current].size();

    if (done[current])
      continue;
    done[current] = true;
    ans[root][current] = chg;

    rep(i, size) {
      pi info = G[current][i];
      int next = info.first;
      int len = info.second;

      if (rem >= len) {
        item.chg = chg;
        item.rem = rem - len;
      } else {
        item.chg = chg + 1;
        item.rem = L - len;
      }
      item.idx = next;
      pq.push(item);
    }
  }
}

int main() {
  int A, B, C;

  cin >> N >> M >> L;
  rep(i, M) {
    cin >> A >> B >> C;
    if (C > L)
      continue;
    G[A].push_back(p(B, C));
    G[B].push_back(p(A, C));
  }
  cin >> Q;
  rep(i, Q) cin >> s[i] >> t[i];

  repc(i, 1, N) repc(j, 1, N) ans[i][j] = -1;
  repc(i, 1, N) setAns(i);

  rep(i, Q) cout << ans[s[i]][t[i]] << endl;

  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
#define PI 3.1415926535897932384626433832795
#define MOD (1000000007)
#define EPS (1e-15)
#define rep(i, n) for (int i = 0; i < (n); i++)
#define repc(i, s, n) for (int i = (s); i <= (n); i++)
#define rrep(i, n) for (int i = (n)-1; i >= 0; i--)
#define rrepc(i, s, n) for (int i = (s); i >= (n); i--)
#define bet(x, a, b) (a) <= (x) && (x) < (b)
typedef long long ll;
typedef unsigned long long ull;

#define NMAX (300)
#define p(a, b) make_pair(a, b)
typedef pair<int, int> pi;

struct pc {
  int chg;
  int rem;
  int idx;
};

int N, M, L, Q;
int s[NMAX * (NMAX - 1)];
int t[NMAX * (NMAX - 1)];

vector<pi> G[NMAX + 1];
bool done[NMAX + 1] = {false};
int ans[NMAX + 1][NMAX + 1];

void setAns(int root) {
  auto comp = [](pc &a, pc &b) {
    return (a.chg != b.chg ? a.chg > b.chg : a.rem < b.rem);
  };

  priority_queue<pc, vector<pc>, decltype(comp)> pq{comp};
  pc item;

  repc(i, 1, N) done[i] = false;

  item.chg = 0, item.rem = L, item.idx = root;
  pq.push(item);

  while (!pq.empty()) {
    item = pq.top();
    pq.pop();
    int chg = item.chg;
    int rem = item.rem;
    int current = item.idx;
    int size = G[current].size();

    if (done[current])
      continue;
    done[current] = true;
    ans[root][current] = chg;

    rep(i, size) {
      pi info = G[current][i];
      int next = info.first;
      int len = info.second;

      if (done[next])
        continue;

      if (rem >= len) {
        item.chg = chg;
        item.rem = rem - len;
      } else {
        item.chg = chg + 1;
        item.rem = L - len;
      }
      item.idx = next;
      pq.push(item);
    }
  }
}

int main() {
  int A, B, C;

  cin >> N >> M >> L;
  rep(i, M) {
    cin >> A >> B >> C;
    if (C > L)
      continue;
    G[A].push_back(p(B, C));
    G[B].push_back(p(A, C));
  }
  cin >> Q;
  rep(i, Q) cin >> s[i] >> t[i];

  repc(i, 1, N) repc(j, 1, N) ans[i][j] = -1;
  repc(i, 1, N) setAns(i);

  rep(i, Q) cout << ans[s[i]][t[i]] << endl;

  return 0;
}