Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;

struct Edge {
  ll to;
  ll cost;
  Edge(ll t, ll c) {
    to = t;
    cost = c;
  }
};

struct Node {
  int cost;
  int gas;
  int from;
  Node(int c, int g, int f) {
    cost = c;
    gas = g;
    from = f;
  }
  bool operator<(const Node &a) const {
    return cost == a.cost ? gas < a.gas : cost > a.cost;
  }
};

ll N, M, L;
ll INF = 1e18;
ll dist[1010][1010];
ll lg[1010][1010];
int s[100010];
int t[100010];
vector<Edge> G[310];

void dijkstra(int start) {
  // cost, left, idx
  priority_queue<Node> que;
  que.emplace(Node(0, L, start));

  while (!que.empty()) {
    ll cost = que.top().cost;
    ll gas = que.top().gas;
    ll from = que.top().from;
    que.pop();
    if (dist[start][from] > cost) {
      dist[start][from] = cost;
    } else if (lg[start][from] < gas && dist[start][from] == cost) {
      lg[start][from] = gas;
    } else {
      continue;
    }
    REP(i, G[from].size()) {
      ll to = G[from][i].to;
      ll ct = G[from][i].cost;
      if (gas >= ct) {
        ll ng = gas - ct;
        ll nc = cost;
        ll ni = to;
        if (dist[start][ni] < nc)
          continue;
        if (dist[start][ni] == nc && lg[start][ni] >= ng)
          continue;
        que.emplace(Node(nc, ng, ni));
      } else {
        ll ng = L - ct;
        ll nc = cost + 1;
        ll ni = to;
        if (dist[start][ni] < nc)
          continue;
        if (dist[start][ni] == nc && lg[start][ni] >= ng)
          continue;
        que.emplace(Node(nc, ng, ni));
      }
    }
  }
}

int main() {
  cin >> N >> M >> L;
  REP(i, 1010) REP(j, 1010) {
    dist[i][j] = INF;
    lg[i][j] = 0;
  }

  REP(i, M) {
    int A, B, C;
    scanf("%d%d%d", &A, &B, &C);
    if (C > L)
      continue;
    G[A].push_back(Edge(B, C));
    G[B].push_back(Edge(A, C));
  }

  REP(i, N) { dijkstra(i + 1); }

  int Q;
  cin >> Q;
  REP(i, Q) { scanf("%d%d", s + i, t + i); }

  REP(i, Q) {
    ll dt = dist[s[i]][t[i]];
    if (dt == INF) {
      cout << -1 << endl;
    } else {
      cout << dt << endl;
    }
  }

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <limits>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
typedef long long ll;
#define REP(i, n) for (int i = 0; i < n; i++)
template <class T>
using reversed_priority_queue = priority_queue<T, vector<T>, greater<T>>;

struct Edge {
  ll to;
  ll cost;
  Edge(ll t, ll c) {
    to = t;
    cost = c;
  }
};

struct Node {
  int cost;
  int gas;
  int from;
  Node(int c, int g, int f) {
    cost = c;
    gas = g;
    from = f;
  }
  bool operator<(const Node &a) const {
    return cost == a.cost ? gas < a.gas : cost > a.cost;
  }
};

ll N, M, L;
ll INF = 1e18;
ll dist[1010][1010];
ll lg[1010][1010];
int s[100010];
int t[100010];
vector<Edge> G[310];

void dijkstra(int start) {
  // cost, left, idx
  priority_queue<Node> que;
  que.emplace(Node(0, L, start));

  while (!que.empty()) {
    ll cost = que.top().cost;
    ll gas = que.top().gas;
    ll from = que.top().from;
    que.pop();
    if (dist[start][from] > cost) {
      dist[start][from] = cost;
      lg[start][from] = gas;
    } else if (lg[start][from] < gas && dist[start][from] == cost) {
      lg[start][from] = gas;
    } else {
      continue;
    }
    REP(i, G[from].size()) {
      ll to = G[from][i].to;
      ll ct = G[from][i].cost;
      if (gas >= ct) {
        ll ng = gas - ct;
        ll nc = cost;
        ll ni = to;
        if (dist[start][ni] < nc)
          continue;
        if (dist[start][ni] == nc && lg[start][ni] >= ng)
          continue;
        que.emplace(Node(nc, ng, ni));
      } else {
        ll ng = L - ct;
        ll nc = cost + 1;
        ll ni = to;
        if (dist[start][ni] < nc)
          continue;
        if (dist[start][ni] == nc && lg[start][ni] >= ng)
          continue;
        que.emplace(Node(nc, ng, ni));
      }
    }
  }
}

int main() {
  cin >> N >> M >> L;
  REP(i, 1010) REP(j, 1010) {
    dist[i][j] = INF;
    lg[i][j] = 0;
  }

  REP(i, M) {
    int A, B, C;
    scanf("%d%d%d", &A, &B, &C);
    if (C > L)
      continue;
    G[A].push_back(Edge(B, C));
    G[B].push_back(Edge(A, C));
  }

  REP(i, N) { dijkstra(i + 1); }

  int Q;
  cin >> Q;
  REP(i, Q) { scanf("%d%d", s + i, t + i); }

  REP(i, Q) {
    ll dt = dist[s[i]][t[i]];
    if (dt == INF) {
      cout << -1 << endl;
    } else {
      cout << dt << endl;
    }
  }

  return 0;
}