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

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;
const int mod = 1e9 + 7;

#define int ll
#define INF LINF

/*
typedef struct Vertex{
    int x;
    int y;
    int dir; // 0:up 1:right 2:down 3:left
} vertex;
 */

using vertex = pair<pair<int, int>, int>;

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

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;

  vector<string> c(h);
  for (int i = 0; i < h; ++i) {
    cin >> c[i];
  }

  vector<vector<vector<int>>> dis(h,
                                  vector<vector<int>>(w, vector<int>(4, INF)));
  priority_queue<pair<int, vertex>, vector<pair<int, vertex>>,
                 greater<pair<int, vertex>>>
      que;

  for (int i = 0; i < 4; ++i) {
    dis[sx][sy][i] = 0;
    que.push(pair<int, vertex>(0, pair<pll, ll>(pll(sx, sy), i)));
  }

  while (!que.empty()) {
    pair<int, vertex> p = que.top();
    que.pop();
    vertex nv = p.second;
    int x = nv.first.first;
    int y = nv.first.second;
    int dir = nv.second;
    if (dis[x][y][dir] != p.first)
      continue;

    int nd = (((dis[x][y][dir] + k - 1) / k) * k) + 1;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || c[nx][ny] == '@')
        continue;

      if (i == dir) {
        if (dis[nx][ny][i] > dis[x][y][i] + 1) {
          dis[nx][ny][i] = dis[x][y][i] + 1;
          que.push(
              pair<int, vertex>(dis[nx][ny][i], pair<pll, ll>(pll(nx, ny), i)));
        }
      } else {
        if (dis[nx][ny][i] > nd) {
          dis[nx][ny][i] = nd;
          que.push(pair<int, vertex>(nd, pair<pll, ll>(pll(nx, ny), i)));
        }
      }
    }
  }

  int res = INF;
  for (int i = 0; i < 4; ++i) {
    res = min(res, dis[gx][gy][i]);
  }

  if (res == INF)
    cout << "-1" << endl;
  else
    cout << (res + k - 1) / k << endl;
}

Error: TLE

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int INF = (1 << 30) - 1;
const ll LINF = (1LL << 60) - 1;
const int mod = 1e9 + 7;

#define int ll
#define INF LINF

typedef struct Vertex {
  int x;
  int y;
  int dir; // 0:up 1:right 2:down 3:left
} vertex;

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

auto compare = [](pair<int, vertex> a, pair<int, vertex> b) {
  return a.first < b.first;
};

signed main() {
  int h, w, k;
  cin >> h >> w >> k;
  int sx, sy, gx, gy;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;

  vector<string> c(h);
  for (int i = 0; i < h; ++i) {
    cin >> c[i];
  }

  vector<vector<vector<int>>> dis(h,
                                  vector<vector<int>>(w, vector<int>(4, INF)));
  priority_queue<pair<int, vertex>, vector<pair<int, vertex>>,
                 decltype(compare)>
      que{compare};

  for (int i = 0; i < 4; ++i) {
    dis[sx][sy][i] = 0;
    que.push(pair<int, vertex>(0, vertex{sx, sy, i}));
  }

  while (!que.empty()) {
    pair<int, vertex> p = que.top();
    que.pop();
    vertex nv = p.second;
    if (dis[nv.x][nv.y][nv.dir] != p.first)
      continue;

    int nd = (((dis[nv.x][nv.y][nv.dir] + k - 1) / k) * k) + 1;
    for (int i = 0; i < 4; ++i) {
      int nx = nv.x + dx[i];
      int ny = nv.y + dy[i];
      if (nx < 0 || nx >= h || ny < 0 || ny >= w || c[nx][ny] == '@')
        continue;

      if (i == nv.dir) {
        if (dis[nx][ny][i] > dis[nv.x][nv.y][i] + 1) {
          dis[nx][ny][i] = dis[nv.x][nv.y][i] + 1;
          que.emplace(dis[nx][ny][i], vertex{nx, ny, i});
        }
      } else {
        if (dis[nx][ny][i] > nd) {
          dis[nx][ny][i] = nd;
          que.emplace(nd, vertex{nx, ny, i});
        }
      }
    }
  }

  int res = INF;
  for (int i = 0; i < 4; ++i) {
    res = min(res, dis[gx][gy][i]);
  }

  if (res == INF)
    cout << "-1" << endl;
  else
    cout << (res + k - 1) / k << endl;
}