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

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

int64_t zero = 0;
int64_t I = 167167167167167;

int main() {
  int H, W, x1, y1, x2, y2;
  int64_t K;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> G(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> G[i][j];
    }
  }
  vector<vector<vector<int64_t>>> D(
      H, vector<vector<int64_t>>(W, vector<int64_t>(4, I)));
  priority_queue<tuple<int64_t, int, int, int>,
                 vector<tuple<int64_t, int, int, int>>,
                 greater<tuple<int64_t, int, int, int>>>
      pq;
  vector<vector<vector<int64_t>>> seen(
      H, vector<vector<int64_t>>(W, vector<int64_t>(4, 0)));
  for (int i = 0; i < 4; i++) {
    D[x1][y1][i] = 0;
    pq.push(make_tuple(zero, x1, y1, i));
  }
  while (!pq.empty()) {
    int64_t d;
    int a, b, v;
    tie(d, a, b, v) = pq.top();
    pq.pop();
    if (seen[a][b][v]) {
      continue;
    }
    seen[a][b][v] = 1;
    int64_t Q = ((d + K - 1) / K) * K;
    for (int i = 0; i < 4; i++) {
      if (D[a][b][i] > Q) {
        D[a][b][i] = Q;
        pq.push(make_tuple(Q, a, b, i));
      }
    }
    d++;
    if (v == 0 && a + 1 < H) {
      if (G[a + 1][b] == '.') {
        if (D[a + 1][b][v] > d) {
          pq.push(make_tuple(d, a + 1, b, v));
        }
      }
    }
    if (v == 2 && a > 0) {
      if (G[a - 1][b] == '.') {
        if (D[a - 1][b][v] > d) {
          pq.push(make_tuple(d, a - 1, b, v));
        }
      }
    }
    if (v == 1 && b + 1 < W) {
      if (G[a][b + 1] == '.') {
        if (D[a][b + 1][v] > d) {
          pq.push(make_tuple(d, a, b + 1, v));
        }
      }
    }
    if (v == 3 && b > 0) {
      if (G[a][b - 1] == '.') {
        if (D[a][b - 1][v] > d) {
          pq.push(make_tuple(d, a, b - 1, v));
        }
      }
    }
  }
  int64_t Z =
      min(min(D[x2][y2][0], D[x2][y2][1]), min(D[x2][y2][2], D[x2][y2][3]));
  if (Z == I) {
    cout << "-1" << endl;
  } else {
    cout << (Z + K - 1) / K << endl;
  }
}

Error: TLE

Fixed submission:

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

int64_t zero = 0;
int64_t I = 167167167167167;

int main() {
  int H, W, x1, y1, x2, y2;
  int64_t K;
  cin >> H >> W >> K;
  cin >> x1 >> y1 >> x2 >> y2;
  x1--, y1--, x2--, y2--;
  vector<vector<char>> G(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> G[i][j];
    }
  }
  vector<vector<vector<int64_t>>> D(
      H, vector<vector<int64_t>>(W, vector<int64_t>(4, I)));
  priority_queue<tuple<int64_t, int, int, int>,
                 vector<tuple<int64_t, int, int, int>>,
                 greater<tuple<int64_t, int, int, int>>>
      pq;
  for (int i = 0; i < 4; i++) {
    D[x1][y1][i] = 0;
    pq.push(make_tuple(zero, x1, y1, i));
  }
  while (!pq.empty()) {
    int64_t d;
    int a, b, v;
    tie(d, a, b, v) = pq.top();
    pq.pop();
    if (D[a][b][v] < d) {
      continue;
    }
    int64_t Q = ((d + K - 1) / K) * K;
    for (int i = 0; i < 4; i++) {
      if (D[a][b][i] > Q) {
        D[a][b][i] = Q;
        pq.push(make_tuple(Q, a, b, i));
      }
    }
    d++;
    if (v == 0 && a + 1 < H) {
      if (G[a + 1][b] == '.') {
        if (D[a + 1][b][v] > d) {
          pq.push(make_tuple(d, a + 1, b, v));
        }
      }
    }
    if (v == 2 && a > 0) {
      if (G[a - 1][b] == '.') {
        if (D[a - 1][b][v] > d) {
          pq.push(make_tuple(d, a - 1, b, v));
        }
      }
    }
    if (v == 1 && b + 1 < W) {
      if (G[a][b + 1] == '.') {
        if (D[a][b + 1][v] > d) {
          pq.push(make_tuple(d, a, b + 1, v));
        }
      }
    }
    if (v == 3 && b > 0) {
      if (G[a][b - 1] == '.') {
        if (D[a][b - 1][v] > d) {
          pq.push(make_tuple(d, a, b - 1, v));
        }
      }
    }
  }
  int64_t Z =
      min(min(D[x2][y2][0], D[x2][y2][1]), min(D[x2][y2][2], D[x2][y2][3]));
  if (Z == I) {
    cout << "-1" << endl;
  } else {
    cout << (Z + K - 1) / K << endl;
  }
}