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

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define PI 3.14159265358979323846264338327950L

struct Node;

struct way {
  Node *to;
  ll cost;
  ll time;
};

struct Node {

  int num;
  int exCost;
  int extime;
  ll minTime = -1;
  set<way *> reachabl;
};

struct compareWay {
  bool operator()(const way *a, const way *b) const {
    return a->time > b->time;
  }
};

const int numNodes = 51;
const int maxSilv = 2500;
int n, m, s;
int u, v, aa, bb;

Node a[numNodes];
ll dp[numNodes][maxSilv];

void solve() {

  cin >> n >> m >> s;

  for (int i = 0; i < m; i++) {
    cin >> u >> v >> aa >> bb;
    u--;
    v--;

    way *k = new way();
    k->to = &a[u];
    k->cost = aa;
    k->time = bb;
    a[v].reachabl.insert(k);

    way *f = new way();
    f->to = &a[v];
    f->cost = aa;
    f->time = bb;
    a[u].reachabl.insert(f);
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> v;

    a[i].num = i;
    a[i].exCost = u;
    a[i].extime = v;
  }
  m = n;

  for (int i = 0; i < numNodes; i++) {
    for (int j = 0; j < maxSilv; j++) {
      dp[i][j] = -1;
    }
  }

  // dp[0][s] = 0;
  if (s > maxSilv)
    s = maxSilv;

  way *t = new way();
  t->to = &a[0];
  t->cost = s;
  t->time = 0;

  priority_queue<way *, vector<way *>, compareWay> next;

  next.push(t);

  while (!next.empty() && n > 0) {
    way *k = next.top();
    next.pop();

    if (dp[k->to->num][k->cost] == -1 || k->time < dp[k->to->num][k->cost]) {
      dp[k->to->num][k->cost] = k->time;

      if (k->to->minTime == -1) {
        k->to->minTime = k->time;
        n--;
      }

      for (auto o : k->to->reachabl) {

        if (o->cost <= k->cost) {
          way *t = new way();
          t->to = o->to;
          t->cost = k->cost - o->cost;
          t->time = k->time + o->time;
          next.push(t);
        }
      }

      way *t = new way();
      t->to = k->to;
      t->cost = k->cost + t->to->exCost;
      if (t->cost > maxSilv)
        t->cost = maxSilv;
      t->time = k->time + k->to->extime;
      next.push(t);
    }
    // free(k);
  }

  for (int i = 1; i < m; i++) {
    cout << a[i].minTime << endl;
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  solve();
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define PI 3.14159265358979323846264338327950L

struct Node;

struct way {
  Node *to;
  ll cost;
  ll time;
};

struct Node {

  int num;
  int exCost;
  int extime;
  ll minTime = -1;
  set<way *> reachabl;
};

struct compareWay {
  bool operator()(const way *a, const way *b) const {
    return a->time >= b->time;
  }
};

const int Nodes = 50;
const int maxSilv = 2500;
int n, m, s;
int u, v, aa, bb;

Node a[50];
ll dp[50][maxSilv];

void solve() {

  cin >> n >> m >> s;

  for (int i = 0; i < m; i++) {
    cin >> u >> v >> aa >> bb;
    u--;
    v--;

    way *k = new way();
    k->to = &a[u];
    k->cost = aa;
    k->time = bb;
    a[v].reachabl.insert(k);

    way *f = new way();
    f->to = &a[v];
    f->cost = aa;
    f->time = bb;
    a[u].reachabl.insert(f);
  }

  for (int i = 0; i < n; i++) {
    cin >> u >> v;

    a[i].num = i;
    a[i].exCost = u;
    a[i].extime = v;
  }
  m = n;

  for (int i = 0; i < 50; i++) {
    for (int j = 0; j < maxSilv; j++) {
      dp[i][j] = -1;
    }
  }

  // dp[0][s] = 0;
  if (s > maxSilv)
    s = maxSilv;

  way *t = new way();
  t->to = &a[0];
  t->cost = s;
  t->time = 0;

  priority_queue<way *, vector<way *>, compareWay> next;

  next.push(t);

  while (!next.empty() && n > 0) {
    way *k = next.top();
    next.pop();

    if (dp[k->to->num][k->cost] == -1 || k->time < dp[k->to->num][k->cost]) {
      dp[k->to->num][k->cost] = k->time;

      if (k->to->minTime == -1) {
        k->to->minTime = k->time;
        n--;
      }

      for (auto o : k->to->reachabl) {

        if (o->cost <= k->cost) {
          way *t = new way();
          t->to = o->to;
          t->cost = k->cost - o->cost;
          t->time = k->time + o->time;
          next.push(t);
        }
      }

      way *t = new way();
      t->to = k->to;
      t->cost = k->cost + t->to->exCost;
      if (t->cost > maxSilv)
        t->cost = maxSilv;
      t->time = k->time + k->to->extime;
      next.push(t);
    }
    // free(k);
  }

  for (int i = 1; i < m; i++) {
    cout << a[i].minTime << endl;
  }
}

int main() {

  ios::sync_with_stdio(false);
  cin.tie(0);

  solve();
}