Problem p02889 (C++)

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

using Weight = int;
using Vertex = int;
struct Edge {
  Vertex from, to;
  Weight weight, charge;
};
using Graph = vector<vector<Edge>>;

int main() {
  int N, M, L;
  cin >> N >> M >> L;

  Graph G(N);
  for (auto i = 0; i < M; ++i) {
    int A, B, C;
    cin >> A >> B >> C;
    --A;
    --B;
    if (L < C)
      continue;
    G[A].push_back({A, B, C});
    G[B].push_back({B, A, C});
  }

  vector<vector<Weight>> memo(G.size(), vector<Weight>(G.size(), -1));
  priority_queue<Edge, vector<Edge>, function<bool(Edge, Edge)>> q(
      [](Edge lhs, Edge rhs) {
        if (lhs.charge != rhs.charge)
          return lhs.charge > rhs.charge;
        return lhs.weight < rhs.weight;
      });
  for (auto s = 0; s < N; ++s) {
    vector<vector<Weight>> d(G.size() + 1, vector<Weight>(G.size(), -1));
    auto &m = memo[s];
    q.push({s, s, L, 0});
    while (!q.empty()) {
      auto cur = q.top();
      q.pop();
      if (m[cur.to] != -1)
        continue;
      m[cur.to] = cur.charge;
      for (const auto &nex : G[cur.to])
        if (m[nex.to] == -1) {
          auto w = cur.weight - nex.weight;
          auto c = cur.charge;
          if (cur.weight < nex.weight)
            w = L - nex.weight, ++c;
          if (w <= d[c][nex.to])
            continue;
          d[c][nex.to] = w;
          q.push({cur.to, nex.to, w, c});
        }
    }
  }

  int Q;
  cin >> Q;
  for (auto i = 0; i < Q; ++i) {
    int s, t;
    cin >> s >> t;
    --s, --t;
    cout << memo[s][t] << endl;
  }
}


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

using Weight = int;
using Vertex = int;
struct Edge {
  Vertex from, to;
  Weight weight, charge;
};
using Graph = vector<vector<Edge>>;

int main() {
  int N, M, L;
  cin >> N >> M >> L;

  Graph G(N);
  for (auto i = 0; i < M; ++i) {
    int A, B, C;
    cin >> A >> B >> C;
    --A;
    --B;
    if (L < C)
      continue;
    G[A].push_back({A, B, C});
    G[B].push_back({B, A, C});
  }

  vector<vector<Weight>> memo(G.size(), vector<Weight>(G.size(), -1));
  priority_queue<Edge, vector<Edge>, function<bool(const Edge &, const Edge &)>>
      q([](const Edge &lhs, const Edge &rhs) {
        if (lhs.charge != rhs.charge)
          return lhs.charge > rhs.charge;
        return lhs.weight < rhs.weight;
      });
  for (auto s = 0; s < N; ++s) {
    vector<vector<Weight>> d(G.size() + 1, vector<Weight>(G.size(), -1));
    auto &m = memo[s];
    q.push({s, s, L, 0});
    while (!q.empty()) {
      auto cur = q.top();
      q.pop();
      if (m[cur.to] != -1)
        continue;
      m[cur.to] = cur.charge;
      for (const auto &nex : G[cur.to])
        if (m[nex.to] == -1) {
          auto w = cur.weight - nex.weight;
          auto c = cur.charge;
          if (cur.weight < nex.weight)
            w = L - nex.weight, ++c;
          if (w <= d[c][nex.to])
            continue;
          d[c][nex.to] = w;
          q.push({cur.to, nex.to, w, c});
        }
    }
  }

  int Q;
  cin >> Q;
  for (auto i = 0; i < Q; ++i) {
    int s, t;
    cin >> s >> t;
    --s, --t;
    cout << memo[s][t] << endl;
  }
}