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

#include <bits/stdc++.h>
using namespace std;
// dijkstra

struct edge {
  int to;
  int cost;
};

using P = pair<int, int>;

const int MAX_N = 1e6;
const int INF = 1e9;
vector<edge> G[MAX_N];
vector<int> dist(MAX_N, INF);

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> PQ;
  dist[s] = 0;
  PQ.push(P(0, s));

  while (PQ.size()) {
    P p = PQ.top();
    PQ.pop();
    int v = p.second;        // from
    if (dist[v] < p.first) { // 現在わかっている距離が更新できない
      continue;
    }
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        PQ.push(P(dist[e.to], e.to));
      }
    }
  }
}

int main() {
  int H, W, sx, sy, gx, gy;
  cin >> H >> W >> sx >> sy >> gx >> gy;
  vector<vector<char>> maze(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }

  vector<int> dx = {0, 1, 2, -1, -2};
  vector<int> dy = {0, 1, 2, -1, -2};

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '.') {
        for (int k = 0; k < 5; k++) {
          for (int l = 0; l < 5; l++) {
            if (i + dx[k] >= 0 && i + dx[k] < H && j + dy[l] >= 0 &&
                j + dy[l] < W) {
              if (abs(dx[k]) + abs(dy[l]) == 1) {
                int t = (i + dx[k]) * H + (j + dy[l]);
                edge add;
                add.to = t;
                add.cost = 0;
                G[i * H + j].push_back(add);
              }
              if (abs(dx[k]) + abs(dy[l]) > 1) {
                int t = (i + dx[k]) * H + (j + dy[l]);
                edge add;
                add.to = t;
                add.cost = 1;
                G[i * H + j].push_back(add);
              }
            }
          }
        }
      }
    }
  }
  sx--;
  sy--;
  gx--;
  gy--;
  dijkstra(sx * H + sy);
  if (dist[gx * H + gy] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gx * H + gy] << endl;
  }
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
// dijkstra

struct edge {
  int to;
  int cost;
};

using P = pair<int, int>;

const int MAX_N = 1e6;
const int INF = 1e9;
vector<edge> G[MAX_N];
vector<int> dist(MAX_N, INF);

void dijkstra(int s) {
  priority_queue<P, vector<P>, greater<P>> PQ;
  dist[s] = 0;
  PQ.push(P(0, s));

  while (PQ.size()) {
    P p = PQ.top();
    PQ.pop();
    int v = p.second;        // from
    if (dist[v] < p.first) { // 現在わかっている距離が更新できない
      continue;
    }
    for (int i = 0; i < G[v].size(); i++) {
      edge e = G[v][i];
      if (dist[e.to] > dist[v] + e.cost) {
        dist[e.to] = dist[v] + e.cost;
        PQ.push(P(dist[e.to], e.to));
      }
    }
  }
}

int main() {
  int H, W, sx, sy, gx, gy;
  cin >> H >> W >> sx >> sy >> gx >> gy;
  vector<vector<char>> maze(H, vector<char>(W));
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      cin >> maze[i][j];
    }
  }

  vector<int> dx = {0, 1, 2, -1, -2};
  vector<int> dy = {0, 1, 2, -1, -2};

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (maze[i][j] == '.') {
        for (int k = 0; k < 5; k++) {
          for (int l = 0; l < 5; l++) {
            if (i + k >= 0 && i + k < H && j + l >= 0 && j + l < W) {
              if (abs(dx[k]) + abs(dy[l]) == 1) {
                int t = (i + dx[k]) * H + (j + dy[l]);
                edge add;
                add.to = t;
                add.cost = 0;
                G[i * H + j].push_back(add);
              }
              if (abs(dx[k]) + abs(dy[l]) > 1) {
                int t = (i + dx[k]) * H + (j + dy[l]);
                edge add;
                add.to = t;
                add.cost = 1;
                G[i * H + j].push_back(add);
              }
            }
          }
        }
      }
    }
  }
  sx--;
  sy--;
  gx--;
  gy--;
  dijkstra(sx * H + sy);
  if (dist[gx * H + gy] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[gx * H + gy] << endl;
  }
}