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

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

ll edgeid(ll h, ll w, ll W) { return h * W + w; }

struct edge {
  ll u;
  ll v;
  ll cost;
};
bool operator<=(const edge &lhs, const edge &rhs) {
  return lhs.cost <= rhs.cost;
}
bool operator<(const edge &lhs, const edge &rhs) { return lhs.cost < rhs.cost; }
bool operator>=(const edge &lhs, const edge &rhs) {
  return lhs.cost >= rhs.cost;
}
bool operator>(const edge &lhs, const edge &rhs) { return lhs.cost > rhs.cost; }
bool operator==(const edge &lhs, const edge &rhs) {
  return lhs.cost == rhs.cost;
}
bool operator!=(const edge &lhs, const edge &rhs) {
  return lhs.cost != rhs.cost;
}

void dijkstra(ll n, ll start, vector<vector<edge>> &edgemap,
              vector<ll> &min_cost) {
  vector<bool> done(n + 1);
  for (ll j = 0; j <= n; j++) {
    min_cost[j] = -1;
    done[j] = false;
  }
  priority_queue<edge, vector<edge>, greater<edge>> pq;

  pq.push(edge{u : -1, v : start, cost : 0});
  while (!pq.empty()) {
    edge e = pq.top();
    pq.pop();
    if (done[e.v]) {
      continue;
    }
    done[e.v] = true;
    min_cost[e.v] = e.cost;
    for (auto next_e : edgemap[e.v]) {
      if (!done[next_e.v]) {
        edge total_next_e = next_e;
        total_next_e.cost = min_cost[next_e.u] + next_e.cost;
        pq.push(total_next_e);
      }
    }
  }
}

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<edge>> edgemap(H * W);
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      for (ll y = h - 2; y <= h + 2; y++) {
        ll u = edgeid(h, w, W);
        for (ll x = w - 2; x <= w + 2; x++) {
          if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x]) {
            ll v = edgeid(y, x, W);
            if (y == h && x == w) {
              continue;
            } else if (abs(x - w) + abs(y - h) == 1) {
              edgemap[u].push_back({u : u, v : v, cost : 0});
            } else {
              edgemap[u].push_back({u : u, v : v, cost : 1});
            }
          }
        }
      }
    }
  }
  vector<ll> min_cost(H * W);
  dijkstra(H * W, edgeid(sh, sw, W), edgemap, min_cost);
  cout << min_cost[edgeid(gh, gw, W)] << endl;
}


Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using ll = int64_t;
using ull = uint64_t;
const ll INF = 9e18;

ll edgeid(ll h, ll w, ll H) { return h * H + w; }

struct edge {
  ll u;
  ll v;
  ll cost;
};
bool operator<=(const edge &lhs, const edge &rhs) {
  return lhs.cost <= rhs.cost;
}
bool operator<(const edge &lhs, const edge &rhs) { return lhs.cost < rhs.cost; }
bool operator>=(const edge &lhs, const edge &rhs) {
  return lhs.cost >= rhs.cost;
}
bool operator>(const edge &lhs, const edge &rhs) { return lhs.cost > rhs.cost; }
bool operator==(const edge &lhs, const edge &rhs) {
  return lhs.cost == rhs.cost;
}
bool operator!=(const edge &lhs, const edge &rhs) {
  return lhs.cost != rhs.cost;
}

void dijkstra(ll n, ll start, vector<vector<edge>> &edgemap,
              vector<ll> &min_cost) {
  vector<bool> done(n + 1);
  for (ll j = 0; j <= n; j++) {
    min_cost[j] = -1;
    done[j] = false;
  }
  priority_queue<edge, vector<edge>, greater<edge>> pq;

  pq.push(edge{u : -1, v : start, cost : 0});
  while (!pq.empty()) {
    edge e = pq.top();
    pq.pop();
    if (done[e.v]) {
      continue;
    }
    done[e.v] = true;
    min_cost[e.v] = e.cost;
    for (auto next_e : edgemap[e.v]) {
      if (!done[next_e.v]) {
        edge total_next_e = next_e;
        total_next_e.cost = min_cost[next_e.u] + next_e.cost;
        pq.push(total_next_e);
      }
    }
  }
}

int main() {
  ll H, W;
  cin >> H >> W;
  ll sh, sw, gh, gw;
  cin >> sh >> sw;
  cin >> gh >> gw;
  sh--;
  sw--;
  gh--;
  gw--;

  // 道の一覧
  vector<vector<bool>> roads(H, vector<bool>(W, false));
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      char c;
      cin >> c;
      while (c != '.' && c != '#') {
        cin >> c;
      }
      if (c == '.') {
        roads[h][w] = true;
      }
    }
  }

  vector<vector<edge>> edgemap(H * W + 1);
  for (ll h = 0; h < H; h++) {
    for (ll w = 0; w < W; w++) {
      for (ll y = h - 2; y <= h + 2; y++) {
        ll u = edgeid(h, w, H);
        for (ll x = w - 2; x <= w + 2; x++) {
          if (y >= 0 && y < H && x >= 0 && x < W && roads[y][x]) {
            ll v = edgeid(y, x, H);
            if (y == h && x == w) {
              continue;
            } else if (abs(x - w) + abs(y - h) == 1) {
              edgemap[u].push_back({u : u, v : v, cost : 0});
            } else {
              edgemap[u].push_back({u : u, v : v, cost : 1});
            }
          }
        }
      }
    }
  }
  vector<ll> min_cost(H * W + 1);
  dijkstra(H * W, edgeid(sh, sw, H), edgemap, min_cost);
  cout << min_cost[edgeid(gh, gw, H)] << endl;
}