alexjercan/bugnet huggingface dataset:
Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> pl3;

const ll max_n = 52;
const ll max_s = 2500;
vector<ll> c, d;
vector<pl3> g[max_n];

ll n, m;
ll s;

const ll INF = 1e15;
ll dp[max_n + 1][max_s + 1];

void dijkstra() {
  s = min(s, max_s);
  rep(i, n) rep(j, max_s + 1) dp[i][j] = INF;
  dp[0][s] = 0;

  priority_queue<pl3, vector<pl3>, greater<pl3>> que;
  que.push({0, {0, s}});

  while (!que.empty()) {
    pl3 p = que.top();
    que.pop();

    ll time = p.first;
    ll v = p.second.first;
    ll ns = p.second.second;

    if (dp[v][ns] < time)
      continue; // 後から最短パスが見つかった場合、先にqueに入っているものはここではじかれる

    if (ns + c[v] <= max_s && dp[v][ns + c[v]] > time + d[v]) {
      dp[v][ns + c[v]] = time + d[v];
      que.push({time + d[v], {v, ns + c[v]}});
    }

    for (pl3 x : g[v]) {
      ll w = x.first;
      ll a = x.second.first;
      ll b = x.second.second;

      if (ns < a || dp[w][ns - a] <= time + b)
        continue;
      dp[w][ns - a] = time + b;
      que.push({time + b, {w, ns - a}});
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m >> s;
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].push_back({v, {a, b}});
    g[v].push_back({u, {a, b}});
  }

  c.resize(n);
  d.resize(n);
  rep(i, n) cin >> c[i] >> d[i];

  dijkstra();

  rep(i, n - 1) {
    ll ans = INF;
    rep(j, max_s + 1) ans = min(ans, dp[i + 1][j]);
    cout << ans << "\\n";
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = (0); i < (n); i++)

using namespace std;

typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<ll, pll> pl3;

const ll max_n = 52;
const ll max_s = 2500;
vector<ll> c, d;
vector<pl3> g[max_n];

ll n, m;
ll s;

const ll INF = 1e15;
ll dp[max_n + 1][max_s + 1];

void dijkstra() {
  s = min(s, max_s);
  rep(i, n) rep(j, max_s + 1) dp[i][j] = INF;
  dp[0][s] = 0;

  priority_queue<pl3, vector<pl3>, greater<pl3>> que;
  que.push({0, {0, s}});

  while (!que.empty()) {
    pl3 p = que.top();
    que.pop();

    ll time = p.first;
    ll v = p.second.first;
    ll ns = p.second.second;

    if (dp[v][ns] < time)
      continue; // 後から最短パスが見つかった場合、先にqueに入っているものはここではじかれる

    if (ns + c[v] <= max_s) {
      que.push({time + d[v], {v, ns + c[v]}});
    }

    for (pl3 x : g[v]) {
      ll w = x.first;
      ll a = x.second.first;
      ll b = x.second.second;

      if (ns < a || dp[w][ns - a] <= time + b)
        continue;
      dp[w][ns - a] = time + b;
      que.push({time + b, {w, ns - a}});
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> n >> m >> s;
  rep(i, m) {
    ll u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    g[u].push_back({v, {a, b}});
    g[v].push_back({u, {a, b}});
  }

  c.resize(n);
  d.resize(n);
  rep(i, n) cin >> c[i] >> d[i];

  dijkstra();

  rep(i, n - 1) {
    ll ans = INF;
    rep(j, max_s + 1) ans = min(ans, dp[i + 1][j]);
    cout << ans << "\\n";
  }
}
Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using vec = vector<pair<int, int64_t>>;
using graph = vector<vec>;
constexpr int64_t inf = 1e18;

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  vector<int> u(m), v(m), a(m), b(m), c(n), d(n);
  for (int i = 0; i < m; i++) {
    cin >> u.at(i) >> v.at(i) >> a.at(i) >> b.at(i);
    u.at(i)--;
    v.at(i)--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c.at(i) >> d.at(i);
  }
  graph to(50 * n * n);
  for (int i = 0; i < m; i++) {
    for (int j = a.at(i); j < 50 * n; j++) {
      to.at(50 * n * u.at(i) + j)
          .emplace_back(50 * n * v.at(i) + j - a.at(i), b.at(i));
      to.at(50 * n * v.at(i) + j)
          .emplace_back(50 * n * u.at(i) + j - a.at(i), b.at(i));
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 50 * n; j++) {
      int next = min(50 * n - 1, j + c.at(i));
      to.at(50 * n * i + j).emplace_back(50 * n * i + next, d.at(i));
    }
  }
  vector<int64_t> cost(50 * n * n, inf);
  priority_queue<pair<int64_t, int>> q;
  q.emplace(0, min(s, 50 * n - 1));
  while (!q.empty()) {
    auto x = q.top();
    q.pop();
    if (cost.at(x.second) < inf)
      continue;
    cost.at(x.second) = -x.first;
    for (auto next : to.at(x.second)) {
      if (cost.at(next.first) < inf)
        continue;
      q.emplace(x.first - next.second, next.first);
    }
  }
  for (int i = 1; i < n; i++) {
    int64_t ans = inf;
    for (int j = 0; j < 50 * n; j++) {
      ans = min(ans, cost.at(50 * n * i + j));
    }
    cout << ans << endl;
  }
}


Error: TLE

Fixed submission:

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

using vec = vector<pair<int, int64_t>>;
using graph = vector<vec>;
constexpr int64_t inf = 1e18;

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  vector<int> u(m), v(m), a(m), b(m), c(n), d(n);
  for (int i = 0; i < m; i++) {
    cin >> u.at(i) >> v.at(i) >> a.at(i) >> b.at(i);
    u.at(i)--;
    v.at(i)--;
  }
  for (int i = 0; i < n; i++) {
    cin >> c.at(i) >> d.at(i);
  }
  graph to(50 * n * n);
  for (int i = 0; i < m; i++) {
    for (int j = a.at(i); j < 50 * n; j++) {
      to.at(50 * n * u.at(i) + j)
          .emplace_back(50 * n * v.at(i) + j - a.at(i), b.at(i));
      to.at(50 * n * v.at(i) + j)
          .emplace_back(50 * n * u.at(i) + j - a.at(i), b.at(i));
    }
  }
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < 50 * n; j++) {
      int next = min(50 * n - 1, j + c.at(i));
      to.at(50 * n * i + j).emplace_back(50 * n * i + next, d.at(i));
    }
  }
  vector<int64_t> cost(50 * n * n, inf);
  priority_queue<pair<int64_t, int>> q;
  q.emplace(0, min(s, 50 * n - 1));
  while (!q.empty()) {
    auto x = q.top();
    q.pop();
    cost.at(x.second) = min(cost.at(x.second), -x.first);
    for (auto next : to.at(x.second)) {
      if (cost.at(next.first) < inf)
        continue;
      q.emplace(x.first - next.second, next.first);
    }
  }
  for (int i = 1; i < n; i++) {
    int64_t ans = inf;
    for (int j = 0; j < 50 * n; j++) {
      ans = min(ans, cost.at(50 * n * i + j));
    }
    cout << ans << endl;
  }
}
Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct E {
  int to, co, ti;
};
struct P {
  int tim, idx, res;
};
bool operator<(const P &a, const P &b) { return a.tim < b.tim; }
bool operator>(const P &a, const P &b) {
  if (a.tim != b.tim)
    return a.tim > b.tim;
  return a.res < b.res;
}
int ans[50][5001];
const long long LINF = 1e18;
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

signed main() {
  memset(ans, -1, sizeof(ans));
  int n, m, s;
  cin >> n >> m >> s;
  chmin(s, 5000LL);
  vector<E> edge[n];
  while (m--) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    edge[u].push_back({v, a, b});
    edge[v].push_back({u, a, b});
  }
  vector<int> c(n), d(n);
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, 0, s});
  vector<int> fans(n, -1);
  int cnt = 0;
  while (que.size()) {
    P p = que.top();
    que.pop(); // cout<<p.idx<<" "<<p.res<<" "<<p.tim<<" "<<cnt<<endl;
    if (~ans[p.idx][p.res])
      continue;
    ans[p.idx][p.res] = p.tim;
    if (fans[p.idx] < 0) {
      fans[p.idx] = p.tim;
      if (++cnt == n)
        break;
    }
    for (auto q : edge[p.idx]) {
      if (p.res < q.co || ~ans[q.to][p.res - q.co])
        continue;
      que.push({p.tim + q.ti, q.to, p.res - q.co});
    }
    if (p.res + c[p.idx] < 5000)
      que.push({p.tim + d[p.idx], p.idx, p.res + c[p.idx]});
  }
  for (int i = 1; i < n; i++)
    cout << fans[i] << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
struct E {
  int to, co, ti;
};
struct P {
  int tim, idx, res;
};
bool operator<(const P &a, const P &b) { return a.tim < b.tim; }
bool operator>(const P &a, const P &b) {
  if (a.tim != b.tim)
    return a.tim > b.tim;
  return a.res < b.res;
}
int ans[50][5001];
const long long LINF = 1e18;
template <typename T> void chmin(T &a, T b) {
  if (a > b)
    a = b;
}

signed main() {
  memset(ans, -1, sizeof(ans));
  int n, m, s;
  cin >> n >> m >> s;
  chmin(s, 5000LL);
  vector<E> edge[n];
  while (m--) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    u--;
    v--;
    edge[u].push_back({v, a, b});
    edge[v].push_back({u, a, b});
  }
  vector<int> c(n), d(n);
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, 0, s});
  vector<int> fans(n, -1);
  int cnt = 0;
  while (que.size()) {
    P p = que.top();
    que.pop(); // cout<<p.idx<<" "<<p.res<<" "<<p.tim<<" "<<cnt<<endl;
    if (~ans[p.idx][p.res])
      continue;
    ans[p.idx][p.res] = p.tim;
    if (fans[p.idx] < 0) {
      fans[p.idx] = p.tim;
      if (++cnt == n)
        break;
    }
    do {
      for (auto q : edge[p.idx]) {
        if (p.res < q.co || ~ans[q.to][p.res - q.co])
          continue;
        que.push({p.tim + q.ti, q.to, p.res - q.co});
      }
      p.res += c[p.idx];
      p.tim += d[p.idx];
    } while (p.res < 5000);
  }
  for (int i = 1; i < n; i++)
    cout << fans[i] << endl;
}
Problem p02703 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const LL inf = 0x3f3f3f3f3f3f3f3f;
const int N = 55, M = 110;
struct Edge {
  int to, next;
  LL a, b;
} e[M * 2];
int head[N], idx;
LL c[N], d[N];
LL dis[N][2510];
int n, m, s;

void add(int u, int v, LL a, LL b) {
  e[++idx].to = v;
  e[idx].next = head[u];
  e[idx].a = a;
  e[idx].b = b;
  head[u] = idx;
}

void spfa() {
  queue<PII> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++) {
      dis[i][j] = inf;
    }
  }
  dis[1][s] = 0;
  q.push({1, s});
  while (!q.empty()) {
    int u = q.front().first, w = q.front().second;
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      LL a = e[i].a, b = e[i].b;
      if (w >= a && dis[v][w - a] > dis[u][w] + b) {
        dis[v][w - a] = dis[u][w] + b;
        q.push({v, w - a});
      }
    }
    if (dis[u][min(w + c[u], (LL)2500)] > dis[u][w] + d[u]) {
      dis[u][min(w + c[u], (LL)2500)] = dis[u][w] + d[u];
      q.push({u, min(w + c[u], (LL)2500)});
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &s);
  s = min(s, 2500);
  while (m--) {
    int u, v;
    LL a, b;
    scanf("%d%d%lld%lld", &u, &v, &a, &b);
    add(u, v, a, b);
    add(v, u, a, b);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &c[i], &d[i]);
  }
  spfa();
  for (int i = 2; i <= n; i++) {
    LL res = inf;
    for (int j = 0; j <= 2500; j++)
      res = min(res, dis[i][j]);
    printf("%lld\\n", res);
  }
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;

const LL inf = 0x3f3f3f3f3f3f3f3f;
const int N = 55, M = 110;
struct Edge {
  int to, next;
  LL a, b;
} e[M];
int head[N], idx;
LL c[N], d[N];
LL dis[N][2510];
int n, m, s;

void add(int u, int v, LL a, LL b) {
  e[++idx].to = v;
  e[idx].next = head[u];
  e[idx].a = a;
  e[idx].b = b;
  head[u] = idx;
}

void spfa() {
  queue<PII> q;
  for (int i = 1; i <= n; i++) {
    for (int j = 0; j <= 2500; j++) {
      dis[i][j] = inf;
    }
  }
  dis[1][s] = 0;
  q.push({1, s});
  while (!q.empty()) {
    int u = q.front().first, w = q.front().second;
    q.pop();
    for (int i = head[u]; i; i = e[i].next) {
      int v = e[i].to;
      LL a = e[i].a, b = e[i].b;
      if (w >= a && dis[v][w - a] > dis[u][w] + b) {
        dis[v][w - a] = dis[u][w] + b;
        q.push({v, w - a});
      }
    }
    if (dis[u][min(w + c[u], (LL)2500)] > dis[u][w] + d[u]) {
      dis[u][min(w + c[u], (LL)2500)] = dis[u][w] + d[u];
      q.push({u, min(w + c[u], (LL)2500)});
    }
  }
}

int main() {
  scanf("%d%d%d", &n, &m, &s);
  s = min(s, 2500);
  while (m--) {
    int u, v;
    LL a, b;
    scanf("%d%d%lld%lld", &u, &v, &a, &b);
    add(u, v, a, b);
    add(v, u, a, b);
  }
  for (int i = 1; i <= n; i++) {
    scanf("%lld %lld", &c[i], &d[i]);
  }
  spfa();
  for (int i = 2; i <= n; i++) {
    LL res = inf;
    for (int j = 0; j <= 2500; j++)
      res = min(res, dis[i][j]);
    printf("%lld\\n", res);
  }
  return 0;
}
Problem p02703 (C++) — buggy submission, verdict: Runtime Error

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

struct Edge {
  long to, c, t;
};

const long inf = 1e18;
const long mx = 5000;
long n, m, s;
vector<Edge> g[50];
long ca[50];
long ti[50];

vector<long> sub() {
  using PP = pair<long, long>;
  using TT = pair<long, PP>;

  priority_queue<TT, vector<TT>, greater<TT>> q;
  q.push(TT(0, PP(0, min(s, mx))));

  vector<vector<long>> dist(n, vector<long>(mx + 1, inf));
  while (!q.empty()) {
    auto tt = q.top();
    q.pop();
    long d = tt.first;
    long from = tt.second.first;
    long mon = tt.second.second;
    if (dist[from][mon] != inf)
      continue;
    dist[from][mon] = d;
    for (auto e : g[from]) {
      long to = e.to;
      long c = e.c;
      long t = e.t;
      if (mon >= c) {
        q.push(TT(d + t, PP(to, mon - c)));
      }
    }
    q.push(TT(d + ti[from], PP(from, min(mon + ca[from], mx))));
  }

  vector<long> ret(n);
  for (int i = 0; i < n; ++i) {
    long mn = inf;
    for (int j = 0; j <= mx; ++j) {
      mn = min(mn, dist[i][j]);
    }
    ret[i] = mn;
  }
  return ret;
}

int main() {
  cin >> n >> m >> s;
  for (int j = 0; j < m; ++j) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    g[u].push_back(Edge{v, a, b});
    g[v].push_back(Edge{u, a, b});
  }
  for (int i = 0; i < n; ++i) {
    cin >> ca[i] >> ti[i];
  }

  auto v = sub();
  for (int i = 1; i < n; ++i) {
    cout << v[i] << endl;
  }
}

Error: 0

Fixed submission:

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

struct Edge {
  long to, c, t;
};

const long inf = 1e18;
const long mx = 5000;
long n, m, s;
vector<Edge> g[50];
long ca[50];
long ti[50];

vector<long> sub() {
  using PP = pair<long, long>;
  using TT = pair<long, PP>;

  priority_queue<TT, vector<TT>, greater<TT>> q;
  q.push(TT(0, PP(0, min(s, mx))));

  vector<vector<long>> dist(n, vector<long>(mx + 1, inf));
  while (!q.empty()) {
    auto tt = q.top();
    q.pop();
    long d = tt.first;
    long from = tt.second.first;
    long mon = tt.second.second;
    if (dist[from][mon] != inf)
      continue;
    dist[from][mon] = d;
    for (auto e : g[from]) {
      long to = e.to;
      long c = e.c;
      long t = e.t;
      if (mon >= c) {
        q.push(TT(d + t, PP(to, mon - c)));
      }
    }
    q.push(TT(d + ti[from], PP(from, mon + ca[from])));
  }

  vector<long> ret(n);
  for (int i = 0; i < n; ++i) {
    long mn = inf;
    for (int j = 0; j <= mx; ++j) {
      mn = min(mn, dist[i][j]);
    }
    ret[i] = mn;
  }
  return ret;
}

int main() {
  cin >> n >> m >> s;
  for (int j = 0; j < m; ++j) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    g[u].push_back(Edge{v, a, b});
    g[v].push_back(Edge{u, a, b});
  }
  for (int i = 0; i < n; ++i) {
    cin >> ca[i] >> ti[i];
  }

  auto v = sub();
  for (int i = 1; i < n; ++i) {
    cout << v[i] << endl;
  }
}
Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

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

using i64 = int_fast64_t;
using f64 = double;
using p64 = pair<i64, i64>;
using t64 = tuple<i64, i64, i64>;

constexpr i64 INF = INT_FAST64_MAX / 2;
constexpr f64 EPS = DBL_EPSILON;

void solve() {
  i64 n, m, s;
  cin >> n >> m >> s;
  vector<vector<t64>> g(n);
  i64 lim = 0;
  for (i64 i = 0; i < m; i++) {
    i64 u, v, a, b;
    cin >> u >> v >> a >> b, u--, v--;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
    lim += a;
  }
  for (i64 i = 0; i < n; i++) {
    i64 c, d;
    cin >> c >> d;
    g[i].emplace_back(i, -c, d);
  }

  priority_queue<t64, vector<t64>, greater<>> pq;
  pq.emplace(0, 0, s);
  map<p64, i64> t;
  t[{0, s}] = 0;

  vector<i64> ans(n, -1);
  i64 u = n;
  while (!pq.empty()) {
    i64 time, town, coin;
    tie(time, town, coin) = pq.top();
    pq.pop();

    if (ans[town] == -1) {
      ans[town] = time;
      u--;
      if (u == 0)
        break;
    }

    for (auto &e : g[town]) {
      i64 to, cost, wait;
      tie(to, cost, wait) = e;
      i64 ncoin = coin - cost, ntime = time + wait;
      if (ncoin < 0 || (ncoin > coin && ncoin > lim && coin > lim))
        continue;
      if (t.find({to, ncoin}) != t.end() && t[{to, ncoin}] <= ntime)
        continue;
      t[{to, ncoin}] = ntime;
      pq.emplace(ntime, to, ncoin);
    }
  }

  for (i64 i = 1; i < n; i++)
    cout << ans[i] << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}


Error: TLE

Fixed submission:

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

using i64 = int_fast64_t;
using f64 = double;
using p64 = pair<i64, i64>;
using t64 = tuple<i64, i64, i64>;

constexpr i64 INF = INT_FAST64_MAX / 2;
constexpr f64 EPS = DBL_EPSILON;

void solve() {
  i64 n, m, s;
  cin >> n >> m >> s;
  vector<vector<t64>> g(n);
  i64 lim = 0;
  for (i64 i = 0; i < m; i++) {
    i64 u, v, a, b;
    cin >> u >> v >> a >> b, u--, v--;
    g[u].emplace_back(v, a, b);
    g[v].emplace_back(u, a, b);
    lim += a * 2;
  }
  for (i64 i = 0; i < n; i++) {
    i64 c, d;
    cin >> c >> d;
    g[i].emplace_back(i, -c, d);
  }

  priority_queue<t64, vector<t64>, greater<>> pq;
  pq.emplace(0, 0, s);
  map<p64, i64> t;
  t[{0, s}] = 0;

  vector<i64> ans(n, -1);
  i64 u = n;
  while (!pq.empty()) {
    i64 time, town, coin;
    tie(time, town, coin) = pq.top();
    pq.pop();

    if (ans[town] == -1) {
      ans[town] = time;
      u--;
      if (u == 0)
        break;
    }

    for (auto &e : g[town]) {
      i64 to, cost, wait;
      tie(to, cost, wait) = e;
      i64 ncoin = coin - cost, ntime = time + wait;
      if (ncoin < 0)
        continue;
      if (t.find({to, ncoin}) != t.end() && t[{to, ncoin}] <= ntime)
        continue;
      t[{to, ncoin}] = ntime;
      pq.emplace(ntime, to, ncoin);
    }
  }

  for (i64 i = 1; i < n; i++)
    cout << ans[i] << endl;
}

int main() {
  cin.tie(nullptr);
  ios_base::sync_with_stdio(false);
  cout << fixed << setprecision(15);
  solve();
}
Problem p02703 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
using LL = long long;
using ULL = unsigned long long;

const LL INF = 0x3f3f3f3f3f3f3f3fll;
const int MAX = 10000;

vector<vector<int>> e[53];
int c[53], d[53];
LL ans[53]{0, 1}, dis[51][10001];

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    e[u].push_back({v, a, b});
    e[v].push_back({u, a, b});
  }
  Rep(i, n) cin >> c[i] >> d[i];
  priority_queue<vector<LL>> q;
  memset(dis, 0x3f, sizeof dis);
  s = min(s, MAX);
  q.push({0, -s, 1});
  dis[1][s] = 0;
  int cnt = n - 1;
  while (true) {
    auto x = q.top();
    q.pop();
    int p = x[2];
    if (ans[p] == 0) {
      ans[p] = -x[0];
      if (--cnt == 0)
        break;
    }
    if (dis[p][-x[1]] < -x[0])
      continue;
    LL coin = min(c[p] - x[1], (LL)MAX), t = d[p] - x[0];
    if (t < dis[p][coin]) {
      q.push({-t, -coin, p});
      dis[p][coin] = t;
    }
    for (auto &e0 : e[p]) {
      coin = -(x[1] + e0[1]);
      t = e0[2] - x[0];
      if (coin >= 0 && t < dis[e0[0]][coin]) {
        q.push({-t, -coin, e0[0]});
        dis[e0[0]][coin] = t;
      }
    }
  }
  for (int i = 2; i <= n; ++i)
    cout << ans[i] << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define Rep(i, n) for (int i = 1; i <= (n); ++i)
using LL = long long;
using ULL = unsigned long long;

vector<vector<int>> e[53];
int c[53], d[53];
LL ans[53]{0, 1};

int main() {
  int n, m, s;
  cin >> n >> m >> s;
  rep(i, m) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    e[u].push_back({v, a, b});
    e[v].push_back({u, a, b});
  }
  Rep(i, n) cin >> c[i] >> d[i];
  priority_queue<vector<LL>> q;
  q.push({0, -s, 0, 1});
  int cnt = n - 1;
  while (true) {
    auto x = q.top();
    q.pop();
    int p = x[3];
    if (ans[p] == 0) {
      ans[p] = -x[0];
      if (--cnt == 0)
        break;
    }
    if (x[1] > -5000)
      q.push({x[0] - d[p], x[1] - c[p], x[2], p});
    for (auto &e0 : e[p]) {
      if (x[1] + e0[1] <= 0 && x[2] > -3 * n)
        q.push({x[0] - e0[2], x[1] + e0[1], x[2] - 1, e0[0]});
    }
  }
  for (int i = 2; i <= n; ++i)
    cout << ans[i] << endl;
  return 0;
}
Problem p02703 (C++) — buggy submission, verdict: Runtime Error

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

typedef long long ll;

const int max_mon = 2507;
const int max_V = 53;
const ll INF = 1e17;
ll dp[max_V + 2][max_mon + 2];
struct edge {
  int to, a, b;
};
typedef tuple<ll, int, int> mint;
vector<edge> g[max_V];
int money, i, j;
int C[max_V];
int D[max_V];
priority_queue<mint, vector<mint>, greater<mint>> q;

void prin(ll x, int v, int s) {
  if (s < 0)
    return;

  if (dp[v][s] <= x)
    return;

  dp[v][s] = x;

  q.push(mint(x, v, s));
}

int main() {
  int N, M;
  int c, r, y, h;
  cin >> N >> M >> money;
  money = min(money, max_mon);
  for (i = 0; i < M; i++) {

    cin >> c >> r >> y >> h;
    c--;
    r--;
    g[c].push_back({r, y, h});
    g[r].push_back({c, y, h});
  }
  for (i = 0; i < N; i++) {
    cin >> c >> r;
    C[i] = c;
    D[i] = r;
  }
  for (i = 0; i < max_V + 2; i++) {
    for (j = 0; j < max_mon + 2; j++) {
      dp[i][j] = INF;
    }
  }
  // cout << dp[1][0] << endl;
  prin(0, 0, money);
  // cout << dp[1][0] << endl;

  while (!q.empty()) {
    mint now = q.top();
    q.pop();
    ll x = get<0>(now);
    int v = get<1>(now);
    int s = get<2>(now);
    if (x != dp[v][s]) {
      continue;
    }
    // if(x+D[v]==0) cout << v << endl;
    prin(x + D[v], v, min(s + C[v], max_mon));
    int k = g[v].size();
    for (i = 0; i < k; i++) {
      edge e = g[v][i];
      // if(x+e.b==0)  cout << e.to << endl;
      /*cout << e.b << endl;*/
      prin(x + e.b, e.to, s - e.a);
    }
  }

  for (i = 1; i < N; i++) {
    ll ans = INF;
    for (j = 0; j < max_mon + 2; j++) {
      ans = min(ans, dp[i][j]);
      // cout << dp[i][j] << " " ;
    }
    cout << ans << endl;
    // cout << endl;
  }
}


Error: -11

Fixed submission:

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

typedef long long ll;

const int max_mon = 2507;
const int max_V = 53;
const ll INF = 1e18;
ll dp[max_V][max_mon];
struct edge {
  int to, a, b;
};
typedef tuple<ll, int, int> mint;
vector<edge> g[max_V];
int money, i, j;
int C[max_V];
int D[max_V];
priority_queue<mint, vector<mint>, greater<mint>> q;

void prin(ll x, int v, int s) {
  if (s < 0)
    return;

  if (dp[v][s] <= x)
    return;

  dp[v][s] = x;
  q.push(mint(x, v, s));
}

int main() {
  int N, M;
  int c, r, y, h;
  cin >> N >> M >> money;
  money = min(money, max_mon);
  for (i = 0; i < M; i++) {

    cin >> c >> r >> y >> h;
    c--;
    r--;
    g[c].push_back({r, y, h});
    g[r].push_back({c, y, h});
  }
  for (i = 0; i < N; i++) {
    cin >> c >> r;
    C[i] = c;
    D[i] = r;
  }
  for (i = 0; i < max_V; i++) {
    for (j = 0; j < max_mon; j++) {
      dp[i][j] = INF;
    }
  }
  prin(0, 0, money);
  int l = 0;
  while (!q.empty()) {
    mint now = q.top();
    q.pop();
    ll x = get<0>(now);
    int v = get<1>(now);
    int s = get<2>(now);
    if (x > dp[v][s]) {
      continue;
    }
    prin(x + D[v], v, s + C[v]);
    for (i = 0; i < g[v].size(); i++) {
      edge e = g[v][i];
      prin(x + e.b, e.to, s - e.a);
    }
  }
}