Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using TCost = pair<long long, long long>;
using TDestination = pair<TCost, int>;

int main(void) {
  int N, M, Bbuf, Cbuf, Dbuf, Ubuf, Vbuf;
  long long Abuf, S;
  long long maxExpense = 0LL;
  cin >> N >> M >> S;
  vector<vector<TDestination>> Graph(N);
  for (int i = 0; i < M; i++) {
    cin >> Ubuf >> Vbuf >> Abuf >> Bbuf;
    Ubuf--;
    Vbuf--;
    Graph[Ubuf].push_back(TDestination(TCost(Bbuf, Abuf), Vbuf));
    Graph[Vbuf].push_back(TDestination(TCost(Bbuf, Abuf), Ubuf));
    maxExpense = max(maxExpense, Abuf);
  }
  maxExpense *= N - 1;
  for (int i = 0; i < N; i++) {
    cin >> Cbuf >> Dbuf;
    Graph[i].push_back(TDestination(TCost(Dbuf, -Cbuf), i));
  }
  priority_queue<TDestination, vector<TDestination>, greater<TDestination>> que;
  que.push(TDestination(TCost(0, min(S, maxExpense)), 0));
  vector<vector<bool>> Experienced(N, vector<bool>(maxExpense, false));
  vector<long long> timFirstVisit(N, -1LL);
  while (!que.empty()) {
    TDestination curStatus = que.top();
    que.pop();
    long long curTime = curStatus.first.first;
    long long curMoney = curStatus.first.second;
    int curCity = curStatus.second;
    if (Experienced[curCity][curMoney])
      continue;
    if (timFirstVisit[curCity] == -1)
      timFirstVisit[curCity] = curTime;
    Experienced[curCity][curMoney] = true;
    bool breakable = true;
    for (int i = 0; i < N; i++)
      breakable = breakable && (timFirstVisit[i] != -1);
    if (breakable)
      break;
    for (TDestination nxtMove : Graph[curCity]) {
      long long nxtTime = curTime + nxtMove.first.first;
      long long nxtMoney = min(curMoney - nxtMove.first.second, maxExpense);
      int nxtCity = nxtMove.second;
      if (nxtMoney < 0)
        continue;
      que.push(TDestination(TCost(nxtTime, nxtMoney), nxtCity));
    }
  }
  for (int i = 1; i < N; i++)
    cout << timFirstVisit[i] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
using TCost = pair<long long, long long>;
using TDestination = pair<TCost, int>;

int main(void) {
  int N, M, Bbuf, Cbuf, Dbuf, Ubuf, Vbuf;
  long long Abuf, S;
  long long maxExpense = 0LL;
  cin >> N >> M >> S;
  vector<vector<TDestination>> Graph(N);
  for (int i = 0; i < M; i++) {
    cin >> Ubuf >> Vbuf >> Abuf >> Bbuf;
    Ubuf--;
    Vbuf--;
    Graph[Ubuf].push_back(TDestination(TCost(Bbuf, Abuf), Vbuf));
    Graph[Vbuf].push_back(TDestination(TCost(Bbuf, Abuf), Ubuf));
    maxExpense = max(maxExpense, Abuf);
  }
  maxExpense *= N - 1;
  for (int i = 0; i < N; i++) {
    cin >> Cbuf >> Dbuf;
    Graph[i].push_back(TDestination(TCost(Dbuf, -Cbuf), i));
  }
  priority_queue<TDestination, vector<TDestination>, greater<TDestination>> que;
  que.push(TDestination(TCost(0, min(S, maxExpense)), 0));
  vector<long long> timFirstVisit(N, -1LL);
  vector<long long> mnyLastVisit(N, 0LL);
  while (!que.empty()) {
    TDestination curStatus = que.top();
    que.pop();
    long long curTime = curStatus.first.first;
    long long curMoney = curStatus.first.second;
    int curCity = curStatus.second;
    if (timFirstVisit[curCity] == -1)
      timFirstVisit[curCity] = curTime;
    mnyLastVisit[curCity] = curMoney;
    bool breakable = true;
    for (int i = 0; i < N; i++)
      breakable = breakable && (timFirstVisit[i] != -1);
    if (breakable)
      break;
    for (TDestination nxtMove : Graph[curCity]) {
      long long nxtTime = curTime + nxtMove.first.first;
      long long nxtMoney = curMoney - nxtMove.first.second;
      int nxtCity = nxtMove.second;
      if ((curCity == nxtCity) && (curMoney > maxExpense))
        continue;
      if (nxtMoney < mnyLastVisit[nxtCity])
        continue;
      que.push(
          TDestination(TCost(nxtTime, min(nxtMoney, maxExpense)), nxtCity));
    }
  }
  for (int i = 1; i < N; i++)
    cout << timFirstVisit[i] << endl;
  return 0;
}