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

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const int INF = 1e8;

// 経路復元なしDijkstra法
template <class T>
vector<T> dijkstra(int start, T INF, const vector<vector<pair<int, T>>> &Hen) {
  int N = Hen.size();
  vector<T> dist(N, INF);
  vector<bool> seen(N);
  dist[start] = 0;
  priority_queue<pair<T, int>, vector<pair<T, int>>, greater<pair<T, int>>> que;
  que.emplace(0, start);

  while (que.size()) {
    int ver = que.top().second;
    T cost = que.top().first;
    que.pop();
    if (seen[ver])
      continue;
    seen[ver] = true;
    for (auto i : Hen[ver]) {
      if (dist[i.first] > cost + i.second) {
        dist[i.first] = cost + i.second;
        que.emplace(dist[i.first], i.first);
      }
    }
  }
  return dist;
}

const vector<vector<int>> dir4 = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
int h, w;

int id(int y, int x) { return y * w + x; }

int main() {
  cin >> h >> w;
  int ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  int dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<vector<bool>> g(h, vector<bool>(w));
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      char c;
      cin >> c;
      g[i][j] = (c == '#');
    }
  }
  int n = h * w;

  vector<vector<pair<int, int>>> hen(h * w + 100);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (g[i][j])
        continue;
      for (auto d : dir4) {
        int ny = i + d[0], nx = j + d[1];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (g[ny][nx])
          continue;
        hen[id(i, j)].push_back({id(ny, nx), 0});
      }

      for (int ny = i - 2; ny <= i + 2; ny++) {
        for (int nx = j - 2; nx <= j + 2; nx++) {
          if (ny == i && nx == j)
            continue;
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (g[ny][nx])
            continue;
          hen[id(i, j)].push_back({id(ny, nx), 1});
        }
      }
    }
  }

  int num = dijkstra(id(ch, cw), INF, hen)[id(dh, dw)];
  cout << (num == INF ? -1 : num) << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;
using ll = long long;
const ll INF = 1e8;

// 経路復元なしDijkstra法
template <class T>
vector<T> dijkstra(ll start, T INF, const vector<vector<pair<ll, T>>> &Hen) {
  ll N = Hen.size();
  vector<T> dist(N, INF);
  vector<bool> seen(N);
  dist[start] = 0;
  priority_queue<pair<T, ll>, vector<pair<T, ll>>, greater<pair<T, ll>>> que;
  que.emplace(0, start);

  while (que.size()) {
    ll ver = que.top().second;
    T cost = que.top().first;
    que.pop();
    if (seen[ver])
      continue;
    seen[ver] = true;
    for (auto i : Hen[ver]) {
      if (dist[i.first] > cost + i.second) {
        dist[i.first] = cost + i.second;
        que.emplace(dist[i.first], i.first);
      }
    }
  }
  return dist;
}

const vector<vector<ll>> dir4 = {{0, 1}, {1, 0}, {-1, 0}, {0, -1}};
ll h, w;

ll id(ll y, ll x) { return y * h + x; }

int main() {
  cin >> h >> w;
  ll ch, cw;
  cin >> ch >> cw;
  ch--, cw--;
  ll dh, dw;
  cin >> dh >> dw;
  dh--, dw--;

  vector<vector<bool>> g(h, vector<bool>(w));
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      char c;
      cin >> c;
      g[i][j] = (c == '#');
    }
  }
  ll n = h * w;

  vector<vector<pair<ll, ll>>> hen(h * w + 100);
  for (ll i = 0; i < h; i++) {
    for (ll j = 0; j < w; j++) {
      if (g[i][j])
        continue;
      for (auto d : dir4) {
        ll ny = i + d[0], nx = j + d[1];
        if (ny < 0 || ny >= h || nx < 0 || nx >= w)
          continue;
        if (g[ny][nx])
          continue;
        hen[id(i, j)].push_back({id(ny, nx), 0});
      }

      for (ll ny = i - 2; ny <= i + 2; ny++) {
        for (ll nx = j - 2; nx <= j + 2; nx++) {
          if (ny == i && nx == j)
            continue;
          if (ny < 0 || ny >= h || nx < 0 || nx >= w)
            continue;
          if (g[ny][nx])
            continue;
          hen[id(i, j)].push_back({id(ny, nx), 1});
        }
      }
    }
  }

  ll num = dijkstra(id(ch, cw), INF, hen)[id(dh, dw)];
  cout << (num == INF ? -1 : num) << endl;
}