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

#include <bits/stdc++.h>

/* snippet: useful macros and functions */
#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
template <class T> T ant(const T &a, const T &b) { return a > b ? a : b; }
template <class T> T ont(const T &a, const T &b) { return a < b ? a : b; }
template <class T> T rng(const T &a, const T &b) {
  return a < b ? b - a : a - b;
}
template <class T> void th(T &a, const T &b, T (*f)(const T &, const T &)) {
  a = f(a, b);
}
#define anth(a, b) th(a, b, ant)
#define onth(a, b) th(a, b, ont)

/* snippet: modint library */
template <class T, int MOD> class Modint {
  T val;

public:
  Modint(T val = 0) : val(val % MOD + (val > 0 ? 0 : MOD)) {}
  operator T() { return val; }
  Modint &operator+=(const Modint &a) {
    val += a.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    val -= a.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    val = val * a.val % MOD;
    return *this;
  }
  void inverse() {
    T x[2] = {MOD, val}, a[2] = {0, 1};
    int i;
    for (i = 0; x[!i]; i ^= 1) {
      a[i] -= x[i] / x[!i] * a[!i];
      x[i] = x[i] % x[!i];
    }
    if (!i)
      a[i] += MOD;
    val = a[i];
  }
  Modint &operator/=(Modint a) {
    a.inverse();
    return *this *= a;
  }
  friend Modint modpow(Modint a, int n) {
    Modint ret(1);
    while (n) {
      if (n & 1)
        ret *= a;
      a *= a;
      n >>= 1;
    }
    return ret;
  }
  friend Modint operator+(Modint a, const Modint &b) { return a += b; }
  friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
  friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
  friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
  friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
    return os << a.val;
  }
};

/* snippet: using declarations */
using Int = long long;
using std::cin;
using std::cout;
using std::endl;
using Mint = Modint<long long, 1000000007>;

struct Edge {
  Int to;
  Int a, b;
};

int main() {
  Int n, m, s;
  cin >> n >> m >> s;

  Int as = 0;

  std::vector<std::vector<Edge>> edge(n);
  RC(i, m) {
    Int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    edge[u].push_back(Edge{v, a, b});
    edge[v].push_back(Edge{u, a, b});
    as += a;
  }
  if (s > as)
    s = as;
  ++as;

  std::vector<Int> c(n), d(n);
  RC(i, n) { cin >> c[i] >> d[i]; }

  std::vector<Int> ans(n * as, -1);
  std::priority_queue<std::pair<Int, Int>, std::vector<std::pair<Int, Int>>,
                      std::greater<std::pair<Int, Int>>>
      queue;
  queue.push(std::make_pair(0, s));
  while (!queue.empty()) {
    std::pair<Int, Int> top = queue.top();
    queue.pop();
    if (ans[top.second] == -1) {
      ans[top.second] = top.first;
      for (int i = top.second / as * as; i < top.second; ++i)
        if (ans[i] == -1)
          ans[i] = top.first;
      if (top.second / as == (top.second + c[top.second / as]) / as) {
        queue.push(std::make_pair(top.first + d[top.second / as],
                                  top.second + c[top.second / as]));
      }
      for (Edge &e : edge[top.second / as]) {
        if (top.second % as >= e.a) {
          queue.push(std::make_pair(top.first + e.b,
                                    e.to * as + top.second % as - e.a));
        }
      }
    }
  }

  ARC(i, 1, n) { cout << ans[i * as] << endl; }
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>

/* snippet: useful macros and functions */
#define ARC(i, a, b) for (Int i = (a); i < (b); ++i)
#define RC(i, n) ARC(i, 0, n)
#define ARCH(i, a, b) for (Int i = (a)-1; i >= (b); --i)
#define RCH(i, n) ARCH(i, n, 0)
template <class T> T ant(const T &a, const T &b) { return a > b ? a : b; }
template <class T> T ont(const T &a, const T &b) { return a < b ? a : b; }
template <class T> T rng(const T &a, const T &b) {
  return a < b ? b - a : a - b;
}
template <class T> void th(T &a, const T &b, T (*f)(const T &, const T &)) {
  a = f(a, b);
}
#define anth(a, b) th(a, b, ant)
#define onth(a, b) th(a, b, ont)

/* snippet: modint library */
template <class T, int MOD> class Modint {
  T val;

public:
  Modint(T val = 0) : val(val % MOD + (val > 0 ? 0 : MOD)) {}
  operator T() { return val; }
  Modint &operator+=(const Modint &a) {
    val += a.val;
    if (val >= MOD)
      val -= MOD;
    return *this;
  }
  Modint &operator-=(const Modint &a) {
    val -= a.val;
    if (val < 0)
      val += MOD;
    return *this;
  }
  Modint &operator*=(const Modint &a) {
    val = val * a.val % MOD;
    return *this;
  }
  void inverse() {
    T x[2] = {MOD, val}, a[2] = {0, 1};
    int i;
    for (i = 0; x[!i]; i ^= 1) {
      a[i] -= x[i] / x[!i] * a[!i];
      x[i] = x[i] % x[!i];
    }
    if (!i)
      a[i] += MOD;
    val = a[i];
  }
  Modint &operator/=(Modint a) {
    a.inverse();
    return *this *= a;
  }
  friend Modint modpow(Modint a, int n) {
    Modint ret(1);
    while (n) {
      if (n & 1)
        ret *= a;
      a *= a;
      n >>= 1;
    }
    return ret;
  }
  friend Modint operator+(Modint a, const Modint &b) { return a += b; }
  friend Modint operator-(Modint a, const Modint &b) { return a -= b; }
  friend Modint operator*(Modint a, const Modint &b) { return a *= b; }
  friend Modint operator/(Modint a, const Modint &b) { return a /= b; }
  friend std::ostream &operator<<(std::ostream &os, const Modint &a) {
    return os << a.val;
  }
};

/* snippet: using declarations */
using Int = long long;
using std::cin;
using std::cout;
using std::endl;
using Mint = Modint<long long, 1000000007>;

struct Edge {
  Int to;
  Int a, b;
};

int main() {
  Int n, m, s;
  cin >> n >> m >> s;

  Int as = 0;

  std::vector<std::vector<Edge>> edge(n);
  RC(i, m) {
    Int u, v, a, b;
    cin >> u >> v >> a >> b;
    --u;
    --v;
    edge[u].push_back(Edge{v, a, b});
    edge[v].push_back(Edge{u, a, b});
    as += a;
  }
  ++as;

  std::vector<Int> c(n), d(n);
  RC(i, n) { cin >> c[i] >> d[i]; }

  std::vector<Int> ans(n * as, -1);
  std::priority_queue<std::pair<Int, Int>, std::vector<std::pair<Int, Int>>,
                      std::greater<std::pair<Int, Int>>>
      queue;
  queue.push(std::make_pair(0, s));
  while (!queue.empty()) {
    std::pair<Int, Int> top = queue.top();
    queue.pop();
    if (ans[top.second] == -1) {
      ans[top.second] = top.first;
      if (top.second % as != as - 1)
        queue.push(std::make_pair(top.first + d[top.second / as],
                                  top.second + c[top.second / as]));
      for (Edge &e : edge[top.second / as]) {
        if (top.second % as >= e.a) {
          queue.push(std::make_pair(top.first + e.b,
                                    e.to * as + (top.second % as) - e.a));
        }
      }
    }
  }

  ARC(i, 1, n) { cout << ans[i * as] << endl; }
}