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

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

void chmin(long long &a, long long b) {
  if (a > b)
    a = b;
}

template <typename T> struct Edge {
  int to;
  T cost;
  Edge(int to, T cost = 1) : to(to), cost(cost) {}
  bool operator<(const Edge &r) const { return (cost < r.cost); }
};

template <typename T> using Graph = vector<vector<Edge<T>>>;

template <typename T> vector<T> dijkstra(const Graph<T> &g, int s) {
  using P = pair<T, int>;
  vector<T> ret(g.size(), -1);
  priority_queue<P, vector<P>, greater<P>> que;
  que.emplace(ret[s], s);
  ret[s] = 0;
  while (not que.empty()) {
    int v;
    T c;
    tie(c, v) = que.top();
    que.pop();
    if (ret[v] < c)
      continue;
    for (const auto &e : g[v]) {
      if (ret[e.to] > ret[v] + e.cost or ret[e.to] == -1) {
        ret[e.to] = ret[v] + e.cost;
        que.emplace(ret[e.to], e.to);
      }
    }
  }
  return ret;
}

int main() {
  constexpr long long SIZE = 2550;
  constexpr long long MAX = 2525;
  long long n, m, s;
  cin >> n >> m >> s;
  s = min(s, MAX);

  Graph<long long> g(n * SIZE);

  auto idx = [&](long long v, long long x) {
    // 頂点 v で x 枚の銀貨を持っている
    assert(v + x * n < g.size());
    return v + x * n;
  };

  for (int i = 0; i < m; i++) {
    long long v, u, a, b;
    cin >> v >> u >> a >> b;
    v--;
    u--;
    for (long long x = 0; x <= MAX; x++)
      if (x - a >= 0) {
        g[idx(v, x)].emplace_back(idx(u, x - a), b);
        g[idx(u, x)].emplace_back(idx(v, x - a), b);
      }
  }

  for (long long i = 0; i < n; i++) {
    long long c, d;
    cin >> c >> d;
    for (long long x = 0; x <= MAX; x++) {
      long long to = min(MAX, x + c);
      g[idx(i, x)].emplace_back(idx(i, to), d);
    }
  }

  vector<long long> dist = dijkstra(g, idx(0, s));

  vector<long long> ans(n, numeric_limits<long long>::max());
  for (long long i = 0; i < n; i++) {
    for (long long j = 0; j <= MAX; j++) {
      if (dist[idx(i, j)] != -1) {
        chmin(ans[i], dist[idx(i, j)]);
      }
    }
  }

  for (int i = 1; i < n; i++) {
    cout << ans[i] << '\\n';
  }

  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
template <class T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

template <typename T> struct Edge {
  int to;
  T cost, sil;
  Edge(int to, T cost, T sil) : to(to), cost(cost), sil(sil) {}
  bool operator<(const Edge &r) const { return (cost < r.cost); }
};

template <typename T> using Graph = vector<vector<Edge<T>>>;

constexpr long long INF = (1LL << 60);

int n, m;
long long s;
Graph<long long> g;
vector<long long> c, d;
vector<long long> ans;

pair<long long, long long> calc(long long city, long long x) {
  const long long sz = x * 2;
  long long mon = 0, t = INF;
  vector<long long> dp(sz, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    if (city & (1LL << i)) {
      if (c[i] >= x) {
        if (chmin(t, d[i]))
          mon = c[i];
      } else {
        for (int j = c[i]; j < sz; j++) {
          if (j - c[i] >= 0)
            chmin(dp[j], dp[j - c[i]] + d[i]);
        }
      }
    }
  for (int i = x; i < sz; i++) {
    if (chmin(t, dp[i]))
      mon = i;
  }
  return make_pair(mon, t);
}

void dfs(int v, int p, int goal, long long t, long long coin, long long city) {
  if (v == goal) {
    chmin(ans[goal], t);
    return;
  }
  city |= (1LL << v);
  for (const auto &e : g[v])
    if (e.to != p) {
      if (e.sil <= coin)
        dfs(e.to, v, goal, t + e.cost, coin - e.sil, city);
      else {
        long long mon, ti;
        tie(mon, ti) = calc(city, e.sil - coin);
        assert(e.sil <= coin + mon);
        dfs(e.to, v, goal, t + ti, coin + mon - e.cost, city);
      }
    }
}

int main() {
  cin >> n >> m >> s;
  g.resize(n);
  c.resize(n);
  d.resize(n);
  ans.resize(n, INF);
  for (int i = 0; i < m; i++) {
    int v, u;
    long long a, b;
    cin >> v >> u >> a >> b;
    v--;
    u--;
    g[v].emplace_back(u, b, a);
    g[u].emplace_back(v, b, a);
  }
  for (int i = 0; i < n; i++)
    cin >> c[i] >> d[i];

  for (int v = 1; v < n; v++) {
    dfs(0, -1, v, 0, s, 0);
  }

  for (int i = 1; i < n; i++) {
    cout << ans[i] << '\\n';
  }

  return 0;
}