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

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair<ll, ll>
#define piiii pair<ll, pair<ll, pii>>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void solve() {
  ll h, w, k, x1, x2, y1, y2;
  char kl;

  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<ll>> a(h, vector<ll>(w, 0));
  rep(i, 0, h) rep(j, 0, w) {
    cin >> kl;
    if (kl == '.')
      a[i][j] = 1;
  }

  vector<pii> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  vector<vector<ll>> dp(h, vector<ll>(w, 1e18));
  priority_queue<piiii, vector<piiii>, greater<piiii>> pq;

  dp[x1][y1] = 0;
  pii pos = {x1, y1};
  if (pos.f > 0 && a[pos.f - 1][pos.s] == 1)
    pq.push({1, {2, {pos.f - 1, pos.s}}});
  if (pos.s > 0 && a[pos.f][pos.s - 1] == 1)
    pq.push({1, {3, {pos.f, pos.s - 1}}});
  if (pos.f + 1 < h && a[pos.f + 1][pos.s] == 1)
    pq.push({1, {0, {pos.f + 1, pos.s}}});
  if (pos.s + 1 < w && a[pos.f][pos.s + 1] == 1)
    pq.push({1, {1, {pos.f, pos.s + 1}}});
  vector<vector<vi>> vis(h, vector<vi>(w, vi(4, 0)));
  while (!pq.empty()) {
    piiii top = pq.top();
    pq.pop();
    pos = top.s.s;
    ll val = top.f, val1 = val;
    ll dire = top.s.f;
    ll yx = ceil((val * 1.0) / k);
    if (vis[pos.f][pos.s][dire])
      continue;
    vis[pos.f][pos.s][dire] = 1;
    if ((dp[pos.f][pos.s] + k - 1) / k <= val / k)
      continue;

    dp[pos.f][pos.s] = min(dp[pos.f][pos.s], val);

    if (pos.f > 0 && a[pos.f - 1][pos.s] == 1) {
      val1 = val;
      if (dire != 2)
        val1 = yx * k;
      if (dp[pos.f - 1][pos.s] + k >= val1 + 1)
        pq.push({val1 + 1, {2, {pos.f - 1, pos.s}}});
    }
    if (pos.s > 0 && a[pos.f][pos.s - 1] == 1) {
      val1 = val;
      if (dire != 3)
        val1 = yx * k;
      if (dp[pos.f][pos.s - 1] + k >= val1 + 1)
        pq.push({val1 + 1, {3, {pos.f, pos.s - 1}}});
    }
    if (pos.f + 1 < h && a[pos.f + 1][pos.s] == 1) {
      val1 = val;
      if (dire != 0)
        val1 = yx * k;
      if (dp[pos.f + 1][pos.s] + k >= val1 + 1)
        pq.push({val1 + 1, {0, {pos.f + 1, pos.s}}});
    }
    if (pos.s + 1 < w && a[pos.f][pos.s + 1] == 1) {
      val1 = val;
      if (dire != 1)
        val1 = yx * k;
      if (dp[pos.f][pos.s + 1] + k >= val1 + 1)
        pq.push({val1 + 1, {1, {pos.f, pos.s + 1}}});
    }
  }
  // cout<<dp[x2][y2]<<endl;
  if (dp[x2][y2] == 1e18)
    cout << -1 << endl;
  else
    cout << (ll)(ceil(1.0 * dp[x2][y2] / k)) << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, a, b) for (int i = a; i < b; i++)
#define rrep(i, b, a) for (int i = b; i >= a; i--)
#define fori(a) for (auto i : a)
#define all(a) begin(a), end(a)
#define set(a, b) memset(a, b, sizeof(a))
#define sz(a) a.size()
#define pi 3.14159
#define ll long long
#define ull unsigned long long
#define pb push_back
#define PF push_front // deque
#define mp make_pair
#define pq priority_queue
#define mod 1000000007
#define f first
#define s second
#define pii pair<ll, ll>
#define piiii pair<ll, pair<ll, pii>>
#define vi vector<int>
#define vpii vector<pii>
#define debug(v)                                                               \\
  for (auto i : v)                                                             \\
    cout << i << " ";
#define tc                                                                     \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)

using namespace std;
void optimizeIO() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);
  cout.tie(NULL);
}
void solve() {
  ll h, w, k, x1, x2, y1, y2;
  char kl;

  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;

  vector<vector<ll>> a(h, vector<ll>(w, 0));
  rep(i, 0, h) rep(j, 0, w) {
    cin >> kl;
    if (kl == '.')
      a[i][j] = 1;
  }

  vector<pii> direction = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  vector<vector<ll>> dp(h, vector<ll>(w, 1e18));
  priority_queue<piiii, vector<piiii>, greater<piiii>> pq;

  dp[x1][y1] = 0;
  pii pos = {x1, y1};
  if (pos.f > 0 && a[pos.f - 1][pos.s] == 1)
    pq.push({1, {2, {pos.f - 1, pos.s}}});
  if (pos.s > 0 && a[pos.f][pos.s - 1] == 1)
    pq.push({1, {3, {pos.f, pos.s - 1}}});
  if (pos.f + 1 < h && a[pos.f + 1][pos.s] == 1)
    pq.push({1, {0, {pos.f + 1, pos.s}}});
  if (pos.s + 1 < w && a[pos.f][pos.s + 1] == 1)
    pq.push({1, {1, {pos.f, pos.s + 1}}});

  while (!pq.empty()) {
    piiii top = pq.top();
    pq.pop();
    pos = top.s.s;
    ll val = top.f, val1 = val;
    ll dire = top.s.f;
    ll yx = ceil((val * 1.0) / k);
    if ((dp[pos.f][pos.s] + k - 1) / k <= val / k)
      continue;

    dp[pos.f][pos.s] = min(dp[pos.f][pos.s], val);

    if (pos.f > 0 && a[pos.f - 1][pos.s] == 1) {
      val1 = val;
      if (dire != 2)
        val1 = yx * k;
      if (dp[pos.f - 1][pos.s] + k >= val1 + 1)
        pq.push({val1 + 1, {2, {pos.f - 1, pos.s}}});
    }
    if (pos.s > 0 && a[pos.f][pos.s - 1] == 1) {
      val1 = val;
      if (dire != 3)
        val1 = yx * k;
      if (dp[pos.f][pos.s - 1] + k >= val1 + 1)
        pq.push({val1 + 1, {3, {pos.f, pos.s - 1}}});
    }
    if (pos.f + 1 < h && a[pos.f + 1][pos.s] == 1) {
      val1 = val;
      if (dire != 0)
        val1 = yx * k;
      if (dp[pos.f + 1][pos.s] + k >= val1 + 1)
        pq.push({val1 + 1, {0, {pos.f + 1, pos.s}}});
    }
    if (pos.s + 1 < w && a[pos.f][pos.s + 1] == 1) {
      val1 = val;
      if (dire != 1)
        val1 = yx * k;
      if (dp[pos.f][pos.s + 1] + k >= val1 + 1)
        pq.push({val1 + 1, {1, {pos.f, pos.s + 1}}});
    }
  }
  // cout<<dp[x2][y2]<<endl;
  if (dp[x2][y2] == 1e18)
    cout << -1 << endl;
  else
    cout << (ll)(ceil(1.0 * dp[x2][y2] / k)) << endl;
}
int main() {
  optimizeIO();
  // tc
  { solve(); }
}