Problem p02703 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double
#define int long long
#define mp make_pair

mt19937 rnd(51);

const int N = 55;
vector<pair<int, pair<int, int>>> g[N];
int a[N][N * N], c[N], d[N];
vector<int> ans(N, 1e18);

void fill() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N * N; j++) {
      a[i][j] = 1e18;
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fill();
  int n, m, s, mx = 0;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].pb({v, {a, b}});
    g[v].pb({u, {a, b}});
    mx = max(mx, a);
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }
  a[1][s] = 0;
  set<pair<int, pair<int, int>>> st;
  st.insert({0, {1, s}});
  while (!st.empty()) {
    auto v = *st.begin();
    st.erase(st.begin());
    for (auto to : g[v.second.first]) {
      if (v.second.second >= to.second.first) {
        if (a[to.first][v.second.second - to.second.first] >
            v.first + to.second.second) {
          st.erase({a[to.first][v.second.second - to.second.first],
                    {to.first, v.second.second - to.second.first}});
          a[to.first][v.second.second - to.second.first] =
              v.first + to.second.second;
          st.insert({a[to.first][v.second.second - to.second.first],
                     {to.first, v.second.second - to.second.first}});
          ans[to.first] = min(ans[to.first], v.first + to.second.second);
        }
      }
    }
    int pl = d[v.second.first];
    for (int i = v.second.second + c[v.second.first]; i < mx * n;
         i += c[v.second.first]) {
      if (a[v.second.first][i] > a[v.second.first][v.second.second] + pl) {
        st.erase({a[v.second.first][i], {v.second.first, i}});
        a[v.second.first][i] = a[v.second.first][v.second.second] + pl;
        st.insert({a[v.second.first][i], {v.second.first, i}});
      }
      pl += d[v.second.first];
    }
  }
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}


Fixed submission:
#include <algorithm>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>

using namespace std;

#pragma GCC optimize("Ofast")
#pragma GCC optimize("fast-math")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,tune=native")

#define pb push_back
#define ld long double
#define int long long
#define mp make_pair

mt19937 rnd(51);

const int N = 55;
vector<pair<int, pair<int, int>>> g[N];
int a[N][N * N], c[N], d[N];
vector<int> ans(N, 1e18);

void fill() {
  for (int i = 0; i < N; i++) {
    for (int j = 0; j < N * N; j++) {
      a[i][j] = 1e18;
    }
  }
}

int32_t main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  fill();
  int n, m, s, mx = 0;
  cin >> n >> m >> s;
  for (int i = 0; i < m; i++) {
    int u, v, a, b;
    cin >> u >> v >> a >> b;
    g[u].pb({v, {a, b}});
    g[v].pb({u, {a, b}});
    mx = max(mx, a);
  }
  for (int i = 1; i <= n; i++) {
    cin >> c[i] >> d[i];
  }
  s = min(s, mx * n);
  a[1][s] = 0;
  set<pair<int, pair<int, int>>> st;
  st.insert({0, {1, s}});
  while (!st.empty()) {
    auto v = *st.begin();
    st.erase(st.begin());
    for (auto to : g[v.second.first]) {
      if (v.second.second >= to.second.first) {
        if (a[to.first][v.second.second - to.second.first] >
            v.first + to.second.second) {
          st.erase({a[to.first][v.second.second - to.second.first],
                    {to.first, v.second.second - to.second.first}});
          a[to.first][v.second.second - to.second.first] =
              v.first + to.second.second;
          st.insert({a[to.first][v.second.second - to.second.first],
                     {to.first, v.second.second - to.second.first}});
          ans[to.first] = min(ans[to.first], v.first + to.second.second);
        }
      }
    }
    int pl = d[v.second.first];
    for (int i = v.second.second + c[v.second.first]; i < mx * n;
         i += c[v.second.first]) {
      if (a[v.second.first][i] > a[v.second.first][v.second.second] + pl) {
        st.erase({a[v.second.first][i], {v.second.first, i}});
        a[v.second.first][i] = a[v.second.first][v.second.second] + pl;
        st.insert({a[v.second.first][i], {v.second.first, i}});
      }
      pl += d[v.second.first];
    }
  }
  for (int i = 2; i <= n; i++) {
    cout << ans[i] << endl;
  }
  return 0;
}