Problem p02889 (C++)

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;

void solve() {
  int AZ = 1000000001;

  int n, m;
  cin >> n >> m;
  int L;
  cin >> L;
  queue<pair<int, int>> st;
  int l[300][300];
  rep(i, n) rep(j, n) l[i][j] = AZ;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int c;
    cin >> c;
    if (c <= L)
      l[a][b] = l[b][a] = c;
    st.push({a, b});
    rep(i, n) st.push({b, a});
  }
  rep(i, n) l[i][i] = 0;
  while (!st.empty()) {
    auto p = st.front();
    st.pop();
    int d = l[p.first][p.second];
    rep(i, n) {
      int dd = l[p.second][i] + d;
      if (dd > L)
        continue;
      if (l[p.first][i] <= dd)
        continue;
      l[p.first][i] = dd;
      st.push({p.first, i});
    }
  }

  int dp[300][300];
  rep(i, n) rep(j, n) {
    if (l[i][j] <= L) {
      st.push({i, j});
      dp[i][j] = 0;
    } else
      dp[i][j] = AZ;
  }
  rep(i, n) dp[i][i] = 0;
  while (!st.empty()) {
    auto p = st.front();
    st.pop();
    int d = dp[p.first][p.second];
    rep(i, n) {
      if (dp[p.second][i] != 0)
        continue;
      if (dp[p.first][i] <= d + 1)
        continue;
      dp[p.first][i] = d + 1;
      st.push({p.first, i});
    }
  }

  int q;
  cin >> q;
  vector<int> ans;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (dp[s][t] == AZ)
      ans.push_back(-1);
    else
      ans.push_back(dp[s][t]);
  }
  rep(i, ans.size()) { cout << ans[i] << endl; }
}

int main() {

  solve();

  return 0;
}

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

#define rep(i, n) for (int i = 0; i < (n); i++)
using ULL = unsigned long long;

void solve() {
  int AZ = 1000000001;

  int n, m;
  cin >> n >> m;
  int L;
  cin >> L;
  queue<pair<int, int>> st;
  int l[300][300];
  rep(i, n) rep(j, n) l[i][j] = AZ;
  rep(i, m) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    int c;
    cin >> c;
    if (c <= L)
      l[a][b] = l[b][a] = c;
    st.push({a, b});
    st.push({b, a});
  }
  rep(i, n) l[i][i] = 0;
  while (!st.empty()) {
    auto p = st.front();
    st.pop();
    int d = l[p.first][p.second];
    rep(i, n) {
      int dd = l[p.second][i] + d;
      if (dd > L)
        continue;
      if (l[p.first][i] <= dd)
        continue;
      l[p.first][i] = dd;
      st.push({p.first, i});
    }
  }

  int dp[300][300];
  rep(i, n) rep(j, n) {
    if (l[i][j] <= L) {
      st.push({i, j});
      dp[i][j] = 0;
    } else
      dp[i][j] = AZ;
  }
  rep(i, n) dp[i][i] = 0;
  while (!st.empty()) {
    auto p = st.front();
    st.pop();
    int d = dp[p.first][p.second];
    rep(i, n) {
      if (dp[p.second][i] != 0)
        continue;
      if (dp[p.first][i] <= d + 1)
        continue;
      dp[p.first][i] = d + 1;
      st.push({p.first, i});
    }
  }

  int q;
  cin >> q;
  vector<int> ans;
  rep(i, q) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    if (dp[s][t] == AZ)
      ans.push_back(-1);
    else
      ans.push_back(dp[s][t]);
  }
  rep(i, ans.size()) { cout << ans[i] << endl; }
}

int main() {

  solve();

  return 0;
}