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

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
static const int INF = 1LL << 61;

static const int MAX_N = 5005;

struct S {
  int to, c, r;
  S(int p1, int p2, int p3) {
    to = p1;
    c = p2;
    r = p3;
  }
};

int C[MAX_N], R[MAX_N], d[MAX_N], n, k, visit[MAX_N];
vector<pii> G[MAX_N];
vector<int> tg[MAX_N];

void dijkstra() {
  fill(d, d + MAX_N, INF);
  d[1] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> q;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      pii e = G[v][i];

      if (d[e.first] < e.second + p.first)
        continue;

      d[e.first] = e.second + p.first;
      q.push(pii(d[e.first], e.first));
    }
  }

  cout << d[n] << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> C[i] >> R[i];
  }

  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    tg[a].PB(b);
    tg[b].PB(a);
  }

  for (int i = 1; i <= n; ++i) {
    memset(visit, 0, sizeof(visit));
    visit[i] = true;
    queue<S> q;
    for (int j = 0; j < tg[i].size(); ++j) {
      q.push(S(tg[i][j], C[i], R[i]));
    }

    while (q.size()) {
      S s = q.front();
      q.pop();
      int v = s.to;
      if (visit[v] || s.r == 0)
        continue;
      G[i].PB(pii(v, s.c));
      visit[v] = true;

      for (int j = 0; j < tg[v].size(); ++j) {
        q.push(S(tg[v][j], s.c, s.r - 1));
      }
    }
  }

  dijkstra();

  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define PB push_back
#define PPB pop_back
typedef pair<int, int> pii;
typedef pair<int, pii> pip;
static const int INF = 1LL << 61;

static const int MAX_N = 5005;

struct S {
  int to, c, r;
  S(int p1, int p2, int p3) {
    to = p1;
    c = p2;
    r = p3;
  }
};

int C[MAX_N], R[MAX_N], d[MAX_N], n, k;
vector<pii> G[MAX_N];
vector<int> tg[MAX_N];

void dijkstra() {
  fill(d, d + MAX_N, INF);
  d[1] = 0;
  priority_queue<pii, vector<pii>, greater<pii>> q;
  q.push(pii(0, 1));

  while (q.size()) {
    pii p = q.top();
    q.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;

    for (int i = 0; i < G[v].size(); ++i) {
      pii e = G[v][i];

      if (d[e.first] < e.second + p.first)
        continue;

      d[e.first] = e.second + p.first;
      q.push(pii(d[e.first], e.first));
    }
  }

  cout << d[n] << endl;
}

signed main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> n >> k;
  for (int i = 1; i <= n; ++i) {
    cin >> C[i] >> R[i];
  }

  for (int i = 0; i < k; ++i) {
    int a, b;
    cin >> a >> b;
    tg[a].PB(b);
    tg[b].PB(a);
  }

  for (int i = 1; i <= n; ++i) {
    queue<S> q;
    for (int j = 0; j < tg[i].size(); ++j) {
      q.push(S(tg[i][j], C[i], R[i]));
    }

    while (q.size()) {
      S s = q.front();
      q.pop();
      if (s.r == 0)
        continue;
      int v = s.to;
      G[i].PB(pii(v, s.c));

      for (int j = 0; j < tg[v].size(); ++j) {
        q.push(S(tg[v][j], s.c, s.r - 1));
      }
    }
  }

  dijkstra();

  return 0;
}