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

/*
author: Apoorv Singh
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
#define pb push_back
#define all(c) c.begin(), c.end()
#define For(i, a, b) for (long long i = a; i < b; ++i)
#define Forr(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define ll long long
#define endl "\\n"
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define fast_io()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <class X> void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << ' ';
  cout << endl;
}

template <class T, class cmp = less<T>>
using ordered_set =
    tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

ll modulo = 1e9 + 7;

ll fpow(ll a, ll n) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = (ans * a) % modulo;
    a = (a * a) % modulo;
    n = n >> 1;
  }
  return ans;
}

const int M = 3000;
ll tim[52][M];
bool visited[52][M];
vector<pair<int, pll>> graph[52];
ll C[52], D[52];

void dijkstra(int s) {
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      pq;
  pq.push({0, {1, s}});
  while (!pq.empty()) {
    ll t = pq.top().F;
    int source = pq.top().S.F;
    int coins = pq.top().S.S;
    pq.pop();
    if (visited[source][coins]) {
      continue;
    }
    visited[source][coins] = 1;
    for (auto a : graph[source]) {
      int left = coins - a.S.F;
      ll next = t + a.S.S;
      if (left >= 0 and tim[a.F][left] > next) {
        tim[a.F][left] = next;
        pq.push({next, {a.F, left}});
      }
    }
    if (tim[source][min(M - 1, coins + C[source])] > t + D[source]) {
      pq.push({t + D[source], {source, min(M - 1, coins + C[source])}});
      tim[source][min(M - 1, coins + C[source])] = t + D[source];
    }
  }
}

void solve() {
  int n, m;
  ll s;
  cin >> n >> m >> s;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < M; ++j) {
      tim[i][j] = LLONG_MAX;
    }
  }
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < m; ++i) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    graph[u].pb({v, {a, b}});
    graph[v].pb({u, {a, b}});
  }
  for (int i = 1; i <= n; ++i) {
    cin >> C[i] >> D[i];
  }
  s = min(s, M - 1);
  dijkstra(s);
  for (int i = 2; i <= n; ++i) {
    ll ans = LLONG_MAX;
    for (int j = 0; j < M; ++j) {
      ans = min(ans, tim[i][j]);
    }
    cout << ans << endl;
  }
}

int main() {
  fast_io();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}


Error: TLE

Fixed submission:

/*
author: Apoorv Singh
*/

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/detail/standard_policies.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;

typedef vector<int> vi;
typedef pair<int, int> pii;
typedef vector<long long> vll;
typedef pair<long long, long long> pll;
#define pb push_back
#define all(c) c.begin(), c.end()
#define For(i, a, b) for (long long i = a; i < b; ++i)
#define Forr(i, a, b) for (long long i = a; i > b; --i)
#define um unordered_map
#define F first
#define S second
#define ll long long
#define endl "\\n"
#define min(a, b) (a < b ? a : b)
#define max(a, b) (a > b ? a : b)
#define fast_io()                                                              \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(NULL);                                                               \\
  cout.tie(NULL);

#define TRACE
#ifndef ONLINE_JUDGE
#define trace(...) __f(#__VA_ARGS__, __VA_ARGS__)
template <typename Arg1> void __f(const char *name, Arg1 &&arg1) {
  cout << name << " : " << arg1 << endl;
}
template <typename Arg1, typename... Args>
void __f(const char *names, Arg1 &&arg1, Args &&...args) {
  const char *comma = strchr(names + 1, ',');
  cout.write(names, comma - names) << " : " << arg1 << " | ";
  __f(comma + 1, args...);
}
#else
#define trace(...)
#endif

template <class X> void printarr(X arr[], int n) {
  for (int i = 0; i < n; ++i)
    cout << arr[i] << ' ';
  cout << endl;
}

template <class T, class cmp = less<T>>
using ordered_set =
    tree<T, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;

ll modulo = 1e9 + 7;

ll fpow(ll a, ll n) {
  ll ans = 1;
  while (n) {
    if (n & 1)
      ans = (ans * a) % modulo;
    a = (a * a) % modulo;
    n = n >> 1;
  }
  return ans;
}

const int M = 3000;
ll tim[52][M];
bool visited[52][M];
vector<pair<int, pll>> graph[52];
ll C[52], D[52];

void dijkstra(int s) {
  priority_queue<pair<ll, pii>, vector<pair<ll, pii>>, greater<pair<ll, pii>>>
      pq;
  pq.push({0ll, {1, s}});
  while (!pq.empty()) {
    ll t = pq.top().F;
    int source = pq.top().S.F;
    int coins = pq.top().S.S;
    pq.pop();
    if (visited[source][coins]) {
      continue;
    }
    visited[source][coins] = 1;
    for (pair<int, pll> a : graph[source]) {
      int v = a.F;
      ll A = a.S.F;
      ll B = a.S.S;
      int left = coins - A;
      ll curr = t + B;
      while (left < 0) {
        left += C[source];
        curr += D[source];
        left = min(left, M - 1);
      }
      while (true) {
        if (tim[v][left] == -1 or tim[v][left] > curr) {
          pq.push({curr, {v, left}});
          tim[v][left] = curr;
        }
        left += C[source];
        curr += D[source];
        if (left >= M) {
          if (tim[v][M - 1] == -1 or tim[v][M - 1] > curr) {
            pq.push({curr, {v, M - 1}});
            tim[v][M - 1] = curr;
          }
          break;
        }
      }
    }
  }
}

void solve() {
  int n, m;
  ll s;
  cin >> n >> m >> s;
  memset(tim, -1, sizeof(tim));
  memset(visited, 0, sizeof(visited));
  for (int i = 0; i < m; ++i) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    graph[u].pb({v, {a, b}});
    graph[v].pb({u, {a, b}});
  }
  for (int i = 1; i <= n; ++i) {
    cin >> C[i] >> D[i];
  }
  s = min(s, M - 1);
  dijkstra(s);
  for (int i = 2; i <= n; ++i) {
    ll ans = LLONG_MAX;
    for (int j = 0; j < M; ++j) {
      if (tim[i][j] != -1) {
        ans = min(ans, tim[i][j]);
      }
    }
    cout << ans << endl;
  }
}

int main() {
  fast_io();
  int t = 1;
  // cin >> t;
  while (t--)
    solve();
  return 0;
}