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

#include <bits/stdc++.h>
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {
#ifndef ONLINE_JUDGE
  auto end_time = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> diff = end_time - start_time;
  cerr << "Time Taken : " << diff.count() << "\\n";
#endif
}
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1e9 + 7;
const int N = 2e5 + 5;
struct node {
  ll u, a, b;
};
struct node1 {
  ll tim, i, t;
};
struct CompareHeight {
  bool operator()(node1 const &p1, node1 const &p2) {
    // return "true" if "p1" is ordered
    // before "p2", for example:
    return p1.tim > p2.tim;
  }
};
int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  // cin>>T;
  while (T--) {
    ll n, m, s;
    cin >> n >> m >> s;
    vector<node> g[n];
    rep(i, m) {
      int u, v, a, b;
      cin >> u >> v >> a >> b;
      u--;
      v--;
      g[u].pb({v, a, b});
      g[v].pb({u, a, b});
    }
    ll c[n], d[n];
    rep(i, n) { cin >> c[i] >> d[i]; }
    ll ans[n];
    ll N = 5000;
    ll dp[n][N];
    memset(dp, -1, sizeof(dp));
    ll rr = min(N - 1, s);
    dp[0][rr] = 0;
    ll visit[n][N];
    memset(visit, 0, sizeof(visit));
    priority_queue<node1, vector<node1>, CompareHeight> pq;
    node1 x = {0, 0, rr};
    pq.push(x);
    while (!pq.empty()) {
      node1 p = pq.top();
      pq.pop();
      ll i = p.i;
      ll t = p.t;
      ll tim = p.tim;
      if (visit[i][t] == 1)
        continue;
      visit[i][t] = 1;
      dp[i][t] = tim;
      for (auto j : g[i]) {
        ll u = t - j.a;
        if (u < 0)
          continue;
        if (visit[j.u][u] == 0) {
          pq.push({tim + j.b, j.u, u});
        }
      }
      if (t + c[i] < N && visit[i][t + c[i]] == 0) {
        pq.push({tim + d[i], i, t + c[i]});
      }
    }
    repA(i, 1, n - 1) {
      ans[i] = 2e18;
      for (int j = 0; j < N; j++) {
        if (dp[i][j] != -1) {
          ans[i] = min(ans[i], dp[i][j]);
        }
      }
    }
    repA(i, 1, n - 1) { cout << ans[i] << endl; }
  }
  timecalculater();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const auto start_time = std::chrono::high_resolution_clock::now();
void timecalculater() {
#ifndef ONLINE_JUDGE
  auto end_time = std::chrono::high_resolution_clock::now();
  std::chrono::duration<double> diff = end_time - start_time;
  cerr << "Time Taken : " << diff.count() << "\\n";
#endif
}
void __print(int x) { cout << x; }
void __print(long x) { cout << x; }
void __print(long long x) { cout << x; }
void __print(unsigned x) { cout << x; }
void __print(unsigned long x) { cout << x; }
void __print(unsigned long long x) { cout << x; }
void __print(float x) { cout << x; }
void __print(double x) { cout << x; }
void __print(long double x) { cout << x; }
void __print(char x) { cout << '\\'' << x << '\\''; }
void __print(const char *x) { cout << '\\"' << x << '\\"'; }
void __print(const string &x) { cout << '\\"' << x << '\\"'; }
void __print(bool x) { cout << (x ? "true" : "false"); }
template <typename T, typename V> void __print(const pair<T, V> &x) {
  cout << '{';
  __print(x.first);
  cout << ',';
  __print(x.second);
  cout << '}';
}
template <typename T> void __print(const T &x) {
  int f = 0;
  cout << '{';
  for (auto &i : x)
    cout << (f++ ? "," : ""), __print(i);
  cout << "}";
}
void _print() { cout << "]\\n"; }
template <typename T, typename... V> void _print(T t, V... v) {
  __print(t);
  if (sizeof...(v))
    cout << ", ";
  _print(v...);
}
#ifndef ONLINE_JUDGE
#define dbg(x...)                                                              \\
  cout << "[" << #x << "] = [";                                                \\
  _print(x)
#else
#define dbg(x...)
#endif
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define repA(i, a, n) for (int i = a; i <= (n); ++i)
#define all(x) x.begin(), x.end()
#define sz(x) (int)(x).size()
#define fill(a) memset(a, 0, sizeof(a))
#define fst first
#define snd second
#define mp make_pair
#define pb push_back
#define endl '\\n'
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<long long, long long> pll;
typedef vector<int> vi;
typedef vector<double> vd;
typedef vector<long long int> vll;
typedef vector<pii> vii;
ll p = 1e9 + 7;
const int N = 2e5 + 5;
struct node {
  ll u, a, b;
};
struct node1 {
  ll tim, i, t;
};
struct CompareHeight {
  bool operator()(node1 const &p1, node1 const &p2) {
    // return "true" if "p1" is ordered
    // before "p2", for example:
    return p1.tim > p2.tim;
  }
};
int main() {

  cin.sync_with_stdio(0);
  cin.tie(0);
  cin.exceptions(cin.failbit);
  int T = 1;
  // cin>>T;
  while (T--) {
    ll n, m, s;
    cin >> n >> m >> s;
    vector<node> g[n];
    rep(i, m) {
      int u, v, a, b;
      cin >> u >> v >> a >> b;
      u--;
      v--;
      g[u].pb({v, a, b});
      g[v].pb({u, a, b});
    }
    ll c[n], d[n];
    rep(i, n) { cin >> c[i] >> d[i]; }
    ll ans[n];
    ll N = 2500;
    ll dp[n][N];
    memset(dp, -1, sizeof(dp));
    dp[0][s % N] = 0;
    ll visit[n][N];
    memset(visit, 0, sizeof(visit));
    priority_queue<node1, vector<node1>, CompareHeight> pq;
    node1 x = {0, 0, s % N};
    pq.push(x);
    while (!pq.empty()) {
      node1 p = pq.top();
      pq.pop();
      ll i = p.i;
      ll t = p.t;
      ll tim = p.tim;
      if (visit[i][t] == 1)
        continue;
      visit[i][t] = 1;
      dp[i][t] = tim;
      for (auto j : g[i]) {
        ll buy = 0;
        buy = (j.a - t) / c[i];
        buy = max((ll)0, buy);
        while (1) {
          ll u = t - j.a;
          ll q = j.b;
          u = (u + buy * c[i]) % N;
          q = q + buy * d[i];
          buy++;
          if (u < 0)
            continue;
          if (visit[j.u][u] == 0) {
            pq.push({tim + q, j.u, u});
          }
          u = u + c[i];
          if (u >= N) {
            break;
          }
        }
      }
    }
    repA(i, 1, n - 1) {
      ans[i] = 1e17;
      for (int j = 0; j < N; j++) {
        if (dp[i][j] != -1) {
          ans[i] = min(ans[i], dp[i][j]);
        }
      }
    }
    repA(i, 1, n - 1) { cout << ans[i] << endl; }
  }
  timecalculater();
  return 0;
}