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

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int h, w;
int ch, cw, dh, dw;
string s[1000];

struct D {
  typedef pair<long long, int> P;
  struct edge {
    int to;
    long long cost;
    edge(int to, long long cost) : to(to), cost(cost) {}
  };
  vector<vector<edge>> graph;
  vector<long long> dist;

  void init(int size) {
    dist.assign(size, 1e18);
    graph.assign(size, vector<edge>());
  }

  void add_edge(int from, int to, long long cost) {
    graph[from].push_back(edge(to, cost));
  }

  void dijkstra(int from) {
    priority_queue<P, vector<P>, greater<P>> q;
    dist[from] = 0;
    q.push(P(0, from));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < graph[v].size(); i++) {
        edge e = graph[v][i];
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          q.push(P(dist[e.to], e.to));
        }
      }
    }
  }
};

int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++)
    cin >> s[i];

  D d;
  d.init(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] != '.')
        continue;
      for (int dx = -2; dx <= 2; dx++) {
        int nx = i + dx;
        if (nx < 0 || nx >= h)
          continue;
        for (int dy = -2; dy <= 2; dy++) {
          int ny = j + dy;
          if (ny < 0 || ny >= w)
            continue;
          if (s[nx][ny] != '.')
            continue;
          if (abs(dx) + abs(dy) <= 1) {
            d.add_edge(i * w + j, nx * w + ny, 0);
          } else {
            d.add_edge(i * w + j, nx * w + ny, 1);
          }
        }
      }
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  d.dijkstra(ch * w + cw);
  long long ans = d.dist[dh * w + dw];
  if (ans == 1e18)
    ans = -1;
  cout << ans << endl;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int h, w;
int ch, cw, dh, dw;
string s[1000];

struct D {
  typedef pair<long long, int> P;
  struct edge {
    int to;
    long long cost;
    edge(int to, long long cost) : to(to), cost(cost) {}
  };
  vector<vector<edge>> graph;
  vector<long long> dist;

  void init(int size) {
    dist.assign(size, 1e18);
    graph.assign(size, vector<edge>());
  }

  void add_edge(int from, int to, long long cost) {
    graph[from].push_back(edge(to, cost));
  }

  void dijkstra(int from) {
    priority_queue<P, vector<P>, greater<P>> q;
    dist[from] = 0;
    q.push(P(0, from));
    while (!q.empty()) {
      P p = q.top();
      q.pop();
      int v = p.second;
      if (dist[v] < p.first)
        continue;
      for (int i = 0; i < graph[v].size(); i++) {
        edge e = graph[v][i];
        if (dist[e.to] > dist[v] + e.cost) {
          dist[e.to] = dist[v] + e.cost;
          q.push(P(dist[e.to], e.to));
        }
      }
    }
  }
};

int main(void) {
  cin >> h >> w >> ch >> cw >> dh >> dw;
  for (int i = 0; i < h; i++)
    cin >> s[i];

  D d;
  d.init(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (s[i][j] != '.')
        continue;
      for (int dx = -2; dx <= 2; dx++) {
        int nx = i + dx;
        if (nx < 0 || nx >= h)
          continue;
        for (int dy = -2; dy <= 2; dy++) {
          int ny = j + dy;
          if (ny < 0 || ny >= w)
            continue;
          if (s[nx][ny] != '.')
            continue;
          if (abs(dx) + abs(dy) <= 1) {
            d.add_edge(i * h + j, nx * h + ny, 0);
          } else {
            d.add_edge(i * h + j, nx * h + ny, 1);
          }
        }
      }
    }
  }
  ch--;
  cw--;
  dh--;
  dw--;
  d.dijkstra(ch * h + cw);
  long long ans = d.dist[dh * h + dw];
  if (ans == 1e18)
    ans = -1;
  cout << ans << endl;
}