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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 105

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<int, int> par;

struct trio {
  ll nod, silv, cost;
  bool operator<(const trio &h) const { return cost > h.cost; }
};

ll N, M, S, C[maxn], D[maxn], a, b, c, d, Dp[maxn][maxn * maxn], MAX, sol[maxn];
priority_queue<trio> Q;
vector<trio> G[maxn];

void dijkstra(ll nod) {
  ll cost, silv, newc, news;
  for (int i = 1; i <= N; i++, sol[i] = 1ll << 60)
    for (int j = 0; j <= MAX; j++)
      Dp[i][j] = 1LL << 60;
  Dp[1][S] = 0;
  Q.push({1, S, 0});
  while (!Q.empty()) {
    auto a = Q.top();
    Q.pop();
    nod = a.nod, cost = a.cost, silv = a.silv;
    if (cost > Dp[nod][silv])
      continue;
    sol[nod] = min(sol[nod], cost);

    news = min(silv + C[nod], MAX), newc = cost + D[nod];
    if (Dp[nod][news] > newc) {
      Q.push({nod, news, newc});
      Dp[nod][news] = newc;
    }

    for (auto b : G[nod]) {
      if (silv >= b.silv && Dp[b.nod][silv - b.silv] > cost + b.cost) {
        Q.push({b.nod, silv - b.silv, cost + b.cost});
        Dp[b.nod][silv - b.silv] = cost + b.cost;
      }
    }
  }
}

int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N >> M >> S;

  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c >> d;
    G[a].pb({b, c, d});
    G[b].pb({a, c, d});
    MAX = max(MAX, c);
  }
  MAX *= N - 1;
  S = min(S, MAX);
  for (int i = 1; i <= N; i++)
    cin >> C[i] >> D[i];

  dijkstra(1);

  for (int i = 2; i <= N; i++)
    cout << sol[i] << "\\n";
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define db(x) (cerr << #x << ": " << (x) << '\\n')
#define cps CLOCKS_PER_SEC
#define tests(t)                                                               \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define iceil(n, x) (((n) + (x)-1) / (x))
#define eb emplace_back
#define pb push_back
#define pf push_front
#define pob pop_back
#define pof pop_front
#define sz size()
#define all(v) (v).begin(), (v).end()
#define uni(v) sort(all(v)), (v).erase(unique(all(v)), (v).end())
#define fi first
#define se second
#define bitcount(x) __builtin_popcount(x)
#define bit(x) (1LL << x)
#define PI acos(-1.0)
#define EPS 1e-9

#define mod 1000000007
#define maxn 105

using namespace std;

using namespace __gnu_pbds;
template <class key, class cmp = std::less<key>>
using ordered_set =
    tree<key, null_type, cmp, rb_tree_tag, tree_order_statistics_node_update>;
/// All indexing is 0-based
/// methods: find_by_order(k); & order_of_key(k);
/// To make it an ordered_multiset, use pairs of (value, time_of_insertion)
/// to distinguish values which are similar

typedef long long ll;
typedef double ld;
typedef pair<int, int> par;

struct trio {
  ll nod, silv, cost;
  bool operator<(const trio &h) const { return cost > h.cost; }
};

ll N, M, S, C[maxn], D[maxn], a, b, c, d, Dp[maxn][maxn * maxn], MAX, sol[maxn];
priority_queue<trio> Q;
vector<trio> G[maxn];

void dijkstra(ll nod) {
  ll cost, silv, newc, newn, news;
  for (int i = 1; i <= N; i++, sol[i] = 1ll << 60)
    for (int j = 0; j <= MAX; j++)
      Dp[i][j] = 1LL << 60;
  Dp[1][S] = 0;
  Q.push({1, S, 0});
  while (!Q.empty()) {
    auto a = Q.top();
    Q.pop();
    nod = a.nod, cost = a.cost, silv = a.silv;
    if (cost > Dp[nod][silv])
      continue;
    sol[nod] = min(sol[nod], cost);
    for (auto b : G[nod]) {
      newn = b.nod, news = b.silv, newc = b.cost + cost;
      for (int j = silv, m = 0; j <= MAX; j += C[nod], m += D[nod]) {
        if (j < news)
          continue;
        if (Dp[newn][j - news] > m + newc) {
          Dp[newn][j - news] = m + newc;
          Q.push({newn, j - news, m + newc});
        }
      }
    }
  }
}

int main() {
#ifdef acm
  freopen("a.in", "r", stdin);
#endif // acm
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  cin >> N >> M >> S;

  for (int i = 1; i <= M; i++) {
    cin >> a >> b >> c >> d;
    G[a].pb({b, c, d});
    G[b].pb({a, c, d});
    MAX = max(MAX, c);
  }
  MAX *= N - 1;
  S = min(S, MAX);
  for (int i = 1; i <= N; i++)
    cin >> C[i] >> D[i];

  dijkstra(1);

  for (int i = 2; i <= N; i++)
    cout << sol[i] << "\\n";
}