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

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 1000000000000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！

struct edge {
  int to, kane, cost;
};

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  whei(edge) G(n);
  rep(i, 0, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    v--;
    u--;
    G[v].push_back(edge{u, a, b});
    G[u].push_back(edge{v, a, b});
  }
  int Max_s = 50 * (n - 1);
  s = min(s, Max_s);
  whei(llint) d(n, hei(llint)(Max_s + 1, INF));
  hei(int) D(n), C(n);
  rep(i, 0, n) cin >> C[i] >> D[i];
  priority_queue<pair<llint, Pint>, vector<pair<llint, Pint>>,
                 greater<pair<llint, Pint>>>
      que;
  auto push = [&](llint t, int v, int nowS) {
    if (nowS < 0)
      return;
    if (d[v][nowS] <= t)
      return;
    d[v][nowS] = t;
    que.push(pair<llint, Pint>(t, Pint(v, nowS)));
  };
  push(0, 0, s);
  while (!que.empty()) {
    pair<llint, Pint> p = que.top();
    que.pop();
    llint T = p.first;
    int V = p.second.first;
    int Nows = p.second.second;
    if (d[V][Nows] != T)
      continue;
    int NextS = min(C[V] + Nows, Max_s);
    push(T + D[V], V, NextS);
    for (edge x : G[V]) {
      push(x.cost + T, x.to, Nows - x.kane);
    }
  }
  rep(i, 1, n) {
    llint ans = INF;
    rep(j, 0, Max_s + 1) ans = min(ans, d[i][j]);
    cout << ans << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <time.h>
#include <vector>
using namespace std;
#define rep(i, a, n) for (int i = (a); i < (n); i++)
#define ll long long
#define llint long long int
#define reverse(v) reverse(v.begin(), v.end());
#define Yes(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "Yes" << endl;                                                     \\
  else                                                                         \\
    cout << "No" << endl;
#define YES(ans)                                                               \\
  if (ans)                                                                     \\
    cout << "YES" << endl;                                                     \\
  else                                                                         \\
    cout << "NO" << endl;
#define hei(a) vector<a>
#define whei(a) vector<vector<a>>
#define UF UnionFind
#define Pint pair<int, int>
#define Pll pair<llint, llint>
#define keta(a) fixed << setprecision(a)
constexpr auto INF = 1000000000000000000;
constexpr auto mod = 1000000007;
// 辞書順はnext_permutation( begin( v ), end( v ) );やで！

struct edge {
  int to;
  int kane;
  int cost;
};
whei(edge) G;
hei(int) C, D;
int sum;
void dijkdtra(vector<vector<llint>> &d, int s) {
  priority_queue<pair<pair<int, int>, llint>,
                 vector<pair<pair<int, int>, llint>>,
                 greater<pair<pair<int, int>, llint>>>
      que;
  d[0][s] = 0; // スタートの距離はもちろんzero
  que.push(pair<pair<int, int>, int>(pair<int, llint>(0, (int)s), 0));
  auto push = [&](int v, int c, llint t) {
    if (c < 0)
      return;
    if (d[v][c] <= t)
      return;
    d[v][c] = t;
    que.push(pair<pair<int, int>, llint>(pair<int, llint>(v, c), t));
  };
  while (!que.empty()) {
    pair<pair<int, int>, llint> p = que.top();
    que.pop();
    int V = p.first.first;
    if (d[V][p.first.second] != p.second)
      continue;
    {
      int ns = min(C[V] + p.first.second, sum);
      push(V, ns, p.second + D[V]);
    }
    for (edge x : G[V])
      push(x.to, p.first.second - x.kane, p.second + x.cost);
  }
}

signed main() {
  int n, m;
  cin >> n >> m;
  int s;
  cin >> s;
  G.resize(n);
  rep(i, 0, m) {
    int v, u, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    G[u].push_back(edge{v, a, b});
    G[v].push_back(edge{u, a, b});
  }
  C.resize(n);
  D.resize(n);
  sum = (int)(n - 1) * 50;
  rep(i, 0, n) cin >> C[i] >> D[i];
  whei(llint) d(n, hei(llint)(sum + 1, INF));

  s = min(s, sum);
  dijkdtra(d, s);
  rep(i, 1, n) {
    llint ans = INF;
    rep(j, 0, sum + 1) ans = min(ans, d[i][j]);
    cout << ans << endl;
  }
  return 0;
}