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

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

#define next nxt

int n;
int p[111111];
bool vis[111111];
int next[111111];
int d[111111];

int dfs(int v) {
  if (p[v] == 0)
    return v;
  if (next[v] != -INF)
    return next[v];
  vis[v] = true;
  if (!vis[v + p[v]])
    return next[v] = dfs(v + p[v]);
  else
    return next[v] = -1;
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> p[i];
  rep(i, n) next[i] = -INF;
  rep(i, n) {
    memset(vis, 0, sizeof(vis));
    next[i] = dfs(i);
  }
  fill(d, d + n, INF);
  priority_queue<P, vector<P>, greater<P>> que;
  que.push(P(0, 0));
  d[0] = 0;
  while (que.size()) {
    P np = que.top();
    que.pop();
    int v = np.se, dist = np.fi;
    if (v == n - 1)
      break;
    repl(j, 1, 7) {
      if (v + j >= n)
        continue;
      int nv = next[v + j];
      if (d[nv] > d[v] + 1) {
        d[nv] = d[v] + 1;
        que.push(P(d[nv], nv));
      }
    }
  }
  cout << d[n - 1] << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define fi first
#define se second
#define repl(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) repl(i, 0, n)
#define each(itr, v) for (auto itr : v)
#define pb(s) push_back(s)
#define mp(a, b) make_pair(a, b)
#define all(x) (x).begin(), (x).end()
#define dbg(x) cout << #x "=" << x << endl
#define maxch(x, y) x = max(x, y)
#define minch(x, y) x = min(x, y)
#define uni(x) x.erase(unique(all(x)), x.end())
#define exist(x, y) (find(all(x), y) != x.end())
#define bcnt(x) bitset<32>(x).count()

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef pair<P, int> PPI;
typedef pair<ll, ll> PL;
typedef pair<P, ll> PPL;

#define INF INT_MAX / 3

#define MAX_N 1000

#define next nxt

int n;
int p[111111];
bool vis[111111];
int next[111111];
int dp[111111];

int dfs(int v) {
  if (p[v] == 0)
    return v;
  vis[v] = true;
  if (next[v + p[v]] == -1)
    return -1;
  else {
    if (!vis[v + p[v]])
      return next[v] = dfs(v + p[v]);
    else
      return next[v] = -1;
  }
}

int main() {
  cin.sync_with_stdio(false);
  cin >> n;
  rep(i, n) cin >> p[i];
  rep(i, n) {
    memset(vis, 0, sizeof(vis));
    next[i] = dfs(i);
  }
  // rep(i,n)dbg(next[i]);
  rep(i, 111111) dp[i] = INF;
  dp[0] = 0;
  rep(i, n) {
    if (next[i] == -1)
      continue;
    minch(dp[next[i]], dp[i]);
    int v = next[i];
    repl(j, 1, 7) {
      if (v + j >= n)
        continue;
      minch(dp[v + j], dp[v] + 1);
    }
  }
  cout << dp[n - 1] << endl;
  return 0;
}