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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

vector<int> g[5005];
vector<int> G[5005];
int C[5005], R[5005];
bool flag[5005];

void dfs(int base, int now, int dis, int before) {
  if (dis >= R[base])
    return;
  // if(flag[now]) return;
  flag[now] = true;
  REP(i, g[now].size()) {
    if (g[now][i] == before)
      continue;
    G[base].push_back(g[now][i]);
    dfs(base, g[now][i], dis + 1, now);
  }
}

struct state {
  int now, dis, before;
};

int main(void) {
  int N, K;
  cin >> N >> K;
  REP(i, N) { cin >> C[i] >> R[i]; }
  REP(i, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, N) {
    memset(flag, 0, sizeof flag);
    queue<state> q;
    q.push({i, 0, i});
    while (q.size()) {
      state s = q.front();
      q.pop();
      if (s.dis >= R[i])
        continue;
      if (flag[s.now])
        continue;
      flag[s.now] = true;
      REP(j, g[s.now].size()) {
        if (g[s.now][j] == s.before)
          continue;
        G[i].push_back(g[s.now][j]);
        q.push({g[s.now][j], s.dis + 1, s.now});
      }
    }
  }
  priority_queue<PII, vector<PII>, greater<PII>> pq;
  ll d[5005];
  fill(d, d + 5005, INF_LL);
  d[0] = 0;
  pq.push({0, 0});
  while (pq.size()) {
    PII p = pq.top();
    pq.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      if (d[G[v][i]] > d[v] + C[v]) {
        d[G[v][i]] = d[v] + C[v];
        pq.push({d[G[v][i]], G[v][i]});
      }
    }
  }
  cout << d[N - 1] << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

#define INF_LL 1e18
#define INF 1e9

#define REP(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(x) x.begin(), x.end()

#define fst first
#define snd second

using namespace std;

using ll = long long;
using PII = pair<int, int>;

vector<int> g[5005];
vector<PII> G[5005];
int C[5005], R[5005];

void dfs(int base, int now, int dis, int before) {
  if (dis >= R[base])
    return;
  REP(i, g[now].size()) {
    if (g[now][i] == before)
      continue;
    G[base].push_back({C[base], g[now][i]});
    dfs(base, g[now][i], dis + 1, now);
  }
}

int main(void) {
  int N, K;
  cin >> N >> K;
  REP(i, N) { cin >> C[i] >> R[i]; }
  REP(i, K) {
    int a, b;
    cin >> a >> b;
    a--;
    b--;
    g[a].push_back(b);
    g[b].push_back(a);
  }
  REP(i, N) { dfs(i, i, 0, i); }
  priority_queue<PII, vector<PII>, greater<PII>> pq;
  ll d[5005];
  fill(d, d + 5005, INF_LL);
  d[0] = 0;
  pq.push({0, 0});
  while (pq.size()) {
    PII p = pq.top();
    pq.pop();
    int v = p.second;
    if (d[v] < p.first)
      continue;
    REP(i, G[v].size()) {
      if (d[G[v][i].second] > d[v] + G[v][i].first) {
        d[G[v][i].second] = d[v] + G[v][i].first;
        pq.push({d[G[v][i].second], G[v][i].second});
      }
    }
  }
  cout << d[N - 1] << endl;
  return 0;
}