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

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

int main() {
  int64_t H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vector<string> S(H);
  for (string &s : S) {
    cin >> s;
  }

  // 繋がっているところは一つにまとめる
  constexpr int64_t DIR_NUM = 4;
  constexpr int64_t dx[DIR_NUM] = {0, 1, 0, -1};
  constexpr int64_t dy[DIR_NUM] = {1, 0, -1, 0};

  constexpr int64_t SKIP_WIDTH = 2;

  vector<int64_t> cost(H * W, LLONG_MAX);
  struct Element {
    int64_t node, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;

  cost[Ch * W + Cw] = 0;
  pq.push({Ch * W + Cw, 0});
  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.node]) {
      continue;
    }

    for (int64_t k = 0; k < DIR_NUM; k++) {
      int64_t ni = t.node / W + dy[k];
      int64_t nj = t.node % W + dx[k];
      if (ni < 0 || H <= ni || nj < 0 || W <= nj || S[ni][nj] == '#') {
        continue;
      }

      int64_t next_index = ni * W + nj;
      int64_t new_cost = t.cost;
      if (new_cost < cost[next_index]) {
        pq.push({next_index, cost[next_index] = new_cost});
      }
    }

    for (int64_t i = -SKIP_WIDTH; i <= SKIP_WIDTH; i++) {
      for (int64_t j = -SKIP_WIDTH; j <= SKIP_WIDTH; j++) {
        int64_t ni = t.node / W + i;
        int64_t nj = t.node % W + j;
        if (ni < 0 || H <= ni || nj < 0 || W <= nj || S[ni][nj] == '#') {
          continue;
        }

        int64_t next_index = ni * W + nj;
        int64_t new_cost = t.cost + 1;
        if (new_cost < cost[next_index]) {
          pq.push({next_index, cost[next_index] = new_cost});
        }
      }
    }
  }

  int64_t goal_index = Dh * W + Dw;
  cout << (cost[goal_index] == LLONG_MAX ? -1 : cost[goal_index]) << endl;
}

Error: 0

Fixed submission:

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

int main() {
  int64_t H, W, Ch, Cw, Dh, Dw;
  cin >> H >> W >> Ch >> Cw >> Dh >> Dw;
  Ch--;
  Cw--;
  Dh--;
  Dw--;

  vector<string> S(H);
  for (string &s : S) {
    cin >> s;
  }

  // 繋がっているところは一つにまとめる
  constexpr int64_t DIR_NUM = 4;
  constexpr int64_t dx[DIR_NUM] = {0, 1, 0, -1};
  constexpr int64_t dy[DIR_NUM] = {1, 0, -1, 0};

  constexpr int64_t SKIP_WIDTH = 2;

  vector<int64_t> cost(H * W, LLONG_MAX);
  struct Element {
    int64_t node, cost;
    bool operator>(const Element &rhs) const { return cost > rhs.cost; }
  };
  priority_queue<Element, vector<Element>, greater<Element>> pq;

  cost[Ch * H + Cw] = 0;
  pq.push({Ch * H + Cw, 0});
  while (!pq.empty()) {
    Element t = pq.top();
    pq.pop();
    if (t.cost > cost[t.node]) {
      continue;
    }

    for (int64_t k = 0; k < DIR_NUM; k++) {
      int64_t ni = t.node / H + dy[k];
      int64_t nj = t.node % H + dx[k];
      if (ni < 0 || H <= ni || nj < 0 || W <= nj || S[ni][nj] == '#') {
        continue;
      }

      int64_t next_index = ni * H + nj;
      int64_t new_cost = t.cost;
      if (new_cost < cost[next_index]) {
        pq.push({next_index, cost[next_index] = new_cost});
      }
    }

    for (int64_t i = -SKIP_WIDTH; i <= SKIP_WIDTH; i++) {
      for (int64_t j = -SKIP_WIDTH; j <= SKIP_WIDTH; j++) {
        int64_t ni = t.node / H + i;
        int64_t nj = t.node % H + j;
        if (ni < 0 || H <= ni || nj < 0 || W <= nj || S[ni][nj] == '#') {
          continue;
        }

        int64_t next_index = ni * H + nj;
        int64_t new_cost = t.cost + 1;
        if (new_cost < cost[next_index]) {
          pq.push({next_index, cost[next_index] = new_cost});
        }
      }
    }
  }

  int64_t goal_index = Dh * H + Dw;
  cout << (cost[goal_index] == LLONG_MAX ? -1 : cost[goal_index]) << endl;
}