Problem p02889 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int N, L;
int ans[300][300];

void func(vector<vector<pair<int, int>>> &E, int start) {
  vector<vector<int>> maxF(N, vector<int>(N, -1));
  vector<int> minCnt(N, -1);

  priority_queue<pair<pair<int, int>, int>> pq;
  maxF[start][0] = L;
  pq.push(make_pair(make_pair(0, L), start));

  while (!pq.empty()) {
    auto temp = pq.top();
    pq.pop();

    int tempF = temp.first.second;
    int node = temp.second;
    int tempCnt = -temp.first.first;

    if (minCnt[node] != -1) {
      continue;
    }
    minCnt[node] = tempCnt;

    if (tempCnt == N - 1) {
      continue;
    }

    for (auto e : E[node]) {
      int nextNode = e.first;
      int cost = e.second;
      if (cost > L) {
        continue;
      }

      if (cost > tempF) {
        if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt + 1] < L) {
          maxF[nextNode][tempCnt + 1] = L - cost;
          pq.push(make_pair(make_pair(-(tempCnt + 1), L - cost), nextNode));
        }
      } else {
        int nextF = tempF - cost;
        if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt] < nextF) {
          maxF[nextNode][tempCnt] = nextF;
          pq.push(make_pair(make_pair(-tempCnt, nextF), nextNode));
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    ans[start][i] = minCnt[i];
  }
}

int main() {

  int M;
  cin >> N >> M >> L;

  vector<vector<pair<int, int>>> E(N);
  int a, b, c;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    E[a - 1].emplace_back(make_pair(b - 1, c));
    E[b - 1].emplace_back(make_pair(a - 1, c));
  }

  for (int i = 0; i < N; i++) {
    func(E, i);
  }

  int Q;
  cin >> Q;
  vector<pair<int, int>> st(Q);
  int s, t;
  for (int i = 0; i < Q; i++) {
    cin >> s >> t;
    st[i] = make_pair(s, t);
  }

  for (int i = 0; i < Q; i++) {
    auto temp = st[i];
    cout << ans[temp.first - 1][temp.second - 1] << endl;
  }

  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <utility>
#include <vector>

using namespace std;

int N, L;
int ans[300][300];

void func(vector<vector<pair<int, int>>> &E, int start) {
  vector<vector<int>> maxF(N, vector<int>(N, -1));
  vector<int> minCnt(N, -1);

  priority_queue<pair<pair<int, int>, int>> pq;
  maxF[start][0] = L;
  pq.push(make_pair(make_pair(0, L), start));

  while (!pq.empty()) {
    auto temp = pq.top();
    pq.pop();

    int tempF = temp.first.second;
    int node = temp.second;
    int tempCnt = -temp.first.first;

    if (minCnt[node] != -1) {
      continue;
    }
    minCnt[node] = tempCnt;

    if (tempCnt == N - 1) {
      continue;
    }

    for (auto e : E[node]) {
      int nextNode = e.first;
      int cost = e.second;
      if (cost > L) {
        continue;
      }

      if (cost > tempF) {
        if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt + 1] < L - cost) {
          maxF[nextNode][tempCnt + 1] = L - cost;
          pq.push(make_pair(make_pair(-(tempCnt + 1), L - cost), nextNode));
        }
      } else {
        int nextF = tempF - cost;
        if (minCnt[nextNode] == -1 && maxF[nextNode][tempCnt] < nextF) {
          maxF[nextNode][tempCnt] = nextF;
          pq.push(make_pair(make_pair(-tempCnt, nextF), nextNode));
        }
      }
    }
  }

  for (int i = 0; i < N; i++) {
    ans[start][i] = minCnt[i];
  }
}

int main() {

  int M;
  cin >> N >> M >> L;

  vector<vector<pair<int, int>>> E(N);
  int a, b, c;
  for (int i = 0; i < M; i++) {
    cin >> a >> b >> c;
    E[a - 1].emplace_back(make_pair(b - 1, c));
    E[b - 1].emplace_back(make_pair(a - 1, c));
  }

  for (int i = 0; i < N; i++) {
    func(E, i);
  }

  int Q;
  cin >> Q;
  vector<pair<int, int>> st(Q);
  int s, t;
  for (int i = 0; i < Q; i++) {
    cin >> s >> t;
    st[i] = make_pair(s, t);
  }

  for (int i = 0; i < Q; i++) {
    auto temp = st[i];
    cout << ans[temp.first - 1][temp.second - 1] << endl;
  }

  return 0;
}