Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
ll n, m, l;
vector<vector<pair<ll, ll>>> edges(400);
vector<vector<bool>> reals(400, vector<bool>(400, false));
vector<vector<ll>> costs(400, vector<ll>(400, -1));

void saiki(ll from, ll now, ll gas) {
  for (auto e : edges[now]) {
    ll to = e.first;
    ll cost = e.second;
    if (cost <= gas && gas - cost > costs[from][to]) {
      reals[from][to] = true;
      costs[from][to] = gas - cost;
      saiki(from, to, gas - cost);
    }
  }
}

int main() {

  std::cin >> n >> m >> l;

  for (int i = 0; i < m; i++) {
    ll a, b, c;
    std::cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back({b, c});
    edges[b].push_back({a, c});
  }

  for (int i = 0; i < n; i++) {
    saiki(i, i, l);
  }

  vector<vector<ll>> dis(400, vector<ll>(400, 1e18));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        dis[i][j] = 0;
      } else if (reals[i][j]) {
        dis[i][j] = 1;
        // std::cout << i<< " "<<j << std::endl;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
        // std::cout << dis[j][k] <<" "<<j<<" "<<k << std::endl;
      }
    }
  }

  ll q;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    ll s, t;
    std::cin >> s >> t;
    s--;
    t--;
    if (dis[s][t] == 1e18) {
      std::cout << -1 << std::endl;
    } else {
      std::cout << dis[s][t] - 1 << std::endl;
    }
  }
}


Fixed submission:
typedef long long ll;
#include <bits/stdc++.h>
using namespace std;
ll n, m, l;
vector<vector<pair<ll, ll>>> edges(400);
vector<vector<bool>> reals(400, vector<bool>(400, false));
vector<vector<ll>> costs(400, vector<ll>(400, -1));

void saiki(ll from, ll now, ll gas) {
  for (auto e : edges[now]) {
    ll to = e.first;
    ll cost = e.second;
    if (cost <= gas && gas - cost > costs[from][to]) {
      reals[from][to] = true;
      costs[from][to] = gas - cost;
      saiki(from, to, gas - cost);
    }
  }
}

int main() {

  std::cin >> n >> m >> l;

  for (int i = 0; i < m; i++) {
    ll a, b, c;
    std::cin >> a >> b >> c;
    a--;
    b--;
    edges[a].push_back({b, c});
    edges[b].push_back({a, c});
  }

  for (int i = 0; i < n; i++) {
    priority_queue<pair<ll, ll>> que;
    que.push({l, i});
    while (!que.empty()) {
      auto qt = que.top();
      que.pop();
      ll now = qt.second;
      ll gas = qt.first;

      for (auto e : edges[now]) {
        ll to = e.first;
        ll cost = e.second;
        if (cost <= gas && gas - cost > costs[i][to]) {
          reals[i][to] = true;
          costs[i][to] = gas - cost;
          que.push({gas - cost, to});
        }
      }
    }
  }

  vector<vector<ll>> dis(400, vector<ll>(400, 1e18));

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      if (i == j) {
        dis[i][j] = 0;
      } else if (reals[i][j]) {
        dis[i][j] = 1;
        // std::cout << i<< " "<<j << std::endl;
      }
    }
  }

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      for (int k = 0; k < n; k++) {
        dis[j][k] = min(dis[j][k], dis[j][i] + dis[i][k]);
        // std::cout << dis[j][k] <<" "<<j<<" "<<k << std::endl;
      }
    }
  }

  ll q;
  std::cin >> q;
  for (int i = 0; i < q; i++) {
    ll s, t;
    std::cin >> s >> t;
    s--;
    t--;
    if (dis[s][t] == 1e18) {
      std::cout << -1 << std::endl;
    } else {
      std::cout << dis[s][t] - 1 << std::endl;
    }
  }
}