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

/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Two Currencies
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_M = 100;
const int MAX_A = 50;
const int MAX_S = MAX_N * MAX_A;
const unsigned long long LINF = ~0ULL;

/* typedef */

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

struct Stat {
  ull d;
  int i, s;
  Stat() {}
  Stat(ull _d, int _i, int _s) : d(_d), i(_i), s(_s) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

vpii nbrs[MAX_N];
int as[MAX_M], bs[MAX_M], cs[MAX_N], ds[MAX_N];
ull dists[MAX_N][MAX_S + 1];

/* subroutines */

/* main */

int main() {
  int n, m, s;
  scanf("%d%d%d", &n, &m, &s);

  int maxa = 0;
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d%d%d", &u, &v, as + i, bs + i);
    u--, v--;
    nbrs[u].push_back(pii(v, i));
    nbrs[v].push_back(pii(u, i));
    if (maxa < as[i])
      maxa = as[i];
  }
  int maxs = maxa * n;
  s = min(s, maxs);

  for (int i = 0; i < n; i++)
    scanf("%d%d", cs + i, ds + i);

  for (int i = 0; i < n; i++)
    fill(dists[i], dists[i] + maxs + 1, LINF);

  dists[0][s] = 0;
  priority_queue<Stat> q;
  q.push(Stat(0, 0, s));

  while (!q.empty()) {
    Stat u = q.top();
    q.pop();
    int ci = cs[u.i], di = ds[u.i];
    vpii &nbru = nbrs[u.i];

    for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int vi = vit->first, ae = as[vit->second], be = bs[vit->second];
      ull vd = u.d + be;
      for (int vs = u.s - ae;; vs += ci, vd += di) {
        if (vs > maxs)
          vs = maxs;
        if (vs >= 0 && dists[vi][vs] > vd) {
          dists[vi][vs] = vd;
          q.push(Stat(vd, vi, vs));
        }
        if (vs >= maxs)
          break;
      }
    }
  }

  for (int i = 1; i < n; i++) {
    ull mind = LINF;
    for (int j = 0; j <= maxs; j++)
      if (mind > dists[i][j])
        mind = dists[i][j];
    printf("%llu\\n", mind);
  }
  return 0;
}


Error: 0

Fixed submission:

/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Two Currencies
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_N = 50;
const int MAX_M = 100;
const int MAX_A = 50;
const int MAX_S = MAX_N * MAX_A;
const unsigned long long LINF = ~0ULL;

/* typedef */

typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef vector<pii> vpii;

struct Stat {
  ull d;
  int i, s;
  Stat() {}
  Stat(ull _d, int _i, int _s) : d(_d), i(_i), s(_s) {}
  bool operator<(const Stat &s) const { return d > s.d; }
};

/* global variables */

vpii nbrs[MAX_N];
int as[MAX_M], bs[MAX_M], cs[MAX_N], ds[MAX_N];
ull dists[MAX_N][MAX_S + 1];

/* subroutines */

/* main */

int main() {
  int n, m, s;
  scanf("%d%d%d", &n, &m, &s);

  int asum = 0;
  for (int i = 0; i < m; i++) {
    int u, v;
    scanf("%d%d%d%d", &u, &v, as + i, bs + i);
    u--, v--;
    nbrs[u].push_back(pii(v, i));
    nbrs[v].push_back(pii(u, i));
    asum += as[i];
  }
  s = min(s, asum);

  for (int i = 0; i < n; i++)
    scanf("%d%d", cs + i, ds + i);

  for (int i = 0; i < n; i++)
    fill(dists[i], dists[i] + asum + 1, LINF);

  dists[0][s] = 0;
  priority_queue<Stat> q;
  q.push(Stat(0, 0, s));

  while (!q.empty()) {
    Stat u = q.top();
    q.pop();
    int ci = cs[u.i], di = ds[u.i];
    vpii &nbru = nbrs[u.i];

    for (vpii::iterator vit = nbru.begin(); vit != nbru.end(); vit++) {
      int vi = vit->first, ae = as[vit->second], be = bs[vit->second];
      ull vd = u.d + be;
      for (int vs = u.s - ae;; vs += ci, vd += di) {
        if (vs > asum)
          vs = asum;
        if (vs >= 0 && dists[vi][vs] > vd) {
          dists[vi][vs] = vd;
          q.push(Stat(vd, vi, vs));
        }
        if (vs >= asum)
          break;
      }
    }
  }

  for (int i = 1; i < n; i++) {
    ull mind = LINF;
    for (int j = 0; j <= asum; j++)
      if (mind > dists[i][j])
        mind = dists[i][j];
    printf("%llu\\n", mind);
  }
  return 0;
}