Problem p02889 (C++)

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

using namespace std;

int main(void) {
  // 5+ 1854-
  int N, M;
  long long L;
  cin >> N >> M >> L;

  vector<vector<int>> Load_to(N, vector<int>());
  vector<vector<int>> Load_cost(N, vector<int>());

  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    Load_to[a].push_back(b);
    Load_cost[a].push_back(c);
    Load_to[b].push_back(a);
    Load_cost[b].push_back(c);
  }

  // Map
  vector<vector<int>> CostMap(N, vector<int>(N, -1));

  // 一回で行ける距離を判定
  for (int i = 0; i < N; i++) {
    priority_queue<pair<int, int>> que;

    que.push(make_pair(L, i));

    while (true) {
      if (que.empty()) {
        break;
      }
      pair<int, int> S = que.top();
      que.pop();
      int tank = S.first;
      int from = S.second;
      if (CostMap[i][from] == 0) {
        continue;
      }
      CostMap[i][from] = 0;
      for (int j = 0; j < Load_to[from].size(); j++) {
        int to = Load_to[from][j];
        int rest = tank - Load_cost[from][j];
        if (Load_cost[from][j] > tank) {
          continue;
        }
        if (tank != 0) {
          que.push(make_pair(rest, to));
        }
      }
    }
  }
  int t = 0;
  while (true) {
    bool check = false;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j) {
          CostMap[i][j] = 0;
          continue;
        }
        if (CostMap[i][j] == t) {
          for (int k = 0; k < N; k++) {
            if (CostMap[j][k] != -1) {
              int cost = CostMap[i][j] + CostMap[j][k] + 1;
              if (CostMap[i][k] == -1 ||
                  (CostMap[i][k] != -1 && CostMap[i][k] > cost)) {
                CostMap[i][k] = cost;
                check = true;
              }
            }
          }
        }
      }
    }
    if (!check) {
      break;
    }
    t++;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    cout << CostMap[s][t] << endl;
  }
  /* debacker
  for (int i = 0; i < N; i++){
    for(int j=0;j<N;j++){
      cout<<CostMap[i][j] <<" ";
    }
    cout<<endl;
  }
  */

  return 0;
}

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

using namespace std;

int main(void) {
  // 5+ 1854-
  int N, M;
  long long L;
  cin >> N >> M >> L;

  vector<vector<int>> Load_to(N, vector<int>());
  vector<vector<int>> Load_cost(N, vector<int>());

  for (int i = 0; i < M; i++) {
    int a, b, c;
    cin >> a >> b >> c;
    a--;
    b--;
    Load_to[a].push_back(b);
    Load_cost[a].push_back(c);
    Load_to[b].push_back(a);
    Load_cost[b].push_back(c);
  }

  // Map
  vector<vector<int>> CostMap(N, vector<int>(N, -1));

  // 一回で行ける距離を判定
  for (int i = 0; i < N; i++) {
    priority_queue<pair<int, int>> que;

    que.push(make_pair(L, i));

    while (true) {
      if (que.empty()) {
        break;
      }
      pair<int, int> S = que.top();
      que.pop();
      int tank = S.first;
      int from = S.second;
      if (CostMap[i][from] == 0) {
        continue;
      }
      CostMap[i][from] = 0;
      for (int j = 0; j < Load_to[from].size(); j++) {
        int to = Load_to[from][j];
        int rest = tank - Load_cost[from][j];
        if (Load_cost[from][j] > tank || CostMap[i][to] == 0) {
          continue;
        }
        if (tank != 0) {
          que.push(make_pair(rest, to));
        }
      }
    }
  }
  int t = 0;
  while (true) {
    bool check = false;
    for (int i = 0; i < N; i++) {
      for (int j = 0; j < N; j++) {
        if (i == j) {
          CostMap[i][j] = 0;
          continue;
        }
        if (CostMap[i][j] == t) {
          for (int k = 0; k < N; k++) {
            if (CostMap[j][k] != -1) {
              int cost = CostMap[i][j] + CostMap[j][k] + 1;
              if (CostMap[i][k] == -1 ||
                  (CostMap[i][k] != -1 && CostMap[i][k] > cost)) {
                CostMap[i][k] = cost;
                check = true;
              }
            }
          }
        }
      }
    }
    if (!check) {
      break;
    }
    t++;
  }

  int Q;
  cin >> Q;

  for (int i = 0; i < Q; i++) {
    int s, t;
    cin >> s >> t;
    s--;
    t--;
    cout << CostMap[s][t] << endl;
  }
  /* debacker
  for (int i = 0; i < N; i++){
    for(int j=0;j<N;j++){
      cout<<CostMap[i][j] <<" ";
    }
    cout<<endl;
  }
  */

  return 0;
}