Problem p02703 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("avx")

#include <functional>
#include <iostream>
#include <queue>
#include <vector>

constexpr int MAX_N = 50;
constexpr int MAX_A = 50;
constexpr int MAX_STATE = MAX_N * MAX_A + 1;
long long int dp[MAX_N][MAX_STATE];

int SIZE[MAX_N];
struct train {
  int to;
  int fare;
  int dist;
  constexpr train(void) : to(), fare(), dist() {}
  constexpr train(const int to, const int fare, const int dist)
      : to(to), fare(fare), dist(dist) {}
} edge[MAX_N][MAX_N];

struct station_t {
  int coin;
  int time;
  constexpr station_t(void) : coin(), time() {}
} station[MAX_N];

struct dijkstra_info {
  long long int d;
  int vertex;
  int coin;
  constexpr dijkstra_info(void) : d(), vertex(), coin() {}
  constexpr dijkstra_info(const long long int d, const int vertex,
                          const int coin)
      : d(d), vertex(vertex), coin(coin) {}
  inline bool operator<(const dijkstra_info &other) const {
    if (d != other.d)
      return d > other.d;
    if (coin != other.coin)
      return coin < other.coin;
    return vertex > other.vertex;
  }
};

int main(void) {
  std::cin.sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cout.tie(nullptr);
  int n, m, S;
  std::cin >> n >> m >> S;
  for (int i = 0; i < n; ++i)
    std::fill(dp[i], dp[i] + MAX_STATE, 1LL << 60);
  for (int i = 0; i < m; ++i) {
    int u, v, a, b;
    std::cin >> u >> v >> a >> b;
    --u;
    --v;

    edge[u][SIZE[u]++] = {v, a, b};
    edge[v][SIZE[v]++] = {u, a, b};
  }
  for (int i = 0; i < n; ++i)
    std::cin >> station[i].coin >> station[i].time;
  const int Need = 50 * n;
  if (Need < S)
    S = Need;

  std::priority_queue<dijkstra_info> PQ;
  PQ.emplace(0, 0, S);
  dp[0][S] = 0;
  while (not PQ.empty()) {
    const auto [d, v, c] = PQ.top();
    PQ.pop();

    if (d > dp[v][c])
      continue;
    for (int nc = c - station[v].coin; nc >= 0 and dp[v][nc] > dp[v][c];
         nc -= station[v].coin)
      dp[v][nc] = dp[v][c];
    {
      const int nc = c + station[v].coin;
      const long long int nd = d + station[v].time;
      if (0 <= nc and nc <= Need and dp[v][nc] > nd) {
        dp[v][nc] = nd;
        PQ.emplace(nd, v, nc);
      }
    }
    for (int i = 0; i < SIZE[v]; ++i) {
      const int nc = c - edge[v][i].fare;
      if (nc < 0)
        continue;
      const long long int nd = d + edge[v][i].dist;
      if (dp[edge[v][i].to][nc] > nd) {
        dp[edge[v][i].to][nc] = nd;
        PQ.emplace(nd, edge[v][i].to, nc);
      }
    }
  }
  for (int i = 1; i < n; ++i) {
    long long int res = 1LL << 60;
    for (int j = 0; j < Need; ++j)
      if (dp[i][j] < res)
        res = dp[i][j];
    std::cout << res << '\\n';
  }
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("avx")

#include <functional>
#include <iostream>
#include <queue>
#include <vector>
constexpr int MAX_N = 50;
constexpr int MAX_A = 50;
constexpr int MAX_STATE = MAX_N * MAX_A + 1;
long long int dp[MAX_N][MAX_STATE];

int SIZE[MAX_N];
struct train {
  int to;
  int fare;
  int dist;
  constexpr train(void) : to(), fare(), dist() {}
  constexpr train(const int to, const int fare, const int dist)
      : to(to), fare(fare), dist(dist) {}
} edge[MAX_N][MAX_N];

struct station_t {
  int coin;
  int time;
  constexpr station_t(void) : coin(), time() {}
} station[MAX_N];

struct dijkstra_info {
  long long int d;
  int vertex;
  int coin;
  constexpr dijkstra_info(void) : d(), vertex(), coin() {}
  constexpr dijkstra_info(const long long int d, const int vertex,
                          const int coin)
      : d(d), vertex(vertex), coin(coin) {}
  inline bool operator<(const dijkstra_info &other) const {
    if (d != other.d)
      return d < other.d;
    if (coin != other.coin)
      return coin < other.coin;
    return vertex < other.vertex;
  }
};

int main(void) {
  std::cin.sync_with_stdio(false);
  std::cin.tie(nullptr);
  std::cout.sync_with_stdio(false);
  std::cout.tie(nullptr);
  int n, m, S;
  std::cin >> n >> m >> S;
  for (int i = 0; i < n; ++i)
    std::fill(dp[i], dp[i] + MAX_STATE, 1LL << 60);
  for (int i = 0; i < m; ++i) {
    int u, v, a, b;
    std::cin >> u >> v >> a >> b;
    --u;
    --v;
    edge[u][SIZE[u]++] = {v, a, b};
    edge[v][SIZE[v]++] = {u, a, b};
  }
  for (int i = 0; i < n; ++i)
    std::cin >> station[i].coin >> station[i].time;
  const int Need = 50 * n;
  if (Need < S)
    S = Need;

  std::priority_queue<dijkstra_info> PQ;
  PQ.emplace(0, 0, S);
  dp[0][S] = 0;
  while (not PQ.empty()) {
    const auto [d, v, c] = PQ.top();
    PQ.pop();
    if (d > dp[v][c])
      continue;
    for (int nc = c - station[v].coin; nc >= 0 and dp[v][nc] > dp[v][c];
         nc -= station[v].coin)
      dp[v][nc] = dp[v][c];
    {
      const int &&nc = c + station[v].coin;
      const long long int &&nd = d + station[v].time;
      if (nc >= 0 and dp[v][nc] > nd) {
        dp[v][nc] = nd;
        PQ.emplace(nd, v, nc);
      }
    }
    for (int i = 0; i < SIZE[v]; ++i) {
      const int nc = c - edge[v][i].fare;
      if (nc < 0)
        continue;
      const long long int &&d1 = d + edge[v][i].dist;
      if (dp[edge[v][i].to][nc] > d1) {
        dp[edge[v][i].to][nc] = d1;
        PQ.emplace(d1, edge[v][i].to, nc);
      }
    }
  }
  for (int i = 1; i < n; ++i) {
    long long int res = 1LL << 60;
    for (int j = 0; j < Need; ++j)
      if (dp[i][j] < res)
        res = dp[i][j];
    std::cout << res << '\\n';
  }
  return 0;
}