Problem p02703 (C++)

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

using namespace std;
using ll = long long int;

int main(void) {
  cout << fixed << setprecision(16);
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, s;
  cin >> n >> m >> s;

  vector<ll> res(n, 1e15);

  vector<vector<tuple<ll, ll, ll>>> G(n);
  while (m--) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    G[u].push_back({v, a, b});
    G[v].push_back({u, a, b});
  }

  vector<tuple<ll, ll>> cd(n);
  for (auto &[c, d] : cd)
    cin >> c >> d;

  if (s >= 50 * 50 * 1) {
    priority_queue<tuple<ll, ll>> q;
    q.push({-0, 0});
    res[0] = 0;
    while (!q.empty()) {
      auto [d, i] = q.top();
      q.pop();
      d = -d;
      for (auto [j, a, b] : G[i]) {
        if (res[j] <= d + b)
          continue;
        res[j] = d + b;
        q.push({-(d + b), j});
      }
    }
    for (ll i = 1; i < n; i++)
      cout << res[i] << endl;
    return 0;
  }

  vector<vector<ll>> dp(n, vector<ll>(50 * 50 * 1 + 1, 1e15));
  dp[0][s] = 0;
  priority_queue<tuple<ll, ll, ll>> q;
  q.push({-0, s, 0});
  bitset<100> bs;
  while (!q.empty()) {
    auto [d, c, i] = q.top();
    q.pop();
    bs[i] = 1;
    if (bs.count() == n)
      break;
    d = -d;
    if (dp[i][c] < d)
      continue;
    {
      auto [cc, dd] = cd[i];
      if (c + cc <= 50 * 50 * 1 && dp[i][c + cc] >= d + dd) {
        dp[i][c + cc] = d + dd;
        q.push({-(d + dd), c + cc, i});
      }
    }
    for (auto [j, a, b] : G[i]) {
      if (c - a < 0)
        continue;
      if (dp[j][c - a] <= d + b)
        continue;
      dp[j][c - a] = d + b;
      q.push({-(d + b), c - a, j});
    }
  }
  for (ll i = 1; i < n; i++)
    cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}


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

using namespace std;
using ll = long long int;

int main(void) {
  cout << fixed << setprecision(16);
  cin.tie(0);
  ios::sync_with_stdio(false);

  ll n, m, s;
  cin >> n >> m >> s;

  vector<ll> res(n, 1e15);

  vector<vector<tuple<ll, ll, ll>>> G(n);
  while (m--) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--, v--;
    G[u].push_back({v, a, b});
    G[v].push_back({u, a, b});
  }

  vector<tuple<ll, ll>> cd(n);
  for (auto &[c, d] : cd)
    cin >> c >> d;

  if (s >= 50 * 50 * 1) {
    priority_queue<tuple<ll, ll>> q;
    q.push({-0, 0});
    res[0] = 0;
    while (!q.empty()) {
      auto [d, i] = q.top();
      q.pop();
      d = -d;
      for (auto [j, a, b] : G[i]) {
        if (res[j] <= d + b)
          continue;
        res[j] = d + b;
        q.push({-(d + b), j});
      }
    }
    for (ll i = 1; i < n; i++)
      cout << res[i] << endl;
    return 0;
  }

  vector<vector<ll>> dp(n, vector<ll>(50 * 50 * 1 + 1, 1e15));
  dp[0][s] = 0;
  priority_queue<tuple<ll, ll, ll>> q;
  q.push({-0, s, 0});
  bitset<100> bs;
  while (!q.empty()) {
    auto [d, c, i] = q.top();
    q.pop();
    bs[i] = 1;
    if (bs.count() == n)
      break;
    d = -d;
    if (dp[i][c] < d)
      continue;
    {
      auto [cc, dd] = cd[i];
      if (c + cc <= 50 * 50 * 1 && dp[i][c + cc] > d + dd) {
        dp[i][c + cc] = d + dd;
        q.push({-(d + dd), c + cc, i});
      }
    }
    for (auto [j, a, b] : G[i]) {
      if (c - a < 0)
        continue;
      if (dp[j][c - a] <= d + b)
        continue;
      dp[j][c - a] = d + b;
      q.push({-(d + b), c - a, j});
    }
  }
  for (ll i = 1; i < n; i++)
    cout << *min_element(dp[i].begin(), dp[i].end()) << endl;
}