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

#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 1000000007

char c[1010][1010];
vector<vector<pair<int, int>>> graph(1010 * 1010);

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '#')
        continue;
      for (int l = max(0, i - 2); l < min(h, i + 3); l++) {
        for (int k = max(0, j - 2); k < min(w, j + 3); k++) {
          if ((l == i && k == j) || c[l][k] == '#')
            continue;
          if (abs(l - i) + abs(k - j) == 1) {
            graph[w * i + j].push_back(make_pair(w * l + k, 0));
          } else {
            graph[w * i + j].push_back(make_pair(w * l + k, 1));
          }
        }
      }
    }
  }
  priority_queue<pair<int, int>> qu;
  qu.push(make_pair(0, w * sx + sy));
  vector<int> res(h * w, MOD);
  while (!qu.empty()) {
    int now = (qu.top()).second;
    int cost = -(qu.top()).first;
    qu.pop();
    if (cost > res[now])
      continue;
    for (int i = 0; i < graph[now].size(); i++) {
      int next = graph[now][i].first;
      int nextc = cost + graph[now][i].second;
      if (res[next] > nextc) {
        res[next] = nextc;
        qu.push(make_pair(-nextc, next));
      }
    }
  }
  if (res[w * tx + ty] == MOD)
    cout << -1 << endl;
  else
    cout << res[w * tx + ty] << endl;
}

Error: 0

Fixed submission:

#include <memory.h>

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <utility>
#include <vector>

using namespace std;

#define MOD 1000000007

char c[1010][1010];
vector<vector<pair<int, int>>> graph(1010 * 1010);

int main() {
  int h, w;
  cin >> h >> w;
  int sx, sy, tx, ty;
  cin >> sx >> sy >> tx >> ty;
  sx--;
  sy--;
  tx--;
  ty--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> c[i][j];
    }
  }
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (c[i][j] == '#')
        continue;
      for (int l = max(0, i - 2); l < min(h, i + 3); l++) {
        for (int k = max(0, j - 2); k < min(w, j + 3); k++) {
          if ((l == i && k == j) || c[l][k] == '#')
            continue;
          if (abs(l - i) + abs(k - j) == 1) {
            graph[w * i + j].push_back(make_pair(w * l + k, 0));
          } else {
            graph[w * i + j].push_back(make_pair(w * l + k, 1));
          }
        }
      }
    }
  }
  priority_queue<pair<int, int>> qu;
  qu.push(make_pair(0, w * sx + sy));
  vector<int> res(w * w, MOD);
  while (!qu.empty()) {
    int now = (qu.top()).second;
    int cost = -(qu.top()).first;
    qu.pop();
    if (cost > res[now])
      continue;
    for (int i = 0; i < graph[now].size(); i++) {
      int next = graph[now][i].first;
      int nextc = cost + graph[now][i].second;
      if (res[next] > nextc) {
        res[next] = nextc;
        qu.push(make_pair(-nextc, next));
      }
    }
  }
  if (res[w * tx + ty] == MOD)
    cout << -1 << endl;
  else
    cout << res[w * tx + ty] << endl;
}