Problem p02889 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define MOD 1000000007
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// グラフ関連
struct Edge { // グラフ
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void Dijkstra(Graph &G, ll s, Array &d, Array &u, ll l) { // O(|E|log|V|)
  d.resize(G.size());
  u.resize(G.size());
  REP(i, d.size()) d[i] = INF;
  REP(i, u.size()) u[i] = INF;
  d[s] = 0;
  u[s] = 0;
  vector<ll> q(G.size());
  REP(i, G.size()) q.push_back(i);
  while (q.size() > 0) {
    ll uu = INF;
    ll dd = INF;
    ll v = -1;
    ll k = -1;
    REP(i, q.size()) {
      if (uu > u[q[i]] || (uu == u[q[i]] && dd > d[q[i]])) {
        v = q[i];
        uu = u[q[i]];
        dd = d[q[i]];
        k = i;
      }
    }
    q.erase(q.begin() + k);

    for (auto e : G[v]) {
      ll nu = uu;
      ll nd = dd + e.cap;
      if (nd > l) {
        nd = e.cap;
        nu++;
      }
      if (u[e.to] > nu || (u[e.to] == nu && d[e.to] > nd)) {
        u[e.to] = nu;
        d[e.to] = nd;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n, m, l;
  cin >> n >> m >> l;
  Graph g(n);
  REP(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c <= l)
      add_edge(g, a, b, c, true, c);
  }

  Matrix ans(n);
  Matrix d(n);

  REP(i, n) { Dijkstra(g, i, d[i], ans[i], l); }
  /*
  for (auto i : ans) {
          for (auto j : i) {
                  cout << j << " ";
          }
          cout << endl;
  }
  */

  ll q;
  cin >> q;

  REP(i, q) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    if (ans[s][t] != INF) {
      cout << ans[s][t] << endl;
    } else {
      cout << -1 << endl;
    }
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <unordered_map>
#include <vector>

using namespace std;

#define MOD 1000000007
#define REP(i, n) for (ll(i) = 0; (i) < (n); (i)++)
#define FOR(i, c)                                                              \
  for (decltype((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define ll long long
#define ull unsigned long long
#define all(hoge) (hoge).begin(), (hoge).end()
typedef pair<ll, ll> P;
const long long INF = 1LL << 60;
typedef vector<ll> Array;
typedef vector<Array> Matrix;

template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

// グラフ関連
struct Edge { // グラフ
  ll to, cap, rev;
  Edge(ll _to, ll _cap, ll _rev) {
    to = _to;
    cap = _cap;
    rev = _rev;
  }
};
typedef vector<Edge> Edges;
typedef vector<Edges> Graph;

void add_edge(Graph &G, ll from, ll to, ll cap, bool revFlag, ll revCap) {
  G[from].push_back(Edge(to, cap, (ll)G[to].size()));
  if (revFlag)
    G[to].push_back(Edge(from, revCap, (ll)G[from].size() - 1));
}

void Dijkstra(Graph &G, ll s, Array &d, Array &u, ll l) { // O(|E|log|V|)
  d.resize(G.size());
  u.resize(G.size());
  REP(i, d.size()) d[i] = INF;
  REP(i, u.size()) u[i] = INF;
  d[s] = 0;
  u[s] = 0;
  vector<ll> q(G.size());
  REP(i, G.size()) q.push_back(i);
  while (q.size() > 0) {
    ll uu = INF;
    ll dd = INF;
    ll v = -1;
    ll k = -1;
    REP(i, q.size()) {
      if (uu > u[q[i]] || (uu == u[q[i]] && dd > d[q[i]])) {
        v = q[i];
        uu = u[q[i]];
        dd = d[q[i]];
        k = i;
      }
    }
    if (k == -1)
      break;
    q.erase(q.begin() + k);

    for (auto e : G[v]) {
      ll nu = uu;
      ll nd = dd + e.cap;
      if (nd > l) {
        nd = e.cap;
        nu++;
      }
      if (u[e.to] > nu || (u[e.to] == nu && d[e.to] > nd)) {
        u[e.to] = nu;
        d[e.to] = nd;
      }
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);

  ll n, m, l;
  cin >> n >> m >> l;
  Graph g(n);
  REP(i, m) {
    ll a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    if (c <= l)
      add_edge(g, a, b, c, true, c);
  }

  Matrix ans(n);
  Matrix d(n);

  REP(i, n) { Dijkstra(g, i, d[i], ans[i], l); }
  /*
  for (auto i : ans) {
          for (auto j : i) {
                  cout << j << " ";
          }
          cout << endl;
  }
  */

  ll q;
  cin >> q;

  REP(i, q) {
    ll s, t;
    cin >> s >> t;
    s--;
    t--;
    if (ans[s][t] != INF) {
      cout << ans[s][t] << endl;
    } else {
      cout << -1 << endl;
    }
  }

  return 0;
}