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

#include <algorithm>
#include <cstring>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

int field[100000] = {0};
int field2[100000] = {0};
bool roop[100000] = {false};
// bool safe[100000] = { 0 };
int min[100000] = {0};

int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> field[i];
  }

  std::memset(field2, -1, sizeof(int) * 100000);

  //???????????´??????????´¢
  for (int i = 1; i < n; i++) { //????§???????
    std::vector<int> passed;
    bool isOk = false;
    int pos = i;
    passed.push_back(pos);
    while (!isOk) {
      int d = field[pos];
      pos += d;
      if (field[pos]) { //?¬????n???????§????
        if (std::find(passed.begin(), passed.end(), pos) != passed.end()) {
          break; //?????????????????¨????????????????????????????????????????????????
        }
        if (roop[pos])
          break; //?????????????????´?????¨???????¨??????????
        if (field2[pos] >= 0) {
          isOk = true;
          pos = field2[pos];
          break; // OK??´?????¨???????¨??????????
        }
      } else {
        isOk = true; // 0????????????????????£??????
        break;
      }
      passed.push_back(pos);
    }
    passed.push_back(pos);
    if (isOk) {
      for (auto &x : passed)
        field2[x] = pos;
    } else
      for (auto &x : passed)
        roop[x] = true;
  }
  std::queue<std::pair<int, int>> q;
  q.push(std::make_pair(0, 0));
  while (!q.empty()) {
    auto node = q.front();
    q.pop();

    for (int i = 1; i <= 6; i++) {
      auto next = node;
      next.second += i;
      if (next.second >= n - 1) { //??´????????????
        std::cout << next.first + 1 << std::endl;
        return 0;
      }
      if (roop[next.second] || field2[next.second] == -1)
        continue;
      next.second = field2[next.second]; //?£???¶
      next.first++;
      if (next.second >= n - 1) { //??´????????????
        std::cout << next.first << std::endl;
        return 0;
      }
      if (min[next.second] && min[next.second] <= next.first)
        continue;
      min[next.second] = next.first;
      q.push(next);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <functional>
#include <iostream>
#include <queue>
#include <vector>

int field[100000] = {0};
int field2[100000] = {0};
bool roop[100000] = {false};
// bool safe[100000] = { 0 };
int min[100000] = {0};

int main() {
  int n;
  std::cin >> n;
  for (int i = 0; i < n; i++) {
    std::cin >> field[i];
  }

  //???????????´??????????´¢
  for (int i = 1; i < n; i++) { //????§???????
    std::vector<int> passed;
    bool isOk = false;
    int pos = i;
    passed.push_back(pos);
    while (!isOk) {
      int d = field[pos];
      pos += d;
      if (field[pos]) { //?¬????n???????§????
        if (std::find(passed.begin(), passed.end(), pos) != passed.end()) {
          break; //?????????????????¨????????????????????????????????????????????????
        }
        if (roop[pos])
          break; //?????????????????´?????¨???????¨??????????
        if (field2[pos]) {
          isOk = true;
          pos = field2[pos];
          break; // OK??´?????¨???????¨??????????
        }
      } else {
        isOk = true; // 0????????????????????£??????
        break;
      }
      passed.push_back(pos);
    }
    passed.push_back(pos);
    if (isOk) {
      for (auto &x : passed)
        field2[x] = pos;
    } else
      for (auto &x : passed)
        roop[x] = true;
  }
  std::queue<std::pair<int, int>> q;
  q.push(std::make_pair(0, 0));
  while (!q.empty()) {
    auto node = q.front();
    q.pop();

    for (int i = 1; i <= 6; i++) {
      auto next = node;
      next.second += i;
      if (next.second >= n - 1) { //??´????????????
        std::cout << next.first + 1 << std::endl;
        return 0;
      }
      if (roop[next.second])
        continue;
      next.second = field2[next.second]; //?£???¶
      next.first++;
      if (next.second >= n - 1) { //??´????????????
        std::cout << next.first << std::endl;
        return 0;
      }
      if (min[next.second] && min[next.second] <= next.first)
        continue;
      min[next.second] = next.first;
      q.push(next);
    }
  }

  return 0;
}