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

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

int main() {
  i64 H, W, K;
  cin >> H >> W >> K;
  i64 sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<string> f(H + 2, string(W + 2, '@'));
  rep(i, 0, H) {
    string s;
    cin >> s;
    f[i + 1] = "@" + s + "@";
  }
  using P = pair<i64, i64>;
  struct info {
    P p;
    i64 d;
    bool operator<(const info &b) const { return p < b.p; }
  };
  using PP = pair<P, info>;
  priority_queue<PP, vector<PP>, greater<PP>> que;
  auto dist = ndvec(H + 2, W + 2, 5, P(1e18, 0));
  dist[sx][sy][4] = P(0, 0);
  que.push({P(0, 0), {{sx, sy}, 4}});

  vector<i64> dx{1, 0, -1, 0};

  while (!que.empty()) {
    P di = que.top().first;
    P p = que.top().second.p;
    i64 pd = que.top().second.d;
    que.pop();
    if (di > dist[p.first][p.second][pd])
      continue;
    if (pd == 4) {
      auto cost = P(di.first + 1, 0);
      rep(d, 0, 4) {
        auto next = P(p.first + dx[d], p.second + dx[d ^ 1]);
        if (f[next.first][next.second] == '.' &&
            dist[next.first][next.second][d] > cost) {
          dist[next.first][next.second][d] = cost;
          que.push({cost, {next, d}});
        }
      }
    } else {
      {
        auto cost = P(di.first, 0);
        if (dist[p.first][p.second][4] > cost) {
          dist[p.first][p.second][4] = cost;
          que.push({cost, {p, 4}});
        }
      }
      [&]() {
        auto cost = P(di.first, di.second + 1);
        if (cost.second >= K)
          return;
        auto next = P(p.first + dx[pd], p.second + dx[pd ^ 1]);
        if (f[next.first][next.second] == '@')
          return;
        if (dist[next.first][next.second][pd] > cost) {
          dist[next.first][next.second][pd] = cost;
          que.push({cost, {next, pd}});
        }
      }();
    }
  }
  i64 ans = dist[tx][ty][4].first;
  if (ans == i64(1e18))
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using i64 = long long;
#define rep(i, s, e) for (i64(i) = (s); (i) < (e); (i)++)
#define all(x) x.begin(), x.end()

template <class T> static inline std::vector<T> ndvec(size_t &&n, T val) {
  return std::vector<T>(n, std::forward<T>(val));
}
template <class... Tail> static inline auto ndvec(size_t &&n, Tail &&...tail) {
  return std::vector<decltype(ndvec(std::forward<Tail>(tail)...))>(
      n, ndvec(std::forward<Tail>(tail)...));
}
template <class Cond> struct chain {
  Cond cond;
  chain(Cond cond) : cond(cond) {}
  template <class T> bool operator()(T &a, const T &b) const {
    if (cond(a, b)) {
      a = b;
      return true;
    }
    return false;
  }
};
template <class Cond> chain<Cond> make_chain(Cond cond) {
  return chain<Cond>(cond);
}

int main() {
  i64 H, W, K;
  cin >> H >> W >> K;
  i64 sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  vector<string> f(H + 2, string(W + 2, '@'));
  rep(i, 0, H) {
    string s;
    cin >> s;
    f[i + 1] = "@" + s + "@";
  }

  auto dist = ndvec(H + 2, W + 2, (i64)(1e18));
  struct info {
    pair<i64, i64> p;
    i64 d;
  };
  using P = pair<i64, i64>;
  vector<i64> dx{1, 0, -1, 0};
  auto next_p = [&](P &p, i64 d) {
    p.first += dx[d];
    p.second += dx[d ^ 1];
  };
  auto acc = [&](auto arr, P p) { return arr[p.first][p.second]; };
  priority_queue<pair<i64, P>, std::vector<pair<i64, P>>, greater<pair<i64, P>>>
      que;
  que.push({0, {sx, sy}});
  dist[sx][sy] = 0;
  while (!que.empty()) {
    P p = que.top().second;
    i64 dst = que.top().first;
    que.pop();
    if (dst > acc(dist, p))
      continue;
    // cout << p.first << " " << p.second << endl;
    rep(d, 0, 4) {
      i64 cnt = 1;
      P now = p;
      next_p(now, d);
      while (acc(f, now) == '.') {
        i64 cost = acc(dist, p) + (cnt + K - 1) / K;
        if (acc(dist, now) > cost) {
          dist[now.first][now.second] = cost;
          que.push({cost, now});
        } else
          break;
        cnt++;
        next_p(now, d);
      }
      continue;
    }
  }
  if (dist[tx][ty] == (i64)(1e18)) {
    cout << "-1" << endl;
  } else {
    cout << dist[tx][ty] << endl;
  }
}