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

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

struct Edge {
  int to;
  lint A;
  lint B;
};

struct State {
  int v;
  int num;
  lint cost;
  bool operator>(const State &o) const { return cost > o.cost; }
};

int N, M, S, C[55], D[55];
lint dst[55][2501];
vector<Edge> edge[55];

int main() {
  cin >> N >> M >> S;

  for_(i, 0, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    edge[u].push_back(Edge{v, a, b});
    edge[v].push_back(Edge{u, a, b});
  }

  for_(i, 0, N) cin >> C[i] >> D[i];

  const int MAX_CNT = N * 50;
  for_(i, 0, N) for_(j, 0, MAX_CNT + 1) dst[i][j] = 1LL << 60;
  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State{0, min(S, MAX_CNT), 0LL});
  dst[0][0] = 0;

  while (!que.empty()) {
    State s = que.top();
    que.pop();
    if (dst[s.v][s.num] < s.cost)
      continue;

    for (const Edge &e : edge[s.v]) {
      int nxt_num = s.num - e.A;
      lint nxt_cost = s.cost + e.B;
      if (nxt_num >= 0 and dst[e.to][nxt_num] > nxt_cost) {
        dst[e.to][nxt_num] = nxt_cost;
        que.push(State{e.to, nxt_num, nxt_cost});
      }

      while (nxt_num < MAX_CNT) {
        nxt_num = min(nxt_num + C[s.v], MAX_CNT);
        nxt_cost += D[s.v];
        if (nxt_num >= 0 and dst[e.to][nxt_num] > nxt_cost) {
          dst[e.to][nxt_num] = nxt_cost;
          que.push(State{e.to, nxt_num, nxt_cost});
        }
      }
    }
  }

  for_(i, 1, N) cout << *min_element(dst[i], dst[i] + MAX_CNT + 1) << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define for_(i, a, b) for (int i = a; i < b; ++i)
#define ALL(a) a.begin(), a.end()
#define INIT(a, b) memset(a, b, sizeof(a))
using lint = long long int;

struct Edge {
  int to;
  lint A;
  lint B;
};

struct State {
  int v;
  int num;
  lint cost;
  bool operator>(const State &o) const { return cost > o.cost; }
};

int N, M, S, C[55], D[55];
lint dst[55][2501];
vector<Edge> edge[55];

int main() {
  cin >> N >> M >> S;

  for_(i, 0, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    edge[u].push_back(Edge{v, a, b});
    edge[v].push_back(Edge{u, a, b});
  }

  for_(i, 0, N) cin >> C[i] >> D[i];

  vector<lint> ans(N, 1LL << 60);

  priority_queue<State, vector<State>, greater<State>> que;
  que.push(State{0, min(S, 2500), 0LL});

  for_(i, 0, N) for_(j, 0, 2501) dst[i][j] = 1LL << 60;
  dst[0][0] = 0;

  while (!que.empty()) {
    State s = que.top();
    que.pop();
    if (dst[s.v][s.num] < s.cost)
      continue;

    for (const Edge &e : edge[s.v]) {
      int nxt_num = s.num - e.A;
      lint nxt_cost = s.cost + e.B;
      if (nxt_num >= 0 and dst[e.to][nxt_num] > nxt_cost) {
        dst[e.to][nxt_num] = nxt_cost;
        que.push(State{e.to, nxt_num, nxt_cost});
      }

      while (nxt_num < 2500) {
        nxt_num = min(nxt_num + C[s.v], 2500);
        nxt_cost += D[s.v];
        if (nxt_num >= 0 and dst[e.to][nxt_num] > nxt_cost) {
          dst[e.to][nxt_num] = nxt_cost;
          que.push(State{e.to, nxt_num, nxt_cost});
        }
      }
    }
  }

  for_(i, 1, N) cout << *min_element(dst[i], dst[i] + 2501) << endl;
}