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

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define COST first
#define POS second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main(void) {
  int N;
  cin >> N;
  VI ps(N);
  for (auto &p : ps)
    cin >> p;

  vector<VPII> es(N);
  for (int i = 0; i < N; i++) {
    if (ps[i] == 0) {
      for (int j = 1; j <= 6; j++) {
        if (i + j >= N) {
          break;
        }

        es[i].PB(MP(1, i + j));
      }
    } else {
      es[i].PB(MP(0, i + ps[i])); // auto move
    }
  }

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

  VI dist(N, INF);
  dist[0] = 0;

  while (q.size()) {
    PII cur = q.top();
    q.pop();

    //        cout << cur.POS << " " << cur.COST << endl;

    if (cur.COST > dist[cur.POS]) {
      continue;
    }

    for (auto e : es[cur.POS]) {
      int next_cost = cur.COST + e.COST;
      int next_pos = e.POS;

      //            cout << "--- " << next_cost << " " << next_pos << endl;

      if (next_cost < dist[next_pos]) {
        q.push(MP(next_cost, next_pos));
        dist[next_pos] = next_cost;
      }
    }
  }

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

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define fst first
#define snd second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main(void) {
  int N;
  cin >> N;
  VI ts(N);
  for (auto &t : ts)
    cin >> t;

  VI dp(N, INF);
  dp[0] = 0;
  for (int i = 0; i < N; i++)
    if (dp[i] != INF && ts[i] == 0) {
      for (int j = 1; j <= 6; j++) {
        int next = min(i + j, N - 1);
        int times = dp[i] + 1;

        set<int> done;
        while (!done.count(next) && times < dp[next]) {
          done.insert(next);

          if (ts[next] == 0) {
            dp[next] = times;
          }

          next = min(next + ts[next], N - 1);
        }
      }
    }

  //    for (auto a : dp) cout << a << " ";

  cout << dp[N - 1] << endl;

  return 0;
}