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

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

const ll INF = (1LL << 60);

int n, m, s;
ll C[50], D[50];
vector<vector<pair<ll, ll>>> a_lis, b_lis;

void input() {
  cin >> n >> m >> s;
  a_lis.resize(n);
  b_lis.resize(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    a_lis[u].emplace_back(v, a);
    a_lis[v].emplace_back(u, a);
    b_lis[u].emplace_back(v, b);
    b_lis[v].emplace_back(u, b);
  }
  for (int i = 0; i < n; ++i) {
    ll c, d;
    cin >> c >> d;
    C[i] = c, D[i] = d;
  }
}

template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  const int n = G.size();
  vector<T> d(n, numeric_limits<T>::max());
  vector<int> b(n, -1);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    pair<T, int> p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        b[u] = v;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

void solve() {
  if (s >= 2500)
    s = 2500;
  vector<vector<pair<int, ll>>> g(n * 2501);
  for (int i = 0; i < n; ++i) {
    ll c = C[i], d = D[i];
    // 両替
    for (int mo = 0; mo < 2500; ++mo) {
      int p = 2501 * i + mo;
      int money = (int)min(2500LL, mo + c);
      int nx = 2501 * i + money;
      g[p].emplace_back(nx, d);
    }
    // 移動
    for (int j = 0; j < (int)(a_lis[i].size()); ++j) {
      int nx_idx = a_lis[i][j].first;
      ll a = a_lis[i][j].second;
      ll b = b_lis[i][j].second;
      for (int mo = a; mo <= 2500; ++mo) {
        int p = 2501 * i + mo;
        int nx = 2501 * nx_idx + mo - a;
        g[p].emplace_back(nx, b);
      }
    }
  }
  auto res = dijkstra<ll>(s, g);
  for (int i = 1; i < n; ++i) {
    ll tmp = INF;
    for (int id = 2501 * i; id < 2501 * (i + 1); ++id)
      tmp = min(tmp, res[id]);
    cout << tmp << endl;
  }
}

int main() {
  input();
  solve();
  return 0;
}

Error: -6
free(): invalid next size (fast)


Fixed submission:

#pragma GCC optimize("Ofast", "unroll-loops")

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld long double

const ll INF = (1LL << 60);

int n, m, s;
ll C[50], D[50];
vector<vector<pair<ll, ll>>> a_lis, b_lis;

void input() {
  cin >> n >> m >> s;
  a_lis.resize(n);
  b_lis.resize(n);
  for (int i = 0; i < m; ++i) {
    int u, v;
    ll a, b;
    cin >> u >> v >> a >> b;
    --u, --v;
    a_lis[u].emplace_back(v, a);
    a_lis[v].emplace_back(u, a);
    b_lis[u].emplace_back(v, b);
    b_lis[v].emplace_back(u, b);
  }
  for (int i = 0; i < n; ++i) {
    ll c, d;
    cin >> c >> d;
    C[i] = c, D[i] = d;
  }
}

template <typename T>
vector<T> dijkstra(int s, vector<vector<pair<int, T>>> &G) {
  vector<T> d(n, numeric_limits<T>::max());
  vector<int> b(n, -1);
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> q;
  d[s] = 0;
  q.emplace(d[s], s);
  while (!q.empty()) {
    pair<T, int> p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    for (auto &e : G[v]) {
      int u = e.first;
      T c = e.second;
      if (d[u] > d[v] + c) {
        d[u] = d[v] + c;
        b[u] = v;
        q.emplace(d[u], u);
      }
    }
  }
  return d;
}

void solve() {
  if (s >= 2500)
    s = 2500;
  vector<vector<pair<int, ll>>> g(n * 2501);
  for (int i = 0; i < n; ++i) {
    ll c = C[i], d = D[i];
    // 両替
    for (int mo = 0; mo < 2500; ++mo) {
      int p = 2501 * i + mo;
      int money = (int)min(2500LL, mo + c);
      int nx = 2501 * i + money;
      g[p].emplace_back(nx, d);
    }
    // 移動
    for (int j = 0; j < a_lis[i].size(); ++j) {
      int nx_idx = a_lis[i][j].first;
      ll a = a_lis[i][j].second;
      ll b = b_lis[i][j].second;
      for (int mo = a; mo <= 2500; ++mo) {
        int p = 2501 * i + mo;
        int nx = 2501 * nx_idx + mo - a;
        g[p].emplace_back(nx, b);
      }
    }
  }
  auto res = dijkstra<ll>(s, g);
  for (int i = 1; i < n; ++i) {
    ll tmp = INF;
    for (int id = 2501 * i; id < 2501 * (i + 1); ++id)
      tmp = min(tmp, res[id]);
    cout << tmp << endl;
  }
}

int main() {
  input();
  solve();
  return 0;
}