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

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
const int MAX = 200005;
const int INF = 1001001001;

struct Pt {
  int x, y, dir;
  P cost;
  bool operator<(const Pt &x) const { return this->cost > x.cost; }
};

int main(int, char **) {
  int H, W, K;
  cin >> H >> W >> K;
  vector<vector<vector<P>>> dp(H, vector<vector<P>>(W, vector<P>(4, {INF, 0})));

  int xs, ys, xe, ye;
  cin >> xs >> ys >> xe >> ye;
  xs--;
  ys--;
  xe--;
  ye--;

  char c;
  rep(i, H) rep(j, W) {
    cin >> c;
    if (c == '@')
      rep(k, 4) dp[i][j][k] = {-1, 0};
  }

  const int dx[4] = {-1, 0, 1, 0};
  const int dy[4] = {0, -1, 0, 1};

  priority_queue<Pt> q;
  rep(i, 4) {
    q.push({xs, ys, i, {0, 0}});
    dp[xs][ys][i] = {0, 0};
  }
  while (!q.empty()) {
    Pt pt = q.top();
    q.pop();
    int x = pt.x;
    int y = pt.y;
    int d = pt.dir;
    P c = pt.cost;
    if (dp[x][y][d] < c)
      continue;
    rep(i, 4) {
      int nx = x;
      int ny = y;
      P nc;
      if (i == d) {
        nx += dx[i];
        ny += dy[i];
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || dp[nx][ny][i].first == -1)
          continue;
      }
      if (i != d)
        nc = {c.first + (c.second > 0 ? 1 : 0), 0};
      else
        nc = {c.first + (c.second + 1) / K, (c.second + 1) % K};

      if (nc >= dp[nx][ny][i])
        continue;
      dp[nx][ny][i] = nc;
      q.push({nx, ny, i, nc});
    }
  }

  int ans = INF;
  rep(i, 4) {
    P p = dp[xe][ye][i];
    if (p.first == INF) {
      cout << -1 << endl;
      return 0;
    } else
      ans = min(ans, p.first + (p.second > 0 ? 1 : 0));
  }
  cout << ans << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
#define rep(j, n) for (int j = 0; j < (n); j++)

using ll = long long;
using P = pair<int, int>;

const int mod = 998244353;
const int MAX = 200005;
const int INF = 1001001001;

struct Pt {
  int x, y, cost;
  bool operator<(const Pt &x) const { return this->cost < x.cost; }
};

int main(int, char **) {
  int h, w, k;
  cin >> h >> w >> k;
  vector<vector<int>> dp(h, vector<int>(w, INF));

  int xs, ys, xe, ye;
  cin >> xs >> ys >> xe >> ye;
  xs--;
  ys--;
  xe--;
  ye--;

  char c;
  rep(i, h) rep(j, w) {
    cin >> c;
    if (c == '@')
      dp[i][j] = -1;
  }

  const int dx[4] = {-1, 1, 0, 0};
  const int dy[4] = {0, 0, -1, 1};

  priority_queue<Pt> q;
  q.push({xs, ys, 0});
  dp[xs][ys] = 0;
  while (!q.empty()) {
    Pt pt = q.top();
    q.pop();
    int x = pt.x;
    int y = pt.y;
    int c = pt.cost;
    if (dp[x][y] < c)
      continue;
    rep(i, 4) rep(j, k) {
      int nx = x + dx[i] * (j + 1);
      int ny = y + dy[i] * (j + 1);
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || dp[nx][ny] == -1)
        break;
      if (dp[x][y] >= dp[nx][ny])
        break;
      if (dp[x][y] + 1 >= dp[nx][ny])
        continue;
      dp[nx][ny] = dp[x][y] + 1;
      q.push({nx, ny, dp[nx][ny]});
    }
  }

  if (dp[xe][ye] == INF)
    cout << -1 << endl;
  else
    cout << dp[xe][ye] << endl;

  return 0;
}