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

#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;

int H, W;
#define index(a, b) ((a)*W + (b))
struct node {
  vector<int> to;
  vector<int> cost;
  ll min_cost;
  vector<int> path;
  node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
  priority_queue<P, vector<P>, greater<P>> q;
  nodes[start_index].min_cost = 0;
  q.push(P(0, start_index));
  while (q.size() != 0) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (nodes[v].min_cost < p.first)
      continue;
    for (int i = 0; i < nodes[v].to.size(); i++) {
      int to = nodes[v].to[i];
      ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
      if (nodes[to].min_cost > tmp_cost) {
        nodes[to].min_cost = tmp_cost;
        q.push(P(nodes[to].min_cost, to));
      }
    }
  }
}

int main() {
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  int start_index = index(Ch - 1, Cw - 1);
  int goal_index = index(Dh - 1, Dw - 1);
  vector<vector<bool>> road(H, vector<bool>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char S;
      cin >> S;
      road[i][j] = (S == '.');
    }
  }
  vector<node> nodes(H * W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < 3; k++) {
        for (int l = -2 * (k != 0); l < 3; l++) {
          if (k == 0 && l == 0)
            continue;
          if (!road[i][j])
            continue;
          if (i + k < H && 0 <= j + l && j + l < W) {
            if (road[i + k][j + l]) {
              nodes[index(i, j)].to.push_back(index(i + k, j + l));
              nodes[index(i + k, j + l)].to.push_back(index(i, j));
              nodes[index(i, j)].cost.push_back((k + abs(l) != 1) * 1);
              nodes[index(i + k, j + l)].cost.push_back((k + abs(l) != 1) * 1);
            }
          }
        }
      }
    }
  }
  dijkstra(start_index, nodes);
  if (nodes[goal_index].min_cost == INF)
    cout << -1 << endl;
  else
    cout << nodes[goal_index].min_cost << endl;
}

Error: 0

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
using namespace std;
#define INF 1000000000000000000
using ll = long long;
using P = pair<ll, int>;

int H, W;
#define index(a, b) ((a)*W + (b))
struct node {
  vector<int> to;
  vector<int> cost;
  ll min_cost;
  vector<int> path;
  node() { min_cost = INF; }
};
void dijkstra(int start_index, vector<node> &nodes) {
  priority_queue<P, vector<P>, greater<P>> q;
  nodes[start_index].min_cost = 0;
  q.push(P(0, start_index));
  while (q.size() != 0) {
    P p = q.top();
    q.pop();
    int v = p.second;
    if (nodes[v].min_cost < p.first)
      continue;
    for (int i = 0; i < nodes[v].to.size(); i++) {
      int to = nodes[v].to[i];
      ll tmp_cost = nodes[v].min_cost + nodes[v].cost[i];
      if (nodes[to].min_cost > tmp_cost) {
        nodes[to].min_cost = tmp_cost;
        q.push(P(nodes[to].min_cost, to));
      }
    }
  }
}

int main() {
  cin >> H >> W;
  int Ch, Cw, Dh, Dw;
  cin >> Ch >> Cw >> Dh >> Dw;
  int start_index = index(Ch - 1, Cw - 1);
  int goal_index = index(Dh - 1, Dw - 1);
  vector<vector<bool>> road(H, vector<bool>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      char S;
      cin >> S;
      road[i][j] = (S == '.');
    }
  }
  vector<node> nodes(H * W);
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      for (int k = 0; k < 3; k++) {
        for (int l = -2 * (k != 0); l < 3; l++) {
          if (k == 0 && l == 0)
            continue;
          if (i + k < H && 0 <= j + l < W) {
            if (road[i + k][j + l]) {
              nodes[index(i, j)].to.push_back(index(i + k, j + l));
              nodes[index(i + k, j + l)].to.push_back(index(i, j));
              nodes[index(i, j)].cost.push_back((k + l != 1) * 1);
              nodes[index(i + k, j + l)].cost.push_back((k + l != 1) * 1);
            }
          }
        }
      }
    }
  }
  dijkstra(start_index, nodes);
  if (nodes[goal_index].min_cost == INF)
    cout << -1 << endl;
  else
    cout << nodes[goal_index].min_cost << endl;
}