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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, M, S;
  cin >> N >> M >> S;
  ll Ui, Vi, Ai, Bi, Ci, Di;
  vector<vector<array<ll, 3>>> g1(N, vector<array<ll, 3>>());
  vector<array<ll, 2>> g2(N, array<ll, 2>());
  for (int i = 0; i < M; i++) {
    cin >> Ui >> Vi >> Ai >> Bi;
    Ui--;
    Vi--;
    g1[Ui].push_back(array<ll, 3>{Vi, Ai, Bi});
    g1[Vi].push_back(array<ll, 3>{Ui, Ai, Bi});
  }
  for (int i = 0; i < N; i++) {
    cin >> Ci >> Di;
    g2[i] = array<ll, 2>{Ci, Di};
  }

  priority_queue<std::array<ll, 3>> q;
  ll n_s_max = 50 * N;
  std::vector<ll> d(N * n_s_max, numeric_limits<ll>::max());
  S = min(S, n_s_max - 1);
  d[0 + S * N] = 0;
  q.push(std::array<ll, 3>{0, S, 0}); // minutes, money, index
  while (!q.empty()) {
    auto tmp = q.top();
    q.pop();
    auto u = tmp[2];
    auto s0 = tmp[1];
    auto d0 = -tmp[0];

    for (size_t i = 0; i < g1[u].size(); i++) {
      auto v = g1[u][i][0];
      auto s = g1[u][i][1];
      auto w = g1[u][i][2];
      if (s0 < s)
        continue;
      auto v1 = v + (s0 - s) * N;
      auto alt = d0 + w;
      if (alt < d[v1]) {
        d[v1] = alt;
        q.push(std::array<ll, 3>{-alt, s0 - s, v});
      }
    }
    if (s0 + g2[u][0] == n_s_max)
      continue;
    s0 += g2[u][0];
    s0 = min(s0, n_s_max - 1);
    auto v1 = u + s0 * N;
    auto alt = d0 + g2[u][1];
    if (alt < d[v1]) {
      d[v1] = alt;
      q.push(std::array<ll, 3>{-(d0 + g2[u][1]), s0, u});
    }
  }
  for (int i = 1; i < N; i++) {
    ll res = numeric_limits<ll>::max();
    for (int j = 0; j < n_s_max; j++) {
      res = min(res, d[i + j * N]);
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;

int main() {
  ll N, M, S;
  cin >> N >> M >> S;
  ll Ui, Vi, Ai, Bi, Ci, Di;
  vector<vector<array<ll, 3>>> g1(N, vector<array<ll, 3>>());
  vector<array<ll, 2>> g2(N, array<ll, 2>());
  for (int i = 0; i < M; i++) {
    cin >> Ui >> Vi >> Ai >> Bi;
    Ui--;
    Vi--;
    g1[Ui].push_back(array<ll, 3>{Vi, Ai, Bi});
    g1[Vi].push_back(array<ll, 3>{Ui, Ai, Bi});
  }
  for (int i = 0; i < N; i++) {
    cin >> Ci >> Di;
    g2[i] = array<ll, 2>{Ci, Di};
  }

  priority_queue<std::array<ll, 3>> q;
  std::vector<ll> d(N, numeric_limits<ll>::max());
  d[0] = 0;
  q.push(std::array<ll, 3>{d[0], S, 0}); // minutes, money, index
  ll dmax = 0;
  set<ll> ss;
  ss.insert(0);
  while (!q.empty()) {
    auto tmp = q.top();
    q.pop();
    auto u = tmp[2];
    auto s0 = tmp[1];
    auto d0 = -tmp[0];
    if (ss.size() == N && dmax <= d0)
      break;

    for (size_t i = 0; i < g1[u].size(); i++) {
      auto v = g1[u][i][0];
      auto s = g1[u][i][1];
      auto w = g1[u][i][2];
      if (s0 >= s) {
        auto alt = d0 + w;
        d[v] = min(d[v], alt);
        q.push(std::array<ll, 3>{-alt, s0 - s, v});
        dmax = max(dmax, d[v]);
        ss.insert(v);
      }
    }
    q.push(std::array<ll, 3>{-(d0 + g2[u][1]), s0 + g2[u][0], u});
  }
  for (int i = 1; i < N; i++) {
    cout << d[i] << endl;
  }

  return 0;
}