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

#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define all(x) begin((x)), end((x))
#define vprint(x)                                                              \\
  for (auto a : (x))                                                           \\
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct v {
  ll k;
  ll rem;
  ll x;
  ll y;
  ll dir;
  v(ll k, ll rem, ll x, ll y, ll dir) : k(k), rem(rem), x(x), y(y), dir(dir) {}
  bool operator<(const v &right) const {
    return k == right.k ? rem > right.rem : k < right.k;
  }
  bool operator>(const v &right) const {
    return k == right.k ? rem < right.rem : k > right.k;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  ll x[2], y[2];
  cin >> x[0] >> y[0] >> x[1] >> y[1];
  x[0]--;
  y[0]--;
  x[1]--;
  y[1]--;
  vector<string> mat(h);
  rep(i, h) { cin >> mat[i]; }
  vector<vector<vector<v>>> cost(
      h, vector<vector<v>>(w, vector<v>(4, v(1e+15, 0, -1, -1, -1))));
  vector<vector<vector<bool>>> chk(
      h, vector<vector<bool>>(w, vector<bool>(4, false)));
  priority_queue<v, vector<v>, greater<v>> pq;
  rep(i, 4) { cost[x[0]][y[0]][i] = v(0, 0, x[0], y[0], i); }
  rep(i, 4) { pq.emplace(0, 0, x[0], y[0], i); }
  while (!pq.empty()) {
    v now = pq.top();

    ll nx = now.x, ny = now.y, dir = now.dir;
    ll nk = now.k, nrem = now.rem;
    pq.pop();
    if (chk[nx][ny][dir]) {
      continue;
    }
    // if (cost[nx][ny][dir] < now){
    //     continue;
    // }
    chk[nx][ny][dir] = true;
    v next = v(0, 0, 0, 0, 0);
    if (nx < h - 1 && mat[nx + 1][ny] == '.') {
      next = (dir == 0 && nrem > 0) ? v(nk, nrem - 1, nx + 1, ny, 0)
                                    : v(nk + 1, k - 1, nx + 1, ny, 0);
      if (chmin(cost[nx + 1][ny][0], next)) {
        pq.push(next);
      }
    }
    if (ny < w - 1 && mat[nx][ny + 1] == '.') {
      next = (dir == 1 && nrem > 0) ? v(nk, nrem - 1, nx, ny + 1, 1)
                                    : v(nk + 1, k - 1, nx, ny + 1, 1);
      if (chmin(cost[nx][ny + 1][1], next)) {
        pq.push(next);
      }
    }
    if (ny > 0 && mat[nx][ny - 1] == '.') {
      next = (dir == 2 && nrem > 0) ? v(nk, nrem - 1, nx, ny - 1, 2)
                                    : v(nk + 1, k - 1, nx, ny - 1, 2);
      if (chmin(cost[nx][ny - 1][2], next)) {
        pq.push(next);
      }
    }
    if (nx > 0 && mat[nx - 1][ny] == '.') {
      next = (dir == 3 && nrem > 0) ? v(nk, nrem - 1, nx - 1, ny, 3)
                                    : v(nk + 1, k - 1, nx - 1, ny, 3);
      if (chmin(cost[nx - 1][ny][3], next)) {
        pq.push(next);
      }
    }
  }
  ll res = ll(1e+15);
  rep(i, 4) { chmin(res, cost[x[1]][y[1]][i].k); }
  cout << (res == ll(1e+15) ? -1 : res) << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define repr(i, from, to) for (int(i) = (from); (i) < (to); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
// const bool debug=true;
const bool debug = false;
#define DEBUG if (debug == true)
#define all(x) begin((x)), end((x))
#define vprint(x)                                                              \\
  for (auto a : (x))                                                           \\
    cout << x << endl;
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll MOD = 1000000007;
const ll MOD2 = 998244353;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}

struct v {
  ll k;
  ll rem;
  ll x;
  ll y;
  ll dir;
  v(ll k, ll rem, ll x, ll y, ll dir) : k(k), rem(rem), x(x), y(y), dir(dir) {}
  bool operator<(const v &right) const {
    return k == right.k ? rem > right.rem : k < right.k;
  }
  bool operator>(const v &right) const {
    return k == right.k ? rem < right.rem : k > right.k;
  }
};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  ll h, w, k;
  cin >> h >> w >> k;
  ll x[2], y[2];
  cin >> x[0] >> y[0] >> x[1] >> y[1];
  x[0]--;
  y[0]--;
  x[1]--;
  y[1]--;
  vector<string> mat(h);
  rep(i, h) { cin >> mat[i]; }
  vector<vector<vector<v>>> cost(
      h, vector<vector<v>>(w, vector<v>(4, v(1e+15, 0, -1, -1, -1))));
  priority_queue<v> pq;
  rep(i, 4) { cost[x[0]][y[0]][i] = v(0, 0, x[0], y[0], i); }
  rep(i, 4) { pq.emplace(0, 0, x[0], y[0], i); }
  while (!pq.empty()) {
    v now = pq.top();

    ll nx = now.x, ny = now.y, dir = now.dir;
    ll nk = now.k, nrem = now.rem;
    pq.pop();
    if (cost[nx][ny][dir] < now) {
      continue;
    }
    v next = v(0, 0, 0, 0, 0);
    if (nx < h - 1 && mat[nx + 1][ny] == '.') {
      next = (dir == 0 && nrem > 0) ? v(nk, nrem - 1, nx + 1, ny, 0)
                                    : v(nk + 1, k - 1, nx + 1, ny, 0);
      if (chmin(cost[nx + 1][ny][0], next)) {
        pq.push(next);
      }
    }
    if (ny < w - 1 && mat[nx][ny + 1] == '.') {
      next = (dir == 1 && nrem > 0) ? v(nk, nrem - 1, nx, ny + 1, 1)
                                    : v(nk + 1, k - 1, nx, ny + 1, 1);
      if (chmin(cost[nx][ny + 1][1], next)) {
        pq.push(next);
      }
    }
    if (ny > 0 && mat[nx][ny - 1] == '.') {
      next = (dir == 2 && nrem > 0) ? v(nk, nrem - 1, nx, ny - 1, 2)
                                    : v(nk + 1, k - 1, nx, ny - 1, 2);
      if (chmin(cost[nx][ny - 1][2], next)) {
        pq.push(next);
      }
    }
    if (nx > 0 && mat[nx - 1][ny] == '.') {
      next = (dir == 2 && nrem > 0) ? v(nk, nrem - 1, nx - 1, ny, 3)
                                    : v(nk + 1, k - 1, nx - 1, ny, 3);
      if (chmin(cost[nx - 1][ny][3], next)) {
        pq.push(next);
      }
    }
  }
  ll res = ll(1e+15);
  rep(i, 4) { chmin(res, cost[x[1]][y[1]][i].k); }
  cout << (res == ll(1e+15) ? -1 : res) << endl;
  return 0;
}