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

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr int MAX_MONEY = 5000;

struct State {
  int at;
  int money;
  ll cost;
  // int prev;
  State(int at, int money, ll cost /*, int prev*/)
      : at(at), money(money), cost(cost) /*, prev(prev)*/ {}
  bool operator>(const State &s) const { return cost > s.cost; }
};

struct Edge {
  int to;
  int money;
  ll cost;
  Edge(int to, int money, ll cost) : to(to), money(money), cost(cost) {}
};

using Graph = vector<vector<Edge>>; // 隣接リスト

const ll INF = 1e18;
// const int NONE = -1;

// sは始点、mincは最短経路のコスト、prevsは最短経路をたどる際の前の頂点
void dijkstra(int s, const Graph &graph, vector<vector<ll>> &minc, int S,
              const vector<int> &c, const vector<ll> &d) {
  minc.assign(graph.size(), vector<ll>(MAX_MONEY + 1, INF));
  // prevs.assign(graph.size(), NONE);

  priority_queue<State, vector<State>, greater<State>> pq;
  S = min(S, MAX_MONEY);
  pq.emplace(s, S, 0 /*, NONE*/);
  minc[s][S] = 0;

  while (!pq.empty()) {
    State cur = pq.top();
    pq.pop();
    if (minc[cur.at][cur.money] < cur.cost)
      continue;

    for (const Edge &e : graph[cur.at]) {
      int money = cur.money - e.money;
      if (money < 0)
        continue;

      ll cost = cur.cost + e.cost;
      if (minc[e.to][money] <= cost)
        continue;

      minc[e.to][money] = cost;
      pq.emplace(e.to, money, cost /*, cur.at*/);
    }

    if (cur.money == MAX_MONEY)
      continue;
    int money = min(cur.money + c[cur.at], MAX_MONEY);
    ll cost = cur.cost + d[cur.at];

    if (minc[cur.at][money] <= cost)
      continue;
    minc[cur.at][money] = cost;
    pq.emplace(cur.at, money, cost /*, cur.at*/);
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  ll s;
  cin >> n >> m >> s;

  Graph g(n);
  for (int i = 0; i < m; ++i) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }

  vector<int> c(n);
  vector<ll> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i] >> d[i];
  }

  vector<vector<ll>> minc;
  dijkstra(0, g, minc, s, c, d);

  for (int i = 1; i < n; ++i) {
    ll ans = INF;
    for (int j = 0; j <= MAX_MONEY; ++j) {
      ans = min(ans, minc[i][j]);
    }
    cout << ans << "\\n";
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

using ll = long long;

constexpr int MAX_MONEY = 5000;

struct State {
  int at;
  int money;
  ll cost;
  // int prev;
  State(int at, int money, ll cost /*, int prev*/)
      : at(at), money(money), cost(cost) /*, prev(prev)*/ {}
  bool operator>(const State &s) const { return cost > s.cost; }
};

struct Edge {
  int to;
  int money;
  ll cost;
  Edge(int to, int money, ll cost) : to(to), money(money), cost(cost) {}
};

using Graph = vector<vector<Edge>>; // 隣接リスト

const ll INF = 1e18;
// const int NONE = -1;

// sは始点、mincは最短経路のコスト、prevsは最短経路をたどる際の前の頂点
void dijkstra(int s, const Graph &graph, vector<vector<ll>> &minc, int S,
              const vector<int> &c, const vector<ll> &d) {
  minc.assign(graph.size(), vector<ll>(MAX_MONEY + 1, INF));
  // prevs.assign(graph.size(), NONE);

  priority_queue<State, vector<State>, greater<State>> pq;
  S = min(S, MAX_MONEY);
  pq.emplace(s, S, 0 /*, NONE*/);
  minc[s][S] = 0;

  while (!pq.empty()) {
    State cur = pq.top();
    pq.pop();
    if (minc[cur.at][cur.money] < cur.cost)
      continue;

    for (const Edge &e : graph[cur.at]) {
      ll cost = cur.cost + e.cost;
      for (int money = cur.money - e.money; money <= MAX_MONEY;) {
        if (money >= 0) {
          if (minc[e.to][money] > cost) {
            minc[e.to][money] = cost;
            pq.emplace(e.to, money, cost /*, cur.at*/);
          }
        }

        if (money == MAX_MONEY)
          break;

        money = min(MAX_MONEY, money + c[cur.at]);
        cost += d[cur.at];
      }
    }
  }
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);
  int n, m;
  ll s;
  cin >> n >> m >> s;

  Graph g(n);
  for (int i = 0; i < m; ++i) {
    int u, v, a;
    ll b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
  }

  vector<int> c(n);
  vector<ll> d(n);
  for (int i = 0; i < n; ++i) {
    cin >> c[i] >> d[i];
  }

  vector<vector<ll>> minc;
  dijkstra(0, g, minc, s, c, d);

  for (int i = 1; i < n; ++i) {
    ll ans = INF;
    for (int j = 0; j <= MAX_MONEY; ++j) {
      ans = min(ans, minc[i][j]);
    }
    cout << ans << "\\n";
  }
  return 0;
}