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

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, int> pil;

long long MOD = 10000000000 + 7;

// dijkstra法
int INF = 1000000;
int N;
vector<vector<int>> v;
vector<int> dist;

bool operator>(pil x, pil y) { return x.second > y.second; }

void shortest_path(int s) {
  // sから各点への最短距離計算
  dist.assign(N, INF);
  priority_queue<pil, vector<pil>, greater<pil>> q;

  dist[s] = 0;
  map<int, int> check;
  q.push(make_pair(s, dist[s]));
  check[s] = 1;

  while (!q.empty()) {
    pil now = q.top();
    int now_v = now.first;
    int now_dist = now.second;
    check[now_v] = 1;
    q.pop();

    if (dist[now_v] < now_dist)
      continue;

    for (int next_v : v.at(now_v)) {
      if (dist[next_v] > dist[now_v] + 1) {
        dist[next_v] = dist[now_v] + 1;
        q.push(make_pair(next_v, dist[next_v]));
      }
    }
  }
}

int main() {
  cout << setprecision(10);
  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;

  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  vector<vector<int>> sim(H, vector<int>(W, -1));

  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};

  int sim_num = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      if (sim[i][j] == -1 && S[i][j] == '.') {
        sim[i][j] = sim_num;
        queue<pii> q;
        q.push(make_pair(i, j));
        while (!q.empty()) {
          int now_x = q.front().first;
          int now_y = q.front().second;
          q.pop();

          for (int k = 0; k < 4; k++) {
            int next_x = now_x + dx[k];
            int next_y = now_y + dy[k];

            if (next_x < 0 || next_x >= H)
              continue;
            if (next_y < 0 || next_y >= W)
              continue;
            if (S[next_x][next_y] == '#')
              continue;

            if (sim[next_x][next_y] == -1) {
              sim[next_x][next_y] = sim_num;
              q.push(make_pair(next_x, next_y));
            }
          }
        }
        sim_num++;
      }
    }
  }

  N = sim_num;
  v.assign(sim_num, vector<int>());

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (sim[i][j] == -1)
        continue;

      int now = sim[i][j]; // 今の同地類
      // cerr << " now: " << now << endl;
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int next_x = i + k;
          int next_y = j + l;

          if (next_x < 0 || next_x >= H)
            continue;
          if (next_y < 0 || next_y >= W)
            continue;
          if (sim[next_x][next_y] == -1)
            continue;

          int next = sim[next_x][next_y];
          if (now == next)
            continue;

          // if(check[now][next] == -1){
          if (find(v[now].begin(), v[now].end(), next) == v[now].end()) {
            v[now].push_back(next);
            v[next].push_back(now);
          }
        }
      }
    }
  }

  int start = sim[sx][sy];
  int goal = sim[gx][gy];
  shortest_path(start);

  if (start == goal) {
    cout << 0 << endl;
    return 0;
  }

  if (dist[goal] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[goal] << endl;
  }
}


Error: 0

Fixed submission:

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

typedef long long ll;
typedef pair<int, int> pii;
typedef pair<int, int> pil;

long long MOD = 10000000000 + 7;

// dijkstra法
int INF = 100000;
int N;
vector<vector<int>> v;
vector<int> dist;

bool operator>(pil x, pil y) { return x.second > y.second; }

void shortest_path(int s) {
  // sから各点への最短距離計算
  dist.assign(N, INF);
  priority_queue<pil, vector<pil>, greater<pil>> q;

  dist[s] = 0;
  map<int, int> check;
  q.push(make_pair(s, dist[s]));
  check[s] = 1;

  while (!q.empty()) {
    pil now = q.top();
    int now_v = now.first;
    int now_dist = now.second;
    check[now_v] = 1;
    q.pop();

    if (dist[now_v] < now_dist)
      continue;

    for (int next_v : v.at(now_v)) {
      if (dist[next_v] > dist[now_v] + 1) {
        dist[next_v] = dist[now_v] + 1;
        q.push(make_pair(next_v, dist[next_v]));
      }
    }
  }
}

int main() {
  cout << setprecision(10);
  int H, W;
  cin >> H >> W;
  int sx, sy;
  cin >> sx >> sy;
  sx--;
  sy--;
  int gx, gy;
  cin >> gx >> gy;
  gx--;
  gy--;

  vector<string> S(H);
  for (int i = 0; i < H; i++) {
    cin >> S[i];
  }

  vector<vector<int>> sim(H, vector<int>(W, -1));

  int dx[4] = {0, 1, 0, -1};
  int dy[4] = {1, 0, -1, 0};

  int sim_num = 0;
  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {

      if (sim[i][j] == -1 && S[i][j] == '.') {
        sim[i][j] = sim_num;
        queue<pii> q;
        q.push(make_pair(i, j));
        while (!q.empty()) {
          int now_x = q.front().first;
          int now_y = q.front().second;
          q.pop();

          for (int k = 0; k < 4; k++) {
            int next_x = now_x + dx[k];
            int next_y = now_y + dy[k];

            if (next_x < 0 || next_x >= H)
              continue;
            if (next_y < 0 || next_y >= W)
              continue;
            if (S[next_x][next_y] == '#')
              continue;

            if (sim[next_x][next_y] == -1) {
              sim[next_x][next_y] = sim_num;
              q.push(make_pair(next_x, next_y));
            }
          }
        }
        sim_num++;
      }
    }
  }

  N = sim_num;
  v.assign(sim_num, vector<int>());
  vector<vector<int>> check(sim_num, vector<int>(sim_num, -1));

  for (int i = 0; i < H; i++) {
    for (int j = 0; j < W; j++) {
      if (sim[i][j] == -1)
        continue;

      int now = sim[i][j]; // 今の同地類
      // cerr << " now: " << now << endl;
      for (int k = -2; k <= 2; k++) {
        for (int l = -2; l <= 2; l++) {
          int next_x = i + k;
          int next_y = j + l;

          if (next_x < 0 || next_x >= H)
            continue;
          if (next_y < 0 || next_y >= W)
            continue;
          if (sim[next_x][next_y] == -1)
            continue;

          int next = sim[next_x][next_y];
          if (now == next)
            continue;

          if (check[now][next] == -1) {
            v[now].push_back(next);
            v[next].push_back(now);

            check[now][next] = 1;
            check[next][now] = 1;
          }
        }
      }
    }
  }

  int start = sim[sx][sy];
  int goal = sim[gx][gy];
  shortest_path(start);

  if (start == goal) {
    cout << 0 << endl;
    return 0;
  }

  if (dist[goal] == INF) {
    cout << -1 << endl;
  } else {
    cout << dist[goal] << endl;
  }
}