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

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int G[100003];
int couse[100003];

struct P {
  int p;
  int cost;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int main() {
  fill_n((int *)G, 100003, (int)1e9);
  int n;
  cin >> n;
  REP(i, n) { cin >> couse[i]; }

  G[0] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, 0});
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.p >= n - 1) {
      cout << p.cost << endl;
      break;
    }

    if (couse[p.p] == 0) {
      FOR(d, 1, 6 + 1) {
        int next_p = min(n, p.p + d);
        int next_c = p.cost + 1;
        if (G[next_p] > next_c) {
          G[next_p] = next_c;
          que.push({next_p, next_c});
        }
      }
    } else {
      int next_p = min(n, p.p + couse[p.p]);
      int next_c = p.cost;
      if (G[next_p] > next_c) {
        G[next_p] = next_c;
        que.push({next_p, next_c});
      }
    }
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
// #include <bits/stdc++.h>

using namespace std;

#define LOG(...) fprintf(stderr, __VA_ARGS__)
// #define LOG(...)
#define FOR(i, a, b) for (int i = (int)(a); i < (int)(b); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort(ALL(c))
#define RSORT(c) sort(RALL(c))
#define SQ(n) (n) * (n)

typedef long long ll;
typedef unsigned long long ull;
typedef vector<bool> vb;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<vb> vvb;
typedef vector<vi> vvi;
typedef vector<vll> vvll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

int G[10003];
int couse[10003];

struct P {
  int p;
  int cost;

  bool operator>(const P &p) const { return cost > p.cost; }
};

int main() {
  fill_n((int *)G, 10003, (int)1e9);
  int n;
  cin >> n;
  REP(i, n) { cin >> couse[i]; }

  G[0] = 0;
  priority_queue<P, vector<P>, greater<P>> que;
  que.push({0, 0});
  while (!que.empty()) {
    P p = que.top();
    que.pop();
    if (p.p >= n - 1) {
      cout << p.cost << endl;
      break;
    }

    if (couse[p.p] == 0) {
      FOR(d, 1, 6 + 1) {
        int next_p = min(n, p.p + d);
        int next_c = p.cost + 1;
        if (G[next_p] > next_c) {
          G[next_p] = next_c;
          que.push({next_p, next_c});
        }
      }
    } else {
      int next_p = min(n, p.p + couse[p.p]);
      int next_c = p.cost;
      if (G[next_p] > next_c) {
        G[next_p] = next_c;
        que.push({next_p, next_c});
      }
    }
  }
}