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

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct Edge {
  long long to, cost,
      dis; // used in state is cur_v,cur_money,distance with node 1
};
vector<Edge> g[55];
long long c[55], d[55], dp[55][2550], maxv = 2500;
struct cmp {
  bool operator()(const Edge a, const Edge b) const { return a.dis > b.dis; }
};
priority_queue<Edge, vector<Edge>, cmp> pq;
int main() {
  long long i, j, n, m, s, u, v, a, b;
  scanf("%lld%lld%lld", &n, &m, &s);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld%lld", &u, &v, &a, &b);
    g[u].emplace_back((Edge){v, a, b});
    g[v].emplace_back((Edge){u, a, b});
  }
  for (i = 1; i <= n; i++)
    scanf("%lld%lld", &c[i], &d[i]);
  for (i = 0; i < 55; i++)
    for (j = 0; j < 2550; j++)
      dp[i][j] = 100000000000000000;
  s = min(s, maxv);
  pq.push({1, s, 0});
  dp[1][s] = 0;
  while (pq.size()) {
    auto x = pq.top();
    pq.pop();
    if (dp[x.to][x.cost] < x.dis)
      continue;
    for (auto y : g[x.to]) {
      for (i = 0;; i++) {
        if (x.cost + i * c[x.to] < y.cost)
          continue;
        if (dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)] >
            dp[x.to][x.cost] + i * d[x.to] + y.dis) {
          dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)] =
              dp[x.to][x.cost] + i * d[x.to] + y.dis;
          pq.push({y.to, min(x.cost + i * c[x.to] - y.cost, maxv),
                   dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)]});
        }
        if (x.cost + i * c[x.to] - y.cost > maxv)
          break;
      }
    }
  }
  for (i = 2; i <= n; i++)
    printf("%lld\\n", *min_element(dp[i], dp[i] + maxv));
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#include <algorithm>
#include <queue>
#include <stdio.h>
#include <vector>
using namespace std;
struct Edge {
  long long to, cost,
      dis; // used in state is cur_v,cur_money,distance with node 1
};
vector<Edge> g[55];
long long c[55], d[55], dp[55][2550], maxv = 2500;
struct cmp {
  bool operator()(const Edge a, const Edge b) const { return a.dis > b.dis; }
};
priority_queue<Edge, vector<Edge>, cmp> pq;
int main() {
  long long i, j, n, m, s, u, v, a, b;
  scanf("%lld%lld%lld", &n, &m, &s);
  for (i = 0; i < m; i++) {
    scanf("%lld%lld%lld%lld", &u, &v, &a, &b);
    g[u].emplace_back((Edge){v, a, b});
    g[v].emplace_back((Edge){u, a, b});
  }
  for (i = 1; i <= n; i++)
    scanf("%lld%lld", &c[i], &d[i]);
  for (i = 0; i < 55; i++)
    for (j = 0; j < 2550; j++)
      dp[i][j] = 100000000000000000;
  s = min(s, maxv);
  pq.push({1, s, 0});
  dp[1][s] = 0;
  while (pq.size()) {
    auto x = pq.top();
    pq.pop();
    for (auto y : g[x.to]) {
      for (i = 0;; i++) {
        if (x.cost + i * c[x.to] < y.cost)
          continue;
        if (dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)] >
            dp[x.to][x.cost] + i * d[x.to] + y.dis) {
          dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)] =
              dp[x.to][x.cost] + i * d[x.to] + y.dis;
          pq.push({y.to, min(x.cost + i * c[x.to] - y.cost, maxv),
                   dp[y.to][min(x.cost + i * c[x.to] - y.cost, maxv)]});
        }
        if (x.cost + i * c[x.to] - y.cost > maxv)
          break;
      }
    }
  }
  for (i = 2; i <= n; i++)
    printf("%lld\\n", *min_element(dp[i], dp[i] + maxv));
}