Problem p01449 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

const int LOOP = 100000000;

typedef pair<int, int> PI;

void next(vector<int> &p, int &cur, int &cur_x2) {
  cur = cur + p[cur];
  cur_x2 = cur_x2 + p[cur_x2];
  cur_x2 = cur_x2 + p[cur_x2];
}

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  vector<int> p2(n);
  vector<bool> detected(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    if (detected[i] || p[i] == 0) {
      if (!detected[i]) {
        p2[i] = i;
        detected[i] = true;
      }
      continue;
    }
    int cur = i;
    int cur_x2 = i + p[i];
    bool has_loop = false;
    while (p[cur] != 0 && !detected[cur]) {
      if (cur == cur_x2) {
        has_loop = true;
        break;
      }
      next(p, cur, cur_x2);
    }
    int dest = detected[cur] ? p2[cur] : has_loop ? LOOP : cur;
    //   cout << dest << "deぬる" << endl;
    cur = i;
    cur_x2 = i + p[i];
    while (!detected[cur]) {
      int prev = cur;
      next(p, cur, cur_x2);
      detected[prev] = true;
      p2[prev] = dest;
    }
  }
  for (int i = 0; i < n; i++) {
    //    cout<<" " << p2[i] << " "<< endl;
  }
  p = p2;
  vector<int> dists(n, -1);
  queue<int> que;
  que.push(0);
  dists[0] = 0;
  while (!que.empty()) {
    int s = que.front();
    que.pop();
    int dist = dists[s];

    for (int i = 1; i <= 6; i++) {
      if (p[s + i] != LOOP && dists[p[s + i]] == -1) {
        dists[p[s + i]] = dist + 1;
        que.push(p[s + i]);
        if (p[s + i] + 1 >= n) {
          //         rep(j,n) cout << dists[j]<< " ";
          cout << dist + 1 << endl;
          return 0;
        }
      }
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
#define rep(i, n) for (int i = 0; i < (n); i++)

const int LOOP = 100000000;

typedef pair<int, int> PI;

void next(vector<int> &p, int &cur) {
  cur = cur + p[cur];
  // cur_x2 = cur_x2 + p[cur_x2];
  // cur_x2 = cur_x2 + p[cur_x2];
}

int main() {
  int n;
  cin >> n;
  vector<int> p(n);
  vector<bool> detected(n);
  for (int i = 0; i < n; i++) {
    cin >> p[i];
  }
  for (int i = 0; i < n; i++) {
    if (detected[i] || p[i] == 0) {
      if (!detected[i]) {
        p[i] = i;
        detected[i] = true;
      }
      continue;
    }
    int cur = i + p[i];
    // int cur_x2 = i + p[i];
    bool has_loop = false;
    while (p[cur] != 0 && !detected[cur]) {
      if (cur == i) {
        has_loop = true;
        break;
      }
      cur = cur + p[cur];
    }
    int dest = has_loop ? LOOP : cur;
    //    cout << cur << "deぬる" << endl;
    cur = i;
    while (!detected[cur]) {
      int prev = cur;
      cur = cur + p[cur];
      detected[prev] = true;
      p[prev] = dest;
    }
  }
  //  for (int i = 0; i < n; i++) {
  //    cout<<" " << p[i] << " "<< endl;
  //  }

  vector<int> dists(n, -1);
  queue<int> que;
  que.push(0);
  dists[0] = 0;
  while (!que.empty()) {
    int s = que.front();
    que.pop();
    int dist = dists[s];

    for (int i = 1; i <= 6; i++) {
      if (p[s + i] != LOOP && dists[p[s + i]] == -1) {
        dists[p[s + i]] = dist + 1;
        que.push(p[s + i]);
        if (p[s + i] + 1 >= n) {
          cout << dist + 1 << endl;
          return 0;
        }
      }
    }
  }

  return 0;
}