Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\n";
using namespace std;

const long long INF = 100100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<long long, long long> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

vector<vector<P>> Edge(305);
// mini_supply.at(i).at(j).first:iからjへの最小補給回数
//.second:iからjへの最小補給回数で行った時の最小消費燃料
vector<vector<P>> mini_supply(305, vector<P>(305));

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

  long long N, M, L;
  cin >> N >> M >> L;
  for (int i = 0; i < M; i++) {
    long long from, to, cost;
    cin >> from >> to >> cost;
    from--, to--;
    Edge.at(from).push_back(make_pair(to, cost));
    Edge.at(to).push_back(make_pair(from, cost));
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      mini_supply.at(i).at(j).first = inf;
      mini_supply.at(i).at(j).second = inf;
    }

  for (long long start = 0; start < N; start++) {
    mini_supply.at(start).at(start).first = 0;
    mini_supply.at(start).at(start).second = 0;
    priority_queue<pair<P, long long>, vector<pair<P, long long>>,
                   greater<pair<P, long long>>>
        PQ;
    // PQ.top().first.first:mini_supply
    //.first.second:mini_fuel when mini_supply
    //.second:from
    PQ.push(make_pair(make_pair(0, 0), start));
    while (!PQ.empty()) {
      pair<P, long long> p = PQ.top();
      PQ.pop();
      long long mini_sup = p.first.first;
      long long mini_fuel = p.first.second;
      long long from = p.second;
      if (mini_sup > mini_supply.at(start).at(from).first)
        continue;

      for (P V : Edge.at(from)) {
        long long to = V.first, cost = V.second;
        // 満タンでも行けないところは飛ばす
        if (cost > L)
          continue;
        long long next_fuel = mini_fuel + cost;
        long long next_sup = mini_sup;

        if (next_fuel > L) {
          next_sup++;
          next_fuel = cost;
        }
        if (mini_supply.at(start).at(to).first > next_sup ||
            (mini_supply.at(start).at(to).first == next_sup &&
             mini_supply.at(start).at(to).second > next_fuel)) {
          mini_supply.at(start).at(to).first = next_sup;
          mini_supply.at(start).at(to).second = next_fuel;
          PQ.push(make_pair(make_pair(next_sup, next_fuel), to));
        }
      }
    }
  }

  long long Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long from, to;
    cin >> from >> to;
    from--, to--;
    if (mini_supply.at(from).at(to).first == inf) {
      cout << -1 << endl;
    } else {
      cout << mini_supply.at(from).at(to).first << endl;
    }
  }
}


Fixed submission:
/*~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=
*$* WRITER:kakitamasziru/OxOmisosiru *$*
~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=*/
#ifdef LOCAL_JUDGE
#define _GLIBCXX_DEBUG
#endif
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <cstdint>   // int64_t, int*_t
#include <iomanip>
#include <iostream> // cout, endl, cin
#include <limits>   //setprecision
#include <string>   // string, to_string, stoi
#include <tuple>    // tuple, make_tuple
#include <utility>  // pair, make_pair
#include <vector>   // vector
// #include <cstdio> // printf
#include <map>   // map
#include <queue> // queue, priority_queue
#include <set>   // set
// #include <unordered_set> //unordered_set
#include <bitset> // bitset
#include <cmath>  //abs,,,
#include <deque>  // deque
#include <math.h> //pow,,,
#include <stack>  // stack
#define endl "\n";
using namespace std;

const long long INF = 100100100100100100;
const int MOD = 1000000007;
const int inf = 1001001001;
typedef pair<long long, long long> P;

// Solve N^M. This, mod_pow use Iterative Square Method.
long long mod_pow(long long N, long long M) {
  if (M == 0)
    return 1;
  long long res = mod_pow((N * N) % MOD, M / 2);
  // 最下位ビット(*N)が1の時は単独でNをかける
  if (M & 1)
    res = (res * N) % MOD;

  return res %= MOD;
}

long long gcd(long long a, long long b) {
  if (b == 0)
    return a;
  else
    return gcd(b, a % b);
}
long long lcm(long long a, long long b) { return a / gcd(a, b) * b; }

vector<vector<P>> Edge(305);
// mini_supply.at(i).at(j).first:iからjへの最小補給回数
//.second:iからjへの最小補給回数で行った時の最小消費燃料
vector<vector<P>> mini_supply(305, vector<P>(305));

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

  long long N, M, L;
  cin >> N >> M >> L;
  for (int i = 0; i < M; i++) {
    long long from, to, cost;
    cin >> from >> to >> cost;
    from--, to--;
    Edge.at(from).push_back(make_pair(to, cost));
    Edge.at(to).push_back(make_pair(from, cost));
  }
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++) {
      mini_supply.at(i).at(j).first = inf;
      mini_supply.at(i).at(j).second = inf;
    }

  for (long long start = 0; start < N; start++) {
    mini_supply.at(start).at(start).first = 0;
    mini_supply.at(start).at(start).second = 0;
    priority_queue<pair<P, long long>, vector<pair<P, long long>>,
                   greater<pair<P, long long>>>
        PQ;
    // PQ.top().first.first:mini_supply
    //.first.second:mini_fuel when mini_supply
    //.second:from
    PQ.push(make_pair(make_pair(0, 0), start));
    while (!PQ.empty()) {
      pair<P, long long> p = PQ.top();
      PQ.pop();
      long long mini_sup = p.first.first;
      long long mini_fuel = p.first.second;
      long long from = p.second;
      if (mini_sup > mini_supply.at(start).at(from).first ||
          mini_sup == mini_supply.at(start).at(from).first &&
              mini_fuel > mini_supply.at(start).at(from).second)
        continue;

      for (P V : Edge.at(from)) {
        long long to = V.first, cost = V.second;
        // 満タンでも行けないところは飛ばす
        if (cost > L)
          continue;
        long long next_fuel = mini_fuel + cost;
        long long next_sup = mini_sup;

        if (next_fuel > L) {
          next_sup++;
          next_fuel = cost;
        }
        if (mini_supply.at(start).at(to).first > next_sup ||
            (mini_supply.at(start).at(to).first == next_sup &&
             mini_supply.at(start).at(to).second > next_fuel)) {
          mini_supply.at(start).at(to).first = next_sup;
          mini_supply.at(start).at(to).second = next_fuel;
          PQ.push(make_pair(make_pair(next_sup, next_fuel), to));
        }
      }
    }
  }

  long long Q;
  cin >> Q;
  for (int i = 0; i < Q; i++) {
    long long from, to;
    cin >> from >> to;
    from--, to--;
    if (mini_supply.at(from).at(to).first == inf) {
      cout << -1 << endl;
    } else {
      cout << mini_supply.at(from).at(to).first << endl;
    }
  }
}