Problem p02579 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int H, W, sh, sw, gh, gw;
bool Grid[1010][1010];
int id[1010][1010];

int64_t INF = (1LL << 62);
int64_t MAXN = 100010;
vector<vector<pair<int, int>>> Graph(MAXN);

int64_t dijkstra(int64_t start, int64_t goal) {
  priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                 greater<pair<int64_t, int64_t>>>
      PQ;
  vector<int64_t> Color(MAXN, 0), D(MAXN, INF);

  D.at(start) = 0;
  PQ.push(make_pair(0, start));
  Color.at(start) = 1;

  while (!PQ.empty()) {
    pair<int64_t, int64_t> f = PQ.top();
    PQ.pop();
    int64_t u = f.second;
    Color.at(u) = 2;
    if (D.at(u) < f.first)
      continue;
    for (int i = 0; i < (int)(Graph.at(u).size()); i++) {
      int64_t v = Graph.at(u).at(i).first;
      if (Color.at(v) == 2)
        continue;
      if (D.at(v) > D.at(u) + Graph.at(u).at(i).second) {
        D.at(v) = D.at(u) + Graph.at(u).at(i).second;
        PQ.push(make_pair(D.at(v), v));
        Color.at(v) = 1;
      }
    }
  }
  return D.at(goal);
}

void initId() {
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      id[i][j] = cnt;
      cnt++;
    }
  }
}

int main() {
  cin >> H >> W >> sh >> sw >> gh >> gw;
  char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      Grid[i][j] = (c == '.');
    }
  }
  initId();
  vector<int> dh = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
                    0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
  vector<int> dw = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                    1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (!Grid[i][j])
        continue;
      int cur = id[i][j];
      for (int k = 0; k < 25; k++) {
        if (dh[k] == 0 && dw[k] == 0)
          continue;
        int nexth = i + dh[k], nextw = j + dw[k];
        if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
          continue;
        if (!Grid[nexth][nextw])
          continue;
        int nextv = id[nexth][nextw];
        if (abs(dh[k]) + abs(dw[k]) == 1) {
          Graph.at(cur).push_back({nextv, 0});
        } else {
          Graph.at(cur).push_back({nextv, 1});
        }
      }
    }
  }
  int64_t ans = dijkstra(id[sh - 1][sw - 1], id[gh - 1][gw - 1]);
  cout << (ans == INF ? -1 : ans) << endl;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int H, W, sh, sw, gh, gw;
bool Grid[1010][1010];
int id[1010][1010];

int64_t INF = (1LL << 62);
int64_t MAXN = 1000010;
vector<vector<pair<int, int>>> Graph(MAXN);

int64_t dijkstra(int64_t start, int64_t goal) {
  priority_queue<pair<int64_t, int64_t>, vector<pair<int64_t, int64_t>>,
                 greater<pair<int64_t, int64_t>>>
      PQ;
  vector<int64_t> Color(MAXN, 0), D(MAXN, INF);

  D.at(start) = 0;
  PQ.push(make_pair(0, start));
  Color.at(start) = 1;

  while (!PQ.empty()) {
    pair<int64_t, int64_t> f = PQ.top();
    PQ.pop();
    int64_t u = f.second;
    Color.at(u) = 2;
    if (D.at(u) < f.first)
      continue;
    for (int i = 0; i < (int)(Graph.at(u).size()); i++) {
      int64_t v = Graph.at(u).at(i).first;
      if (Color.at(v) == 2)
        continue;
      if (D.at(v) > D.at(u) + Graph.at(u).at(i).second) {
        D.at(v) = D.at(u) + Graph.at(u).at(i).second;
        PQ.push(make_pair(D.at(v), v));
        Color.at(v) = 1;
      }
    }
  }
  return D.at(goal);
}

void initId() {
  int cnt = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      id[i][j] = cnt;
      cnt++;
    }
  }
}

int main() {
  cin >> H >> W >> sh >> sw >> gh >> gw;
  char c;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> c;
      Grid[i][j] = (c == '.');
    }
  }
  initId();
  vector<int> dh = {-2, -2, -2, -2, -2, -1, -1, -1, -1, -1, 0, 0, 0,
                    0,  0,  1,  1,  1,  1,  1,  2,  2,  2,  2, 2};
  vector<int> dw = {-2, -1, 0,  1,  2, -2, -1, 0,  1,  2, -2, -1, 0,
                    1,  2,  -2, -1, 0, 1,  2,  -2, -1, 0, 1,  2};
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (!Grid[i][j])
        continue;
      int cur = id[i][j];
      for (int k = 0; k < 25; k++) {
        if (dh[k] == 0 && dw[k] == 0)
          continue;
        int nexth = i + dh[k], nextw = j + dw[k];
        if (nexth < 0 || nexth >= H || nextw < 0 || nextw >= W)
          continue;
        if (!Grid[nexth][nextw])
          continue;
        int nextv = id[nexth][nextw];
        if (abs(dh[k]) + abs(dw[k]) == 1) {
          Graph.at(cur).push_back({nextv, 0});
        } else {
          Graph.at(cur).push_back({nextv, 1});
        }
      }
    }
  }
  int64_t ans = dijkstra(id[sh - 1][sw - 1], id[gh - 1][gw - 1]);
  cout << (ans == INF ? -1 : ans) << endl;
}