Problem p02889 (C++)

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

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

// #define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
const long long INF = 1LL << 60;

// const long long INF = 1LL << 60;

ll n, m, l;

template <typename T> struct Edge {
  int to;
  T cost;
};

// Dijkstra
template <typename T> class Dijkstra {
private:
  vector<vector<Edge<T>>> G;
  int num_v;

public:
  Dijkstra(int num_v) : G(num_v), num_v(num_v) {}

  void add_edge(int from, int to, T cost) {
    G[from].push_back((Edge<T>){to, cost});
  }

  // start: Start point
  // result: result
  void solve(int start, vector<T> &result) {
    vector<int> tmp;
    solve(start, result, tmp);
  }

  void solve(int start, vector<T> &result, vector<int> &prev) {
    result.resize(num_v, make_pair(INF, INF));
    prev.resize(num_v, -1);

    // get<0>(T): cost
    // get<1>(T): index
    // get<2>(T): ... ( for extent)
    using Tup = tuple<T, int>;
    priority_queue<Tup, vector<Tup>, greater<Tup>> que;

    que.emplace(make_pair(0ll, 0ll), start);
    result[start] = make_pair(0ll, 0ll);

    while (!que.empty()) {
      Tup p = que.top();
      que.pop();

      T t = get<0>(p);
      int v = get<1>(p);

      // vの各辺に対しよりコストの低い経路があるかを確認
      for (auto &e : G[v]) {
        auto pr = result[v];
        pr.second += e.cost.second;
        if (pr.second > l) {
          pr.first++;
          pr.second = e.cost.second;
        }
        if (result[e.to] > pr) {
          prev[e.to] = v;
          result[e.to] = pr;
          que.emplace(pr, e.to);
        }
      }
    }
  }

  // t: 目的地
  vector<int> get_path(const vector<int> &prev, int t) {
    vector<int> path;

    for (int v = t; v != -1; v = prev[v]) {
      path.push_back(v);
    }

    reverse(path.begin(), path.end());
    return path;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  cin >> n >> m >> l;

  Dijkstra<P> g(n + 1);

  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    if (c <= l) {
      auto p = make_pair(0ll, c);
      g.add_edge(a, b, p);
      g.add_edge(b, a, p);
    }
  }

  vector<vector<P>> ans(n + 1);
  REP(i, n + 1) { g.solve(i, ans[i]); }

  ll q;
  cin >> q;
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    ll v = ans[s][t].first;
    if (v == INF) {
      v = -1;
    }
    cout << v << endl;
  }

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;

using ll = long long;
using ld = long double;

using vi = vector<int>;
using vvi = vector<vi>;
using vll = vector<ll>;
using vvll = vector<vll>;
using vb = vector<bool>;
using vs = vector<string>;
using vld = vector<ld>;
using vvld = vector<vld>;

typedef pair<ll, ll> P;

#define bit(n) (1LL << (n))

// #define int long long

#define all(v) v.begin(), v.end()

#define rep(i, n) for (ll i = 0; i < n; i++)
#define REP(i, n) for (ll i = 1; i < n; i++)

#define FOR(i, a, b) for (ll i = (a); i < (b); i++)
#define FORm(i, m) for (auto i = m.begin(); i != m.end(); i++)

template <class T> inline void chmax(T &a, T b) { a = std::max(a, b); }
template <class T> inline void chmin(T &a, T b) { a = std::min(a, b); }

#define mod (ll)(1e9 + 7)
const long long INF = 1LL << 60;

// const long long INF = 1LL << 60;

ll n, m, l;

template <typename T> struct Edge {
  int to;
  T cost;
};

// Dijkstra
template <typename T> class Dijkstra {
private:
  vector<vector<Edge<T>>> G;
  int num_v;

public:
  Dijkstra(int num_v) : G(num_v), num_v(num_v) {}

  void add_edge(int from, int to, T cost) {
    G[from].push_back((Edge<T>){to, cost});
  }

  // start: Start point
  // result: result
  void solve(int start, vector<T> &result) {
    vector<int> tmp;
    solve(start, result, tmp);
  }

  void solve(int start, vector<T> &result, vector<int> &prev) {
    result.resize(num_v, make_pair(INF, INF));
    prev.resize(num_v, -1);

    // get<0>(T): cost
    // get<1>(T): index
    // get<2>(T): ... ( for extent)
    using Tup = tuple<T, int>;
    priority_queue<Tup, vector<Tup>, greater<Tup>> que;

    que.emplace(make_pair(0ll, 0ll), start);
    result[start] = make_pair(0ll, 0ll);

    while (!que.empty()) {
      Tup p = que.top();
      que.pop();

      T t = get<0>(p);
      int v = get<1>(p);

      if (result[v] != t) {
        continue;
      }

      // vの各辺に対しよりコストの低い経路があるかを確認
      for (auto &e : G[v]) {
        auto pr = result[v];
        pr.second += e.cost.second;
        if (pr.second > l) {
          pr.first++;
          pr.second = e.cost.second;
        }
        if (result[e.to] > pr) {
          prev[e.to] = v;
          result[e.to] = pr;
          que.emplace(pr, e.to);
        }
      }
    }
  }

  // t: 目的地
  vector<int> get_path(const vector<int> &prev, int t) {
    vector<int> path;

    for (int v = t; v != -1; v = prev[v]) {
      path.push_back(v);
    }

    reverse(path.begin(), path.end());
    return path;
  }
};

signed main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  cin >> n >> m >> l;

  Dijkstra<P> g(n + 1);

  rep(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    if (c <= l) {
      auto p = make_pair(0ll, c);
      g.add_edge(a, b, p);
      g.add_edge(b, a, p);
    }
  }

  vector<vector<P>> ans(n + 1);
  REP(i, n + 1) { g.solve(i, ans[i]); }

  ll q;
  cin >> q;
  rep(i, q) {
    ll s, t;
    cin >> s >> t;
    ll v = ans[s][t].first;
    if (v == INF) {
      v = -1;
    }
    cout << v << endl;
  }

  return 0;
}