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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<ll>;
using P = pair<int, int>;
using LP = pair<ll, int>;
const ll INF = 1e18 + 7;

// 未解決
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int si, sj, ti, tj;
  cin >> si >> sj >> ti >> tj;
  si--;
  sj--;
  ti--;
  tj--;
  vector<string> c(h);
  rep(i, h) { cin >> c[i]; }

  auto toId = [&](int i, int j, int v) { return (i * w + j) * 4 + v; };
  auto modCeilK = [&](ll x) { return (x + k - 1) / k * k; };

  // start dijkstra
  int di[] = {1, 0, -1, 0};
  int dj[] = {0, 1, 0, -1};
  priority_queue<LP, vector<LP>, greater<LP>> q;
  vector<ll> dist(h * w * 4, INF);
  auto push = [&](int i, int j, int v, ll x) {
    int id = toId(i, j, v);
    if (dist[id] <= x)
      return;
    dist[id] = x;
    q.emplace(x, id);
  };
  rep(v, 4) push(si, sj, v, 0);

  while (!q.empty()) {
    ll d = q.top().first;
    int id = q.top().second;
    q.pop();

    if (dist[id] != d)
      continue;

    int v = id % 4;
    int j = id / 4 % w;
    int i = id / 4 / w;

    rep(nv, 4) {
      push(i, j, nv, modCeilK(d));
      int ni = i + di[v], nj = j + dj[v];
      if (0 <= ni && ni < h && 0 <= nj && nj < w && c[ni][nj] == '.') {
        push(ni, nj, v, d + 1);
      }
    }
  }
  // end dijkstra

  ll ans = INF;
  rep(v, 4) {
    int id = toId(ti, tj, v);
    ans = min(ans, dist[id]);
  }
  if (ans == INF)
    cout << -1 << endl;
  else
    cout << modCeilK(ans) / k << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define all(a) (a).begin(), (a).end()
using namespace std;
using ll = long long int;
using vec = vector<ll>;
using P = pair<int, int>;
using PP = pair<P, int>;
const ll INF = 1e18 + 7;

// 未解決
int main() {
  int h, w, k;
  cin >> h >> w >> k;
  int x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<string> c(h);
  rep(i, h) { cin >> c[i]; }

  // start dijkstra
  int dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
  priority_queue<PP> que;
  vector<vector<vec>> d(h, vector<vec>(w, vec(4, INF)));
  que.emplace(P(x1, y1), 0);
  d[x1][y1][0] = 0;

  while (!que.empty()) {
    PP pp = que.top();
    que.pop();
    int px = pp.first.first, py = pp.first.second, z = pp.second;
    // cout << px << " " << py << " " << z << " " << d[px][py][z] << endl;

    rep(i, 4) {
      ll nd;
      if (d[px][py][z] % k == 0)
        nd = d[px][py][z];
      else
        nd = (d[px][py][z] / k + 1) * k;
      if (z != i && nd < d[px][py][i]) {
        que.emplace(P(px, py), i);
        d[px][py][i] = nd;
      }
    }

    int nx = px + dx[z], ny = py + dy[z];
    if (0 <= nx && nx < h && 0 <= ny && ny < w && c[nx][ny] != '@' &&
        d[px][py][z] + 1 < d[nx][ny][z]) {
      que.emplace(P(nx, ny), z);
      d[nx][ny][z] = d[px][py][z] + 1;
    }
  }
  // end dijkstra

  ll dist = INF;
  rep(i, 4) { dist = min(dist, d[x2][y2][i]); }
  int ans;
  if (dist == INF)
    ans = -1;
  else if (dist % k == 0)
    ans = dist / k;
  else
    ans = dist / k + 1;

  cout << ans << endl;
}