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

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

#define INF 1e+15
#define MAX_V 1000000

struct edge {
  long long to;
};

// <最短距離, 頂点の番号>
using P = pair<long long, pair<long long, bool>>;

long long V;
long long K;
vector<edge> G[MAX_V][2];
long long d[MAX_V][2];

void dijkstra(long long s, bool direct) {
  priority_queue<P, vector<P>, greater<P>> que;
  for (long long i = 0; i < V; i++) {
    d[i][1] = INF;
    d[i][0] = INF;
  }
  d[s][1] = 0;
  d[s][0] = 0;
  que.push(make_pair(0, make_pair(s, direct)));

  while (!que.empty()) {
    P p = que.top();
    que.pop();
    long long v = p.second.first;
    bool ar = p.second.second;
    if (d[v][ar] < p.first)
      continue;
    // if prim,
    // d[v] = -d[v];
    // for keep tree
    for (long long i = 0; i < G[v][ar].size(); ++i) {
      edge e = G[v][ar][i];
      if (d[e.to][ar] > d[v][ar] + 1) {
        d[e.to][ar] = d[v][ar] + 1;
        que.push(make_pair(d[e.to][ar], make_pair(e.to, ar)));
      }
    }
    for (long long i = 0; i < G[v][!p.second.second].size(); ++i) {
      edge e = G[v][!p.second.second][i];
      if (d[v][ar] % K == 0) {
        if (d[e.to][!ar] > d[v][ar] + 1) {
          d[e.to][!ar] = d[v][ar] + 1;
          que.push(make_pair(d[e.to][!ar], make_pair(e.to, !p.second.second)));
        }
      } else {
        if (d[e.to][!ar] > d[v][ar] - (d[v][ar] % K) + K + 1) {
          d[e.to][!ar] = d[v][ar] - (d[v][ar] % K) + K + 1;
          que.push(make_pair(d[e.to][!ar], make_pair(e.to, !p.second.second)));
        }
      }
    }
  }
}

int main() {
  // input
  long long H, W;
  cin >> H >> W >> K;
  V = H * W;
  long long x1, y1, x2, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string c[H];
  long long i, j;
  for (i = 0; i < H; i++) {
    cin >> c[i];
  }

  // calc
  for (i = 0; i < H; i++) {
    for (j = 0; j < W - 1; j++) {
      if ((c[i][j] == '.') && (c[i][j + 1] == '.')) {
        G[W * i + j][0].push_back({W * i + j + 1});
        G[W * i + j + 1][0].push_back({W * i + j});
      }
    }
  }
  for (i = 0; i < H - 1; i++) {
    for (j = 0; j < W; j++) {
      if ((c[i][j] == '.') && (c[i + 1][j] == '.')) {
        G[W * i + j][1].push_back({W * (i + 1) + j});
        G[W * (i + 1) + j][1].push_back({W * i + j});
      }
    }
  }
  long long ans;
  dijkstra(W * (x1 - 1) + (y1 - 1), 1);
  ans = min(d[W * (x2 - 1) + (y2 - 1)][1], d[W * (x2 - 1) + (y2 - 1)][0]);

  // answer
  if (ans == INF) {
    cout << -1 << endl;
  } else {
    cout << (ans + K - 1) / K << endl;
  }
  system("pause");
  return 0;
}

Error: -11

Fixed submission:

#include <iostream>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int main() {
  int H, W, K;
  cin >> H >> W >> K;
  int x1, x2, y1, y2;
  cin >> x1 >> y1 >> x2 >> y2;
  string S;
  vector<string> c;
  int i, j;
  for (i = 0; i < H; i++) {
    cin >> S;
    c.push_back(S);
  }

  pair<int, int> dad[H][W];
  queue<pair<int, int>> que;
  bool val[H][W];
  for (i = 0; i < H; i++) {
    for (j = 0; j < W; j++) {
      val[i][j] = 0;
    }
  }
  dad[x1][y1] = make_pair(x1, y1);
  que.push(make_pair(x1, y1));
  val[x1][y1] = 1;
  pair<int, int> p;
  int s, t;
  int a, b;
  while (!que.empty()) {
    p = que.front();
    que.pop();
    s = p.first;
    t = p.second;
    if (s > 0) {
      if (c[s][t] == '.' && c[s - 1][t] == '.') {
        if (val[s - 1][t] == 0) {
          val[s - 1][t] = 1;
          dad[s - 1][t] = make_pair(s, t);
          que.push(make_pair(s - 1, t));
        }
      }
    }
    if (s < H - 1) {
      if (c[s][t] == '.' && c[s + 1][t] == '.') {
        if (val[s + 1][t] == 0) {
          val[s + 1][t] = 1;
          dad[s + 1][t] = make_pair(s, t);
          que.push(make_pair(s + 1, t));
        }
      }
    }
    if (t > 0) {
      if (c[s][t] == '.' && c[s][t - 1] == '.') {
        if (val[s][t - 1] == 0) {
          val[s][t - 1] = 1;
          dad[s][t - 1] = make_pair(s, t);
          que.push(make_pair(s, t - 1));
        }
      }
    }
    if (t < W - 1) {
      if (c[s][t] == '.' && c[s][t + 1] == '.') {
        if (val[s][t + 1] == 0) {
          val[s][t + 1] = 1;
          dad[s][t + 1] = make_pair(s, t);
          que.push(make_pair(s, t + 1));
        }
      }
    }
  }
  if (val[x2][y2] == 0) {
    cout << -1 << endl;
  } else {
    vector<pair<int, int>> keiro;
    s = x2;
    t = y2;
    while (s != x1 || t != y1) {
      keiro.push_back(make_pair(s, t));
      a = s;
      b = t;
      s = dad[a][b].first;
      t = dad[a][b].second;
    }
    keiro.push_back(make_pair(x1, y1));
    keiro.push_back(make_pair(x1, y1));
    vector<int> senbun;
    bool tayo;
    int counta = 0;
    s = dad[x2][y2].first;
    t = dad[x2][y2].second;
    if (t != y2) {
      tayo = 1;
    } else {
      tayo = 0;
    }
    for (i = 0; i < keiro.size() - 1; i++) {
      s = keiro[i].first;
      t = keiro[i].second;
      a = keiro[i + 1].first;
      b = keiro[i + 1].second;
      if (tayo == 1) {
        if (t != b) {
          counta++;
        } else {
          senbun.push_back(counta);
          counta = 1;
          tayo = 0;
        }
      } else {
        if (s != a) {
          counta++;
        } else {
          senbun.push_back(counta);
          counta = 1;
          tayo = 1;
        }
      }
    }
    counta = 0;
    int sub;
    for (i = 0; i < senbun.size(); i++) {
      counta += senbun[i] / K;
      if (senbun[i] % K != 0) {
        counta++;
      }
    }
    cout << counta << endl;
  }
  system("pause");
  return 0;
}