Problem p02644 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lp;
typedef pair<ll, pair<ll, ll>> lpp;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T = int> void print(vector<T> v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;
}

/*
まっすぐk個まで進める：曲がったタイミングで精算

6 7 3
6 2 6 7
.@.@...
.@...@.
...@.@.
..@....
.@..@@@
..@....
*/

ll h, w, k, x3, x2, y3, y2, ans = 1e18;
priority_queue<lp, vector<lp>, greater<lp>> qu;
ll dx[] = {0, 0, 1, -1}, dy[] = {1, -1, 0, 0};

int main(void) {
  cin >> h >> w >> k;
  cin >> x3 >> y3 >> x2 >> y2;
  x3--;
  y3--;
  x2--;
  y2--;
  vector<string> s(h);
  vector<vector<vector<ll>>> d(h, vector<vector<ll>>(w, vector<ll>(4, 1e18)));
  rep(i, h) cin >> s[i];
  d[x3][y3][0] = 0;
  qu.push({0, (w * x3 + y3) * 4 + 0});
  while (!qu.empty()) {
    auto a = qu.top();
    qu.pop();
    ll cur = a.second;
    ll x = a.second / 4 / w, y = (a.second / 4) % w, v = a.second % 4;
    if (x == x2 && y == y2) {
      cout << (a.first + k - 1) / k << endl;
      return 0;
    }
    rep(i, 4) {                                        // 向きを変える
      if (d[x][y][i] > (d[x][y][v] + k - 1) / k * k) { // 切り上げ
        d[x][y][i] = (d[x][y][v] + k - 1) / k * k;
        qu.push({d[x][y][i], (x * w + y) * 4 + i});
      }
    }
    ll xx = x + dx[v], yy = y + dy[v];
    if (0 <= xx && xx < h && 0 <= yy && yy < w &&
        d[xx][yy][v] > d[x][y][v] + 1 && s[xx][yy] != '@') {
      d[xx][yy][v] = d[x][y][v] + 1;
      qu.push({d[xx][yy][v], (xx * w + yy) * 4 + v});
    }
  }
  cout << -1 << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll, ll> lp;
typedef pair<ll, pair<ll, ll>> lpp;
#define rep(i, n) for (ll i = 0; i < ((ll)(n)); i++)
#define reg(i, a, b) for (ll i = ((ll)(a)); i <= ((ll)(b)); i++)
#define irep(i, n) for (ll i = ((ll)(n)-1); i >= 0; i--)
#define ireg(i, a, b) for (ll i = ((ll)(b)); i >= ((ll)(a)); i--)
template <class T = int> using V = vector<T>;
template <class T = int> using VV = V<V<T>>;
template <class T = int> void print(vector<T> v) {
  for (auto a : v)
    cout << a << " ";
  cout << endl;
}

/*
 */

ll h, w, k, x_1, x2, y_1, y2, d[1000010][4];
char c[1000010];
priority_queue<lpp, vector<lpp>, greater<lpp>> qu;
ll dx[] = {0, 0, -1, 1}, dy[] = {1, -1, 0, 0};

int main(void) {
  cin >> h >> w >> k;
  cin >> x_1 >> y_1 >> x2 >> y2;
  x_1--;
  y_1--;
  x2--;
  y2--;
  rep(i, w * h) cin >> c[i];
  rep(i, w * h) rep(j, 4) d[i][j] = 1e9;
  d[x_1 * w + y_1][0] = 0;
  qu.push({0, {w * x_1 + y_1, 0}});
  while (!qu.empty()) {
    auto a = qu.top();
    qu.pop();
    ll x = a.second.first / w, y = a.second.first % w, v = a.second.second;
    if (d[(x + dx[v]) * w + y + dy[v]][v] > d[x * w + y][v] + 1 &&
        0 <= x + dx[v] && x + dx[v] < h && 0 <= y + dy[v] && y + dy[v] < w &&
        c[(x + dx[v]) * w + y + dy[v]] == '.') {
      d[(x + dx[v]) * w + y + dy[v]][v] = d[x * w + y][v] + 1;
      qu.push({d[(x + dx[v]) * w + y + dy[v]][v],
               {(x + dx[v]) * w + y + dy[v], v}});
    }
    rep(i, 4) {
      if (d[x * w + y][i] > (d[x * w + y][v] + k - 1) / k * k) { // 切り上げ
        d[x * w + y][i] = (d[x * w + y][v] + k - 1) / k * k;
        qu.push({d[x * w + y][i], {x * w + y, i}});
      }
    }
  }
  ll ans = 1e9;
  rep(i, 4) ans = min(ans, (d[x2 * w + y2][i] + k - 1) / k);
  cout << (ans == 1e9 ? -1 : ans) << endl;
  return 0;
}