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

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<bool>> grid(h, vector<bool>(w, true));
  rep(y, h) {
    string s;
    cin >> s;
    rep(x, w) {
      if (s[x] == '@') {
        grid[y][x] = false;
      }
    }
  }
  vector<vector<vector<ll>>> costs(h,
                                   vector<vector<ll>>(w, vector<ll>(4, INF)));
  priority_queue<t4, vector<t4>, greater<t4>> qs;
  for (int i = 0; i < 4; i++) {
    qs.emplace(0, x1, y1, i);
    costs[y1][x1][i] = 0;
  }
  constexpr P shifts[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  while (!qs.empty()) {
    ll x, y, d, c;
    tie(c, x, y, d) = qs.top();
    qs.pop();
    for (int i = 0; i < 4; i++) {
      if (d == i)
        continue;
      auto ncost = c % k ? (c / k + 1) * k : c;
      if (costs[y][x][i] <= ncost)
        continue;
      costs[y][x][i] = ncost;
      qs.emplace(ncost, x, y, i);
    }
    {
      auto nx = x + shifts[d].first;
      auto ny = y + shifts[d].second;
      if (nx < 0 || nx >= w)
        continue;
      if (ny < 0 || ny >= h)
        continue;
      if (!grid[ny][nx])
        continue;
      ll ncost = c + 1;
      if (costs[ny][nx][d] <= ncost)
        continue;
      costs[ny][nx][d] = ncost;
      qs.emplace(ncost, nx, ny, d);
    }
  }
  ll low = INF;
  for (int i = 0; i < 4; i++) {
    auto c = costs[y2][x2][i];
    if (c != INF) {
      auto ncost = c % k ? (c / k + 1) : c / k;
      low = min(low, ncost);
    }
  }
  if (low == INF) {
    cout << -1 << endl;
  } else {
    cout << low << endl;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define M_PI 3.14159265358979323846 // pi

using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<ll> VI;
typedef pair<ll, ll> P;
typedef tuple<ll, ll, ll> t3;
typedef tuple<ll, ll, ll, ll> t4;
typedef tuple<ll, ll, ll, ll, ll> t5;

#define rep(a, n) for (ll a = 0; a < n; a++)
#define repi(a, b, n) for (ll a = b; a < n; a++)

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

static const ll INF = 1e15;

static const ll mod = 1e9 + 7;

int main() {
  ll h, w, k;
  cin >> h >> w >> k;
  ll x1, y1, x2, y2;
  cin >> y1 >> x1 >> y2 >> x2;
  x1--;
  y1--;
  x2--;
  y2--;
  vector<vector<bool>> grid(h, vector<bool>(w, true));
  rep(y, h) {
    string s;
    cin >> s;
    rep(x, w) {
      if (s[x] == '@') {
        grid[y][x] = false;
      }
    }
  }
  vector<vector<vector<ll>>> costs(h,
                                   vector<vector<ll>>(w, vector<ll>(4, INF)));
  priority_queue<t4, vector<t4>, less<t4>> qs;
  for (int i = 0; i < 4; i++) {
    qs.emplace(0, x1, y1, i);
    costs[y1][x1][i] = 0;
  }
  constexpr P shifts[4] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
  while (!qs.empty()) {
    ll x, y, d, c;
    tie(c, x, y, d) = qs.top();
    qs.pop();
    for (int i = 0; i < 4; i++) {
      if (d == i)
        continue;
      auto ncost = c % k ? (c / k + 1) * k : c;
      if (costs[y][x][i] <= ncost)
        continue;
      costs[y][x][i] = ncost;
      qs.emplace(ncost, x, y, i);
    }
    {
      auto nx = x + shifts[d].first;
      auto ny = y + shifts[d].second;
      if (nx < 0 || nx >= w)
        continue;
      if (ny < 0 || ny >= h)
        continue;
      if (!grid[ny][nx])
        continue;
      ll ncost = c + 1;
      if (costs[ny][nx][d] <= ncost)
        continue;
      costs[ny][nx][d] = ncost;
      qs.emplace(ncost, nx, ny, d);
    }
  }
  ll low = INF;
  for (int i = 0; i < 4; i++) {
    auto c = costs[y2][x2][i];
    if (c != INF) {
      auto ncost = c % k ? (c / k + 1) : c / k;
      low = min(low, ncost);
    }
  }
  if (low == INF) {
    cout << -1 << endl;
  } else {
    cout << low << endl;
  }
  return 0;
}