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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

using P = pair<int, int>;
using Edge = pair<int, P>;

signed main() {
  int N, M, S;
  cin >> N >> M >> S;

  vector<vector<Edge>> graph(N);
  REP(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    graph[u].emplace_back(Edge(v, make_pair(a, b)));
    graph[v].emplace_back(Edge(u, make_pair(a, b)));
  }

  vector<int> c(N), d(N);
  REP(i, N) cin >> c[i] >> d[i];

  const int MAX = 2500;
  chmin(S, MAX);

  vector<vector<int>> dp(N, vector<int>(MAX + 1, INF));
  dp[0][S] = 0;

  priority_queue<Edge, vector<Edge>, greater<Edge>> q;
  q.push({0, {0, S}});

  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int time = p.first;
    int v = p.second.first;
    int s = p.second.second;
    if (time > dp[v][s])
      continue;

    if (s + c[v] <= MAX) {
      int ns = s + c[v];
      int nt = time + d[v];
      if (chmin(dp[v][ns], nt)) {
        q.push({nt, {v, ns}});
      }
    }

    for (auto e : graph[v]) {
      if (s < e.second.first)
        continue;
      int nv = e.first;
      int ns = s - e.second.first;
      int nt = time + e.second.second;
      if (chmin(dp[nv][ns], nt)) {
        q.push({nt, {nv, ns}});
      }
    }
  }

  FOR(i, 1, N) {
    int ret = INF;
    REP(j, MAX) chmin(ret, dp[i][j]);
    cout << ret << "\\n";
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string.h>
#include <string>
#include <vector>

// output
#define SPBR(w, n) std::cout << (w + 1 == n ? '\\n' : ' ');
#define YES cout << "YES" << endl
#define Yes cout << "Yes" << endl
#define NO cout << "NO" << endl
#define No cout << "No" << endl
// utility
#define ALL(i) (i).begin(), (i).end()
#define FOR(i, a, n) for (int i = (a); i < (n); ++i)
#define RFOR(i, a, n) for (int i = (n)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = int(n) - 1; i >= 0; --i)
#define IN(a, x, b) (a <= x && x < b)
#define OUT(a, x, b) (x < a || b <= x)
template <class T> inline T chmax(T &a, const T b) {
  return a = (a < b) ? b : a;
}
template <class T> inline T chmin(T &a, const T b) {
  return a = (a > b) ? b : a;
}

// type/const
#define int ll
using ll = long long;
using ull = unsigned long long;
using ld = long double;
const int MOD = 1000000007;
/* const int MOD = 998244353; */
const int INF = 1e18;
const double PI = acos(-1);

using namespace std;
struct INIT {
  INIT() {
    cin.tie(0);
    ios::sync_with_stdio(false);
    cout << fixed << setprecision(10);
  }
} INIT;

using P = pair<int, int>;
using Edge = pair<int, P>;

signed main() {
  int N, M, S;
  cin >> N >> M >> S;

  vector<vector<Edge>> graph(N);
  REP(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    graph[u].emplace_back(make_pair(v, make_pair(a, b)));
    graph[v].emplace_back(make_pair(u, make_pair(a, b)));
  }

  vector<int> c(N), d(N);
  REP(i, N) cin >> c[i] >> d[i];

  int MAX = N * 50;
  chmin(S, MAX);

  vector<vector<int>> dp(N, vector<int>(MAX + 1, INF));
  dp[0][S] = 0;

  priority_queue<Edge, vector<Edge>, greater<Edge>> q;
  q.push({0, {0, S}});

  while (!q.empty()) {
    auto p = q.top();
    q.pop();
    int time = p.first;
    int v = p.second.first;
    int s = p.second.second;
    if (time > dp[v][s])
      continue;

    if (s + c[v] <= MAX) {
      if (chmin(dp[v][s + c[v]], time + d[v])) {
        q.push({time + d[v], {v, s + c[v]}});
      }
    }

    for (auto e : graph[v]) {
      if (s - e.second.first >= 0)
        if (chmin(dp[e.first][s - e.second.first], time + e.second.second)) {
          q.push({time + e.second.second, {e.first, s - e.second.first}});
        }
    }
  }

  FOR(i, 1, N) {
    int ret = INF;
    REP(j, MAX) chmin(ret, dp[i][j]);
    cout << ret << "\\n";
  }

  return 0;
}