alexjercan/bugnet huggingface dataset:
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#define INF (1e9)
#define N 100001
using namespace std;

int main() {
  int n, p[N], dp[N];
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  for (int i = 0; i < n; i++)
    dp[i] = INF;
  dp[n - 1] = 0;
  for (int i = n - 1; i >= 0; i--) {
    if (p[i] > 0)
      dp[i] = min(dp[i], dp[i + p[i]]);
    if (!p[i])
      for (int j = 1; j <= 6; j++)
        if (i + j <= n - 1)
          dp[i] = min(dp[i], dp[i + j] + 1);
  }
  while (1) {
    int f = 0;
    for (int i = n - 1; i >= 0; i--) {
      if (p[i] > 0)
        if (dp[i] > dp[i + p[i]])
          dp[i] = dp[i + p[i]], f = 1;
      if (!p[i])
        for (int j = 1; j <= 6; j++)
          if (i + j <= n - 1)
            if (dp[i] > dp[i + j] + 1)
              dp[i] = dp[i + j] + 1, f = 1;
      if (p[i] < 0)
        if (dp[i] > dp[i + p[i]])
          dp[i] = dp[i + p[i]], f = 1;
    }
    if (!f)
      break;
  }
  cout << dp[0] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
#define INF (1e9)
#define N 100001
using namespace std;
int make_edge(int);
void cut(int);
int n, p[N], dp[N], to[N];
bool visited[N], node[N];

int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i], node[i] = true;
  for (int i = 0; i < n; i++)
    if (p[i])
      cut(i);
  for (int i = 0; i < n; i++)
    if (p[i] && !node[i + p[i]])
      node[i] = false;
  for (int i = 0; i < n; i++)
    if (node[i] && p[i])
      to[i] = make_edge(i);
  for (int i = 0; i < n - 1; i++)
    dp[i] = INF;
  dp[n - 1] = 0;
  for (int i = n - 2; i >= 0; i--) {
    if (!node[i])
      continue;
    if (p[i])
      dp[i] = dp[to[i]];
    else {
      for (int j = 1; j <= 6; j++) {
        if (i + j >= n - 1)
          dp[i] = 1;
        if (!node[i + j])
          continue;
        dp[i] = min(dp[i], dp[i + j] + 1);
      }
    }
  }
  cout << dp[0] << endl;
  return 0;
}

void cut(int x) {
  if (!p[x])
    return;
  if (visited[x]) {
    node[x] = false;
    return;
  }
  visited[x] = true;
  cut(x + p[x]);
  visited[x] = false;
}

int make_edge(int x) {
  if (!p[x])
    return x;
  return make_edge(x + p[x]);
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
using namespace std;

int main() {
  int N, p[100001], road[100001];
  queue<int> Q;

  cin >> N;
  for (int i = 0; i < N; i++) {
    cin >> p[i];
    road[i] = 10000000;
  }
  road[0] = 0;
  Q.push(0);
  while (!Q.empty()) {
    int a = Q.front();
    Q.pop();
    if (p[a] != 0) {
      if (road[a] < road[p[a] + a]) {
        Q.push(a + p[a]);
        road[p[a] + a] = road[a];
      }
    } else {
      for (int i = 1; i <= 6; i++) {
        if (a + i < N) {
          if (road[a + i] > road[a] + 1) {
            road[a + i] = road[a] + 1;
            Q.push(a + i);
          }
        }
      }
    }
  }
  cout << road[N - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int N, SR[200001], now, mi, len = 0, M[200001];
  vector<int> V;
  cin >> N;
  for (int i = 0; i < N; i++)
    cin >> SR[i];
  for (int i = 0; i < N; i++)
    M[i] = 1000002;
  M[0] = 0;
  for (int i = 1; i <= 6; i++) {
    for (int j = i; j < N; j++) {
      for (int k = 1; k <= i; k++) {
        if (SR[j - k] == 0)
          M[j] = min(M[j], M[j - k] + 1);
      }

      now = j;
      V.clear();
      V.push_back(now);
      while (SR[now] != 0) {
        now += SR[now];
        M[now] = min(M[j], M[now]);
        if (find(V.begin(), V.end(), now) != (V.end())) {
          while (!V.empty()) {
            now = V.front();
            M[now] = 1000003;
            V.erase(V.begin());
          }
          break;
        }
        V.push_back(now);
      }
    }
  }
  cout << M[N - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  cin >> n;
  vi in(n);
  rep(i, n) cin >> in[i];
  vi out(n, inf);
  out[0] = 0;
  vi t;
  t.pb(0);
  vector<bool> dame(n, 0);
  while (t.size()) {
    vi tt;
    rep(i, t.size()) {
      loop(j, 1, 7) {
        int d = t[i] + j;
        if (d < n && dame[d])
          continue;
        vector<bool> h(n, 0);
        if (d < n)
          while (in[d] && h[d] == 0) {
            h[d] = true;
            d += in[d];
          }
        if (d < n && h[d]) {
          rep(k, n) if (h[k]) dame[k] = true;
          continue;
        }
        if (d >= n) {
          out[n - 1] = min(out[n - 1], out[t[i]] + 1);
          continue;
        }
        if (out[d] > out[t[i]] + 1) {
          out[d] = out[t[i]] + 1;
          tt.pb(d);
        }
      }
    }
    t = tt;
  }
  cout << out[n - 1] << endl;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
const double PI = acos(-1);
const double EPS = 1e-10;
const int inf = 1e8;
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
int main() {
  int n;
  cin >> n;
  vi in(n);
  rep(i, n) cin >> in[i];
  vi out(n, inf);
  out[0] = 0;
  vi t;
  t.pb(0);
  while (t.size()) {
    vi tt;
    rep(i, t.size()) {
      loop(j, 1, 7) {
        int d = t[i] + j;
        vector<bool> h(n, 0);
        if (d < n)
          while (in[d] && h[d] == 0) {
            h[d] = true;
            d += in[d];
          }
        if (d < n && h[d])
          continue;
        if (d >= n) {
          out[n - 1] = min(out[n - 1], out[t[i]] + 1);
          continue;
        }
        if (out[d] > out[t[i]] + 1) {
          out[d] = out[t[i]] + 1;
          tt.pb(d);
        }
      }
    }
    t = tt;
  }
  cout << out[n - 1] << endl;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2332&lang=jp
typedef long long ll;
#define INF 1 << 30
using namespace std;

void dfs(int n, int v, queue<pair<int, int>> &q, vector<int> &c,
         vector<int> &masu) {
  if (c[n] <= v)
    return;
  c[n] = v;
  if (masu[n] == 0) {
    q.push({v, n});
  } else {
    dfs(n + masu[n], v, q, c, masu);
  }
}

void solve(int N, vector<int> &c, vector<int> &masu) {
  queue<pair<int, int>> q;
  q.push({0, 1});
  c[1] = 0;

  while (!q.empty()) {
    auto p = q.front();
    q.pop();
    int C = p.first;
    int i = p.second;
    for (int j = 1; j <= 6; j++) {
      if (masu[i] != 0)
        break;
      if (masu[min(i + j, N)] == 0) {
        if (c[min(i + j, N)] > c[i] + 1) {
          c[min(i + j, N)] = c[i] + 1;
          q.push({c[min(i + j, N)], i + j});
        }
      } else {
        dfs(i + j, c[i] + 1, q, c, masu);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> masu(N + 1, INF);
  vector<int> c(N + 1, INF);
  for (int i = 1; i <= N; i++)
    cin >> masu[i];
  solve(N, c, masu);
  cout << c[N] << endl;
}

Error: TLE

Fixed submission:

#include "bits/stdc++.h"
// http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=2332&lang=jp
typedef long long ll;
#define INF 1 << 30
using namespace std;

void dfs(int n, int v, priority_queue<pair<int, int>> &pq, vector<int> &c,
         vector<int> &masu) {
  if (c[n] <= v)
    return;
  c[n] = v;
  if (masu[n] == 0) {
    pq.push({v, n});
  } else {
    dfs(n + masu[n], v, pq, c, masu);
  }
}

void solve(int N, vector<int> &c, vector<int> &masu) {
  priority_queue<pair<int, int>> pq;
  pq.push({0, 1});
  c[1] = 0;

  while (!pq.empty()) {
    auto p = pq.top();
    pq.pop();
    int C = p.first;
    int i = p.second;
    for (int j = 1; j <= 6; j++) {
      if (masu[i] != 0)
        break;
      if (masu[min(i + j, N)] == 0) {
        if (c[min(i + j, N)] > c[i] + 1) {
          c[min(i + j, N)] = c[i] + 1;
          pq.push({c[min(i + j, N)], i + j});
        }
      } else {
        dfs(i + j, c[i] + 1, pq, c, masu);
      }
    }
  }
}

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  int N;
  cin >> N;
  vector<int> masu(N + 1, INF);
  vector<int> c(N + 1, INF);
  for (int i = 1; i <= N; i++)
    cin >> masu[i];
  solve(N, c, masu);
  cout << c[N] << endl;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n;
  std::cin >> n;
  vector<int> p(n), cell(n, 1e9);
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
  }
  queue<pii> que;
  cell[0] = 0;
  que.push(pii(0, 0));
  while (not que.empty()) {
    int s = que.front().first;
    int cost = que.front().second;
    que.pop();
    for (int i = 1; i <= 6; i++) {
      int pos = s + i;
      bool flag = false;
      for (int j = 0; j < 2 * n; j++) {
        pos = min(n - 1, pos);
        if (p[pos] != 0 and cell[pos] > cost) {
          cell[pos] = cost;
          pos += p[pos];
        } else {
          break;
        }
        if (j == 2 * n - 1)
          flag = true;
      }
      pos = min(n - 1, pos);
      if (flag == true)
        continue;
      if (cell[pos] > cost + 1) {
        cell[pos] = cost + 1;
        que.push(pii(pos, cost + 1));
      }
    }
  }

  std::cout << cell[n - 1] << std::endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n;
  std::cin >> n;
  vector<int> p(n), cell(n, 1e9);
  for (int i = 0; i < n; i++) {
    std::cin >> p[i];
  }
  queue<pii> que;
  cell[0] = 0;
  que.push(pii(0, 0));
  while (not que.empty()) {
    int s = que.front().first;
    int cost = que.front().second;
    que.pop();
    for (int i = 1; i <= 6; i++) {
      int pos = s + i;
      bool flag = false;
      for (int j = 0; j < 2 * n; j++) {
        pos = min(n - 1, pos);
        if (p[pos] != 0) {
          cell[pos] = cost;
          pos += p[pos];
        } else {
          break;
        }
        if (j == 2 * n - 1)
          flag = true;
      }
      pos = min(n - 1, pos);
      if (flag == true)
        continue;
      if (cell[pos] > cost + 1) {
        cell[pos] = cost + 1;
        que.push(pii(pos, cost + 1));
      }
    }
  }

  std::cout << cell[n - 1] << std::endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <functional>
#include <queue>
#include <utility>
#include <vector>

constexpr int INF = 1 << 28;

struct Edge {
  size_t src, dst;
  int cost;
  Edge() {}
  Edge(size_t src, size_t dst, int cost) : src(src), dst(dst), cost(cost) {}
};

template <class T>
using iqueue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

std::vector<int> shortest(const std::vector<std::vector<Edge>> &g, size_t s) {
  std::vector<int> d(g.size(), INF);
  d[s] = 0;
  iqueue<std::pair<int, size_t>> q;
  q.emplace(0, s);
  while (!q.empty()) {
    int w = q.top().first;
    size_t v = q.top().second;
    q.pop();
    if (w > d[v])
      continue;

    for (const Edge &e : g[v]) {
      if (d[e.dst] > d[e.src] + e.cost) {
        // fprintf(stderr, "src: d[%zu] = %d\\n", e.src, d[e.src]);
        // fprintf(stderr, "dst: d[%zu] = %d\\n", e.dst, d[e.dst]);
        d[e.dst] = d[e.src] + e.cost;
        q.emplace(d[e.dst], e.dst);
      }
    }
  }

  return d;
}

int main() {
  int N;
  scanf("%d", &N);

  std::vector<std::vector<Edge>> g(N);
  for (int i = 0; i < N; ++i) {
    int p;
    scanf("%d", &p);
    if (p) {
      // fprintf(stderr, "%d -> %d\\n", i, i+p);
      g[i].emplace_back(i, i + p, 0);
    } else {
      for (int j = 1; j <= 6; ++j) {
        if (i + j > N - 1)
          continue;

        // fprintf(stderr, "%d -> %d\\n", i, i+j);
        g[i].emplace_back(i, i + j, 1);
      }
    }
  }

  std::vector<int> d = shortest(g, 0);
  // for (int i=0; i<N; ++i)
  //   fprintf(stderr, "%d%c", d[i], i+1<N? ' ':'\\n');

  printf("%d\\n", d.back());
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>

constexpr int INF = 1 << 28;

std::vector<int> normalize(const std::vector<int> &p) {
  std::vector<int> q(p.size());
  q.back() = p.size() - 1;

  for (size_t i = 1; i + 1 < p.size(); ++i) {
    q[i] = i;
    if (p[i] == 0) {
      continue;
    }

    size_t j = i;
    bool yabai = false;
    while (p[j]) {
      j += p[j];
      if (j == i) {
        q[i] = -INF;
        yabai = true;
        break;
      } else if (j < i) {
        q[i] = q[j];
        yabai = true;
        break;
      }
    }
    if (!yabai) {
      q[i] = j;
    }
  }

  return q;
}

int main() {
  int N;
  scanf("%d", &N);

  std::vector<int> p(N);
  for (int i = 0; i < N; ++i) {
    scanf("%d", &p[i]);
  }

  std::vector<int> q = normalize(p);

  std::vector<int> dp(N, INF);
  dp[0] = 0;
  for (int i = 0; i < N; ++i) {
    for (int j = 1; j <= 6; ++j) {
      if (i + j >= N - 1) {
        dp[N - 1] = std::min(dp[N - 1], dp[i] + 1);
      } else if (q[i + j] == -INF) {
        continue;
      } else {
        dp[q[i + j]] = std::min(dp[q[i + j]], dp[i] + 1);
      }
    }
  }

  printf("%d\\n", dp[N - 1]);
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

typedef pair<int, int> P;

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  rep(i, n) cin >> p[i];

  vector<int> dist(n, 1e7);
  dist[0] = 0;

  priority_queue<P, vector<P>, greater<P>> q;
  q.push(MP(0, 0));

  while (!q.empty()) {
    int d, pos;
    tie(d, pos) = q.top();
    q.pop();

    if (dist[pos] < d)
      continue;
    if (pos == n - 1)
      break;

    if (p[pos] == 0) {
      FOR(i, 1, 7) {
        int npos = min(pos + i, n - 1);
        if (dist[npos] <= d + 1)
          continue;
        dist[npos] = d + 1;
        q.push(MP(d + 1, npos));
      }
    } else {
      if (dist[pos + p[pos]] <= d)
        continue;
      dist[pos + p[pos]] = d;
      q.push(MP(d, pos + p[pos]));
    }
  }

  cout << dist[n - 1] << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <iostream>
#include <queue>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define rev(v) (v).rbegin(), (v).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef vector<int> vi;
typedef pair<int, int> P;
typedef long long ll;

int main() {
  // freopen("test.txt", "r", stdin);
  int N;
  while (cin >> N) {
    vector<int> p(N);
    rep(i, N) cin >> p[i];
    vector<int> dest(N);
    rep(i, N) {
      vector<int> visit(N, 0);
      visit[i] = 1;
      dest[i] = i;
      int next = dest[i] + p[dest[i]];
      while (dest[i] != next) {
        if (visit[next] == 1) {
          dest[i] = -1;
          break;
        }
        visit[next] = 1;
        dest[i] = next;
        next = dest[i] + p[dest[i]];
      }
    }

    // rep(i, N) cout << (i?" ":"") << dest[i];

    priority_queue<P, vector<P>, greater<P>> q;
    vector<int> visit(N, 0);
    q.push(MP(0, 0));

    int ans = -1;
    while (!q.empty()) {
      P p = q.top();
      q.pop();

      if (visit[p.X])
        continue;
      visit[p.X] = true;
      if (p.X >= N - 1) {
        ans = p.second;
        break;
      }

      for (int i = 1; i <= 6; i++) {
        int x = p.X + i;
        if (x >= N)
          x = N - 1;
        int next = dest[x];
        if (next == -1)
          continue;
        q.push(MP(next, p.second + 1));
      }
    }
    cout << ans << endl;
  }

  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include "bits/stdc++.h"

using namespace std;

const int INF = numeric_limits<int>::max() / 2;

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

  int n;
  cin >> n;
  vector<int> p(2 * n), dp(2 * n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 1; i <= 6; i++) {
      int next = now + i;
      if (next > n - 1)
        break;
      while (dp[next] == INF) {
        if (p[next] == 0)
          q.push(next);
        dp[next] = dp[now] + 1;
        next += p[next];
      }
    }
    if (dp[n - 1] != INF)
      break;
  }
  cout << dp[n - 1] << endl;
}

Error: 0

Fixed submission:

#include "bits/stdc++.h"

using namespace std;

const int INF = numeric_limits<int>::max() / 2;

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

  int n;
  cin >> n;
  vector<int> p(n), dp(n, INF);
  dp[0] = 0;
  for (int i = 0; i < n; i++)
    cin >> p[i];
  queue<int> q;
  q.push(0);
  while (!q.empty()) {
    int now = q.front();
    q.pop();
    for (int i = 1; i <= 6; i++) {
      int next = now + i;
      if (next > n - 1)
        break;
      while (dp[next] == INF) {
        dp[next] = dp[now] + 1;
        next += p[next];
      }
      if (p[next] == 0)
        q.push(next);
    }
    if (dp[n - 1] != INF)
      break;
  }
  cout << dp[n - 1] << endl;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
#include <vector>
#pragma warning(disable : 4996)
// FILE *in = freopen("input.txt", "r", stdin);
using namespace std;
int n, p[100007], vis[100007], can[100007], d[100007];
vector<int> v;
void rec(int pos, int cur) {
  vis[pos] = cur;
  while (true) {
    if (p[pos] == 0)
      break;
    v.push_back(pos);
    pos += p[pos];
    if (vis[pos] == cur || !can[pos]) {
      for (int i = 0; i < v.size(); i++)
        can[v[i]] = 0;
      break;
    }
    vis[pos] = cur;
  }
  v.clear();
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i], can[i] = 1;
  for (int i = 0; i < n; i++) {
    if (!vis[i] && p[i])
      rec(i, i + 1);
  }
  fill(d + 1, d + n, -1);
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 1; i <= 6 && u + i < n; i++) {
      int cur = u + i;
      if (can[cur] && d[cur] == -1) {
        while (p[cur])
          cur += p[cur];
        if (d[cur] == -1)
          d[cur] = d[u] + 1, que.push(cur);
      }
    }
  }
  cout << d[n - 1] << endl;
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <stack>
#include <vector>
#pragma warning(disable : 4996)
FILE *in = freopen("input.txt", "r", stdin);
using namespace std;
int n, p[100007], vis[100007], can[100007], d[100007];
vector<int> v;
void rec(int pos, int cur) {
  stack<pair<int, int>> s;
  s.push(make_pair(pos, cur));
  while (true) {
    pos = s.top().first, cur = s.top().second;
    if (vis[pos] == cur) {
      for (int i = 0; i < v.size(); i++)
        can[v[i]] = false;
      return;
    }
    vis[pos] = cur;
    v.push_back(pos);
    int nxt = pos + p[pos];
    if (p[nxt] == 0 || nxt == n - 1)
      return;
    s.push(make_pair(nxt, cur));
  }
}
int main() {
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> p[i], can[i] = 1;
  for (int i = 0; i < n; i++) {
    if (!vis[i] && p[i])
      rec(i, i + 1);
  }
  fill(d + 1, d + n, -1);
  queue<int> que;
  que.push(0);
  while (!que.empty()) {
    int u = que.front();
    que.pop();
    for (int i = 1; i <= 6 && u + i < n; i++) {
      int cur = u + i;
      if (can[cur] && d[cur] == -1) {
        while (p[cur])
          cur += p[cur];
        if (d[cur] == -1)
          d[cur] = d[u] + 1, que.push(cur);
      }
    }
  }
  cout << d[n - 1] << endl;
  return 0;
}
Problem p01449 (C++) — buggy submission, verdict: Runtime Error

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

void dijkstra_deque(int n, vector<edge> G[], int s, ll d[]) {
  vector<bool> vis(n, false);
  fill(d, d + n, LLONG_MAX);
  d[s] = 0;
  deque<ll> deq;
  deq.push_back(s);
  while (!deq.empty()) {
    int u = deq.front();
    deq.pop_front();
    if (vis[u])
      continue;
    vis[u] = true;
    for (int i = 0; i < G[u].size(); i++) {
      edge e = G[u][i];
      if (d[e.v] > d[u] + e.w) {
        d[e.v] = d[u] + e.w;
        if (e.w == 0)
          deq.push_front(e.v);
        else
          deq.push_back(e.v);
      }
    }
  }
}

vector<edge> G[100000];
ll d[100000];

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  for (int i = 0; i < N; i++) {
    if (p[i] != 0) {
      edge e = {i, i + p[i], 0};
      G[i].push_back(e);
      continue;
    }
    for (int j = 1; j <= 6 && i + j < N; j++) {
      edge e = {i, i + j, 1};
      G[i].push_back(e);
    }
  }
  dijkstra_deque(N, G, 0, d);
  cout << d[N - 1] << endl;
}

Error: -11

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int foo(int i, vector<int> &p, vector<int> &memo) {
  if (memo[i] >= 0)
    return memo[i];
  if (p[i] != 0) {

    return memo[i] = foo(i + p[i], p, memo);
  }
  memo[i] = INT_MAX / 2;
  int N = p.size();
  int mini = INT_MAX / 2;
  for (int j = 1; j <= 6 && i + j < N; j++)
    mini = min(mini, foo(i + j, p, memo) + 1);
  return memo[i] = mini;
}

int main() {
  int N;
  cin >> N;
  vector<int> p(N);
  for (int i = 0; i < N; i++)
    cin >> p[i];
  vector<int> memo(N, -1);
  memo[N - 1] = 0;
  cout << foo(0, p, memo) << endl;
}