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

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

int main() {
  int h, w, k;
  int x1, y1, x2, y2;
  static string s[1000002];
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= h; ++i) {
    string c;
    cin >> c;
    s[i] = "@" + c + "@";
  }
  s[0] = "";
  s[h + 1] = "";
  for (int j = 0; j <= w + 1; ++j) {
    s[0].push_back('@');
    s[h + 1].push_back('@');
  }

  auto idx = [&](int i, int j) { return (i - 1) * w + (j - 1); };
  static int64_t dist[4000005];
  for (int i = 0; i < idx(h, w) * 4 + 4; ++i)
    dist[i] = INT64_MIN;
  int dx[4] = {0, -1, 1, 0}, dy[4] = {1, 0, 0, -1};

  priority_queue<pair<int64_t, tuple<int, int, int>>> q;
  for (int i = 0; i < 4; ++i) {
    dist[idx(x1, y1) * 4 + i] = 0;
    q.emplace(0, tuple<int, int, int>{x1, y1, i});
  }

  while (!q.empty()) {
    auto [c, pos] = q.top();
    auto [x, y, d] = pos;
    int64_t ccl = (-c + k - 1) / k;
    q.pop();
    if (idx(x, y) == idx(x2, y2)) {
      cout << ccl << endl;
      return 0;
    }
    for (int i = 0; i < 4; ++i) {
      if (d == i) {
        if (s[x + dx[i]][y + dy[i]] == '.' &&
            c - 1 > dist[idx(x + dx[i], y + dy[i]) * 4 + i]) {
          dist[idx(x + dx[i], y + dy[i]) * 4 + i] = c - 1;
          q.emplace(c - 1, tuple<int, int, int>{x + dx[i], y + dy[i], i});
        }
      } else {
        if (-ccl * k > dist[idx(x, y) * 4 + i]) {
          dist[idx(x, y) * 4 + i] = -ccl * k;
          q.emplace(-ccl * k, tuple<int, int, int>{x, y, i});
        }
      }
    }
  }
  cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int h, w, k;
  int x1, y1, x2, y2;
  static string s[1000002];
  cin >> h >> w >> k;
  cin >> x1 >> y1 >> x2 >> y2;
  for (int i = 1; i <= h; ++i) {
    string c;
    cin >> c;
    s[i] = "@" + c + "@";
  }
  s[0] = "";
  s[h + 1] = "";
  for (int j = 0; j <= w + 1; ++j) {
    s[0].push_back('@');
    s[h + 1].push_back('@');
  }

  auto idx = [&](int i, int j) { return (i - 1) * w + (j - 1) + 1; };
  static bool vis[1000001];
  priority_queue<pair<int, int>> q;
  vis[idx(x1, y1)] = true;
  q.emplace(0, idx(x1, y1));

  static int tl[1000001], tr[1000001], tu[1000001], td[1000001];
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (i < h)
        td[idx(i, j)] = idx(i + 1, j);
      if (j < w)
        tr[idx(i, j)] = idx(i, j + 1);
      if (i > 1)
        tu[idx(i, j)] = idx(i - 1, j);
      if (j > 1)
        tl[idx(i, j)] = idx(i, j - 1);
    }
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      if (s[i][j] == '@') {
        if (i > 1)
          td[idx(i - 1, j)] = 0;
        if (j > 1)
          tr[idx(i, j - 1)] = 0;
        if (i < h)
          tu[idx(i + 1, j)] = 0;
        if (j < w)
          tl[idx(i, j + 1)] = 0;
      }
    }
  }

  while (!q.empty()) {
    auto [c, pos] = q.top();
    q.pop();
    if (pos == idx(x2, y2)) {
      cout << -c << endl;
      return 0;
    }

    for (int p = td[pos]; p != 0 && p <= pos + k * w; p = td[p]) {
      if (tl[p] != 0)
        tr[tl[p]] = tr[p];
      if (tr[p] != 0)
        tl[tr[p]] = tl[p];
      if (tu[p] != 0)
        td[tu[p]] = td[p];
      if (td[p] != 0)
        tu[td[p]] = tu[p];
      if (!vis[p]) {
        vis[p] = true;
        q.emplace(c - 1, p);
      }
    }
    for (int p = tu[pos]; p != 0 && pos - k * w <= p; p = tu[p]) {
      if (tl[p] != 0)
        tr[tl[p]] = tr[p];
      if (tr[p] != 0)
        tl[tr[p]] = tl[p];
      if (tu[p] != 0)
        td[tu[p]] = td[p];
      if (td[p] != 0)
        tu[td[p]] = tu[p];
      if (!vis[p]) {
        vis[p] = true;
        q.emplace(c - 1, p);
      }
    }
    for (int p = tr[pos]; p != 0 && p <= pos + k; p = tr[p]) {
      if (tl[p] != 0)
        tr[tl[p]] = tr[p];
      if (tr[p] != 0)
        tl[tr[p]] = tl[p];
      if (tu[p] != 0)
        td[tu[p]] = td[p];
      if (td[p] != 0)
        tu[td[p]] = tu[p];
      if (!vis[p]) {
        vis[p] = true;
        q.emplace(c - 1, p);
      }
    }
    for (int p = tl[pos]; p != 0 && pos - k <= p; p = tl[p]) {
      if (tl[p] != 0)
        tr[tl[p]] = tr[p];
      if (tr[p] != 0)
        tl[tr[p]] = tl[p];
      if (tu[p] != 0)
        td[tu[p]] = td[p];
      if (td[p] != 0)
        tu[td[p]] = tu[p];
      if (!vis[p]) {
        vis[p] = true;
        q.emplace(c - 1, p);
      }
    }
  }
  cout << -1 << endl;

  return 0;
}