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

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long

#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using vi = vector<int>;
using pii = pair<int, int>;

bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
int read() {
  int a;
  scanf("%lld", &a);
  return a;
}

const int inf = 3e18;

struct edge {
  int to, cost, dist;
  edge(int to, int cost, int dist) : to(to), cost(cost), dist(dist) {}
};

using Data = pair<pii, int>;

const int OK = 3000;

int N, M, S;
int C[55], D[55];
vector<edge> G[55];
int dist[55][3005]; // dist[i][j] := 頂点i, 残り銀貨j枚

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

  rep(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    G[u].PB(v, a, b);
    G[v].PB(u, a, b);
  }

  rep(i, N) {
    cin >> C[i] >> D[i];
    G[i].PB(i, -C[i], D[i]);
  }

  S = min(S, OK);

  rep(i, N) { rep(j, OK + 1) dist[i][j] = inf; }

  dist[0][S] = 0;

  priority_queue<Data, vector<Data>, greater<Data>> pq;

  pq.push(Data(pii(0, -S), 0));

  while (pq.size()) {
    Data data = pq.top();
    pq.pop();
    int d = data.first.first, s = -data.first.second;
    int cur = data.second;

    if (d > dist[cur][s])
      continue;

    for (edge &e : G[cur]) {
      if (s < e.cost)
        continue;

      int nxt = e.to;
      int nd = d + e.dist, ns = s - e.cost;

      if (s == OK or ns >= OK)
        ns = OK;

      if (dist[nxt][ns] > nd) {
        dist[nxt][ns] = nd;
        pq.push(Data(pii(nd, -ns), nxt));
      }
    }
  }

  for (int i = 1; i < N; ++i) {
    int mn = inf;
    rep(j, OK + 1) { chmin(mn, dist[i][j]); }
    printf("%lld\\n", mn);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define int long long
#define rep(i, n) for (int i = 0; i < (int)n; ++i)
#define rep1(i, n) for (int i = 1; i <= (int)n; ++i)
#define rrep(i, n) for (int i = (int)n - 1; i >= 0; --i)
#define rrep1(i, n) for (int i = n; i >= 1; --i)
#define range(i, l, r) for (int i = l; i < (int)r; ++i)
#define rrange(i, l, r) for (int i = (int)r - 1; i >= (int)l; --i)
#define unless(a) if (!(a))
#define all(a) begin(a), end(a)
#define fst first
#define scd second
#define PB emplace_back
#define PPB pop_back

using vi = vector<int>;
using pii = pair<int, int>;

bool chmin(int &a, int b) { return a > b ? (a = b, true) : false; }
bool chmax(int &a, int b) { return a < b ? (a = b, true) : false; }
int read() {
  int a;
  scanf("%lld", &a);
  return a;
}

const int inf = 3e18;

struct edge {
  int to, cost, dist;
  bool mouii;
  edge(int to, int cost, int dist) : to(to), cost(cost), dist(dist) {
    mouii = false;
  }
};

using Data = pair<pii, int>;

const int OK = 3000;

int N, M, S;
int C[55], D[55];
vector<pair<int, edge>> edges;
vector<edge> G[55];
int dist[55][3005]; // dist[i][j] := 頂点i, 残り銀貨j枚

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

  rep(i, M) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    edges.PB(u, edge(v, a, b));
    edges.PB(v, edge(u, a, b));
  }

  rep(i, N) { cin >> C[i] >> D[i]; }

  for (auto &aa : edges) {
    int from = aa.first;
    edge e = aa.second;
    rep(i, 2505) {
      G[from].PB(e.to, e.cost - C[from] * i, e.dist + D[from] * i);
    }
  }

  S = min(S, OK);

  rep(i, N) { rep(j, OK + 1) dist[i][j] = inf; }

  dist[0][S] = 0;

  priority_queue<Data, vector<Data>, greater<Data>> pq;

  pq.push(Data(pii(0, -S), 0));

  while (pq.size()) {
    Data data = pq.top();
    pq.pop();
    int d = data.first.first, s = -data.first.second;
    int cur = data.second;

    if (d > dist[cur][s])
      continue;

    for (edge &e : G[cur]) {
      if (s < e.cost)
        continue;

      int nxt = e.to;
      int nd = d + e.dist, ns = s - e.cost;

      if (s == OK or ns >= OK)
        ns = OK;

      if (dist[nxt][ns] > nd) {
        dist[nxt][ns] = nd;
        pq.push(Data(pii(nd, -ns), nxt));
      }
    }
  }

  for (int i = 1; i < N; ++i) {
    int mn = inf;
    rep(j, OK + 1) { chmin(mn, dist[i][j]); }
    printf("%lld\\n", mn);
  }
}