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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

const ll INF = 1e18;
vector<int> nex = {1, -1, 0, 0}, ney = {0, 0, 1, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  --xs;
  --ys;
  --xg;
  --yg;
  vector<vector<int>> rake(H, vector<int>(W));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<ll> d(H * W * 4, INF);
  auto toid = [&](int x, int y, int muki) { return (x * W + y) * 4 + muki; };
  auto upK = [&](ll x) { return (x + K - 1) / K * K; };
  priority_queue<LP, vector<LP>, greater<LP>> que;
  que.push({0, toid(xs, ys, 0)});
  d[toid(xs, ys, 0)] = 0;
  while (!que.empty()) {
    LP now = que.top();
    que.pop();
    if (d[now.second] != now.first)
      continue;
    int x = now.second / 4 / W, y = now.second / 4 % W, muki = now.second % 4;

    int newx = x + nex.at(muki), newy = y + ney.at(muki);
    if (0 <= newx && newx < H && 0 <= newy && newy < W) {
      if (!rake.at(newx).at(newy)) {
        if (d[toid(newx, newy, muki)] > now.first + 1) {
          d[toid(newx, newy, muki)] = now.first + 1;
          que.push({d[toid(newx, newy, muki)], toid(newx, newy, muki)});
        }
      }
    }

    rep(newmuki, 4) {
      if (d[toid(x, y, newmuki)] > upK(now.first)) {
        d[toid(x, y, newmuki)] = upK(now.first);
        que.push({d[toid(x, y, newmuki)], toid(x, y, newmuki)});
      }
    }
  }
  ll ans = d[toid(xg, yg, 3)];
  rep(i, 3) { chmin(ans, d[toid(xg, yg, i)]); }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << upK(ans) / K << endl;
  }

  /*rep(i,H){
      rep(j,W){
          cout<<"("<<d[toid(i,j,0)]<<" "<<d[toid(i,j,1)]<<" "<<d[toid(i,j,2)]<<"
  "<<d[toid(i,j,3)]<<")";
      }
      cout<<endl;
  }*/
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<int, ll>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

const ll INF = 1e18;
vector<int> nex = {1, -1, 0, 0}, ney = {0, 0, 1, -1};

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int xs, ys, xg, yg;
  cin >> xs >> ys >> xg >> yg;
  --xs;
  --ys;
  --xg;
  --yg;
  vector<vector<int>> rake(H, vector<int>(W));
  rep(i, H) rep(j, W) {
    char c;
    cin >> c;
    if (c == '@') {
      rake.at(i).at(j) = 1;
    }
  }
  vector<ll> d(H * W * 4, INF);
  auto toid = [&](int x, int y, int muki) { return (x * W + y) * 4 + muki; };
  auto upK = [&](ll x) { return (x + K - 1) / K * K; };
  priority_queue<LP, vector<LP>, greater<LP>> que;
  que.push({toid(xs, ys, 0), 0});
  d[toid(xs, ys, 0)] = 0;
  while (!que.empty()) {
    LP now = que.top();
    que.pop();
    if (d[now.first] != now.second)
      continue;
    int x = now.first / 4 / W, y = now.first / 4 % W, muki = now.first % 4;

    int newx = x + nex.at(muki), newy = y + ney.at(muki);
    if (0 <= newx && newx < H && 0 <= newy && newy < W) {
      if (!rake.at(newx).at(newy)) {
        if (d[toid(newx, newy, muki)] > now.second + 1) {
          d[toid(newx, newy, muki)] = now.second + 1;
          que.push({toid(newx, newy, muki), d[toid(newx, newy, muki)]});
        }
      }
    }

    rep(newmuki, 4) {
      if (d[toid(x, y, newmuki)] > upK(now.second)) {
        d[toid(x, y, newmuki)] = upK(now.second);
        que.push({toid(x, y, newmuki), d[toid(x, y, newmuki)]});
      }
    }
  }
  ll ans = d[toid(xg, yg, 3)];
  rep(i, 3) { chmin(ans, d[toid(xg, yg, i)]); }
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << upK(ans) / K << endl;
  }

  /*rep(i,H){
      rep(j,W){
          cout<<"("<<d[toid(i,j,0)]<<" "<<d[toid(i,j,1)]<<" "<<d[toid(i,j,2)]<<"
  "<<d[toid(i,j,3)]<<")";
      }
      cout<<endl;
  }*/
}