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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  vector<string> pond(H);
  for (int i = 0; i < H; ++i)
    cin >> pond[i];

  vector<vector<ll>> dis(H, vector<ll>(W, LINF));
  vector<vector<vector<int>>> dm(H, vector<vector<int>>(W, vector<int>(4)));
  dis[x1][y1] = 0;
  priority_queue<tuple<ll, ll, int, int>, vector<tuple<ll, ll, int, int>>,
                 greater<tuple<ll, ll, int, int>>>
      que;
  ll cnt = 0;
  que.push(make_tuple(0, cnt++, x1, y1));
  while (!que.empty()) {
    auto p = que.top();
    que.pop();
    ll d = get<0>(p);
    int x = get<2>(p), y = get<3>(p);
    if (d > dis[x][y])
      continue;
    for (int i = 0; i < 4; ++i) {
      int nx = x + dx[i], ny = y + dy[i];
      if (nx < 0 || nx >= H || ny < 0 || ny >= W || pond[nx][ny] == '@')
        continue;
      int m = dm[x][y][i];
      dm[x][y][i] = 0;
      if (m > 0) {
        if (d < dis[nx][ny]) {
          dis[nx][ny] = d;
          dm[nx][ny] = {0, 0, 0, 0};
          dm[nx][ny][i] = m - 1;
          que.push(make_tuple(dis[nx][ny], cnt++, nx, ny));
        } else if (d == dis[nx][ny]) {
          dm[nx][ny][i] = m - 1;
        }
      } else {
        if (d + 1 < dis[nx][ny]) {
          dis[nx][ny] = d + 1;
          dm[nx][ny] = {0, 0, 0, 0};
          dm[nx][ny][i] = K - 1;
          que.push(make_tuple(dis[nx][ny], cnt++, nx, ny));
        } else if (d + 1 == dis[nx][ny]) {
          dm[nx][ny][i] = K - 1;
        }
      }
    }
  }
  cout << (dis[x2][y2] == LINF ? -1 : dis[x2][y2]) << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
using Graph = vector<vector<int>>;
using WGraph = vector<vector<pair<int, ll>>>;

template <class T> inline bool chmax(T &a, const T &b) {
  if (b > a) {
    a = b;
    return true;
  }
  return false;
}
template <class T> inline bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return true;
  }
  return false;
}

constexpr int dx[4] = {-1, 0, 0, 1}, dy[4] = {0, -1, 1, 0};
constexpr int INF = 1e9;
constexpr int MOD = 1e9 + 7;
constexpr long long LINF = 1e18;
constexpr double EPS = 1e-10;
constexpr double PI = M_PI;

void solve() {
  int H, W, K, x1, y1, x2, y2;
  cin >> H >> W >> K >> x1 >> y1 >> x2 >> y2;
  --x1, --y1, --x2, --y2;
  string pond[H];
  for (int i = 0; i < H; ++i)
    cin >> pond[i];

  ll dis[H][W];
  for (int i = 0; i < H; ++i)
    for (int j = 0; j < W; ++j)
      dis[i][j] = LINF;
  dis[x1][y1] = 0;
  queue<pair<int, int>> que;
  que.push({x1, y1});
  while (!que.empty()) {
    auto p = que.front();
    que.pop();
    int x = p.first, y = p.second, d = dis[p.first][p.second];
    if (x == x2 && y == y2)
      break;
    for (int j = 0; j < 4; ++j) {
      for (int i = 1; i <= K; ++i) {
        int nx = x + dx[j] * i, ny = y + dy[j] * i;
        if (nx < 0 || nx >= H || ny < 0 || ny >= W || pond[nx][ny] == '@')
          break;
        if (dis[nx][ny] == LINF) {
          dis[nx][ny] = d + 1;
          que.push({nx, ny});
        }
      }
    }
  }
  cout << (dis[x2][y2] == LINF ? -1 : dis[x2][y2]) << '\\n';
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(20);

  solve();

  return 0;
}