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

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

int n, m, s, dis[maxn], s1, t1, s2, t2, N, M;
struct node {
  int v, w;
};
vector<node> mp[maxn];
struct node2 {
  int dis, to;
};
char p[1005][1005];

inline bool operator<(node2 a, node2 b) { return a.dis > b.dis; }

priority_queue<node2> que;
bool vis[maxn];

inline int A(int x, int y) { return M * (x - 1) + y; }
inline void Add(int u, int v, int w) { mp[u].pb({v, w}), mp[v].pb({u, w}); }

const int kx[4] = {0, 0, 1, -1};
const int ky[4] = {1, -1, 0, 0};

int main() {
  N = read(), M = read();
  s1 = read();
  t1 = read();
  s2 = read();
  t2 = read();
  rep(i, 1, N) scanf("%s", p[i] + 1);
  rep(i, 1, N) rep(j, 1, M) {
    if (p[i][j] == '.') {
      rep(k, 0, 3) {
        int tx = i + kx[k], ty = j + ky[k];
        if (tx <= 0 || tx > N || ty <= 0 || ty > M || p[tx][ty] == '#')
          continue;
        Add(A(i, j), A(tx, ty), 0);
      }

      rep(w, -2, 2) rep(k, -2, 2) {
        int tx = i + w, ty = j + k;
        if (tx <= 0 || tx > N || ty <= 0 || ty > M || p[tx][ty] == '#')
          continue;
        Add(A(i, j), A(tx, ty), 1);
      }
    }
  }

  n = N * M;
  s = A(s1, t1);
  for (int i = 1; i <= n; i++)
    dis[i] = inf;
  dis[s] = 0;
  que.push((node2){0, s});
  while (!que.empty()) {
    node2 u = que.top();
    que.pop();
    if (vis[u.to])
      continue;
    vis[u.to] = 1;
    int uu = u.to; // cout<<uu<<endl;
    for (int i = 0; i < mp[uu].size(); i++) {
      node v = mp[uu][i];
      if (vis[v.v])
        continue;
      if (dis[uu] + v.w < dis[v.v]) {
        dis[v.v] = dis[uu] + v.w;
        que.push((node2){dis[v.v], v.v});
      }
    }
  }
  if (dis[A(s2, t2)] == inf)
    puts("-1");
  else
    printf("%d ", dis[A(s2, t2)]);
  return 0;
}

Error: -11

Fixed submission:

#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define ll long long
#define maxn 1000005
#define inf 1e9
#define pb push_back
#define rep(i, a, b) for (int i = a; i <= b; i++)
using namespace std;

inline int read() {
  int x = 0, w = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      w = -1;
    c = getchar();
  }
  while (c <= '9' && c >= '0') {
    x = (x << 1) + (x << 3) + c - '0';
    c = getchar();
  }
  return w == 1 ? x : -x;
}

int n, m, s, dis[maxn], s1, t1, s2, t2, N, M;
struct node {
  int v, w;
};
vector<node> mp[maxn];
struct node2 {
  int dis, to;
};
char p[1005][1005];

inline bool operator<(node2 a, node2 b) { return a.dis > b.dis; }

priority_queue<node2> que;
bool vis[maxn];

inline int A(int x, int y) { return M * (x - 1) + y; }
inline int Add(int u, int v, int w) { mp[u].pb({v, w}), mp[v].pb({u, w}); }

const int kx[4] = {0, 0, 1, -1};
const int ky[4] = {1, -1, 0, 0};

int main() {
  N = read(), M = read();
  s1 = read();
  t1 = read();
  s2 = read();
  t2 = read();
  rep(i, 1, N) scanf("%s", p[i] + 1);
  rep(i, 1, N) rep(j, 1, M) {
    if (p[i][j] == '.') {
      rep(k, 0, 3) {
        int tx = i + kx[k], ty = j + ky[k];
        if (tx <= 0 || tx > N || ty <= 0 || ty > M || p[tx][ty] == '#')
          continue;
        Add(A(i, j), A(tx, ty), 0);
      }

      rep(w, -2, 2) rep(k, -2, 2) {
        int tx = i + w, ty = j + k;
        if (tx <= 0 || tx > N || ty <= 0 || ty > M || p[tx][ty] == '#')
          continue;
        Add(A(i, j), A(tx, ty), 1);
      }
    }
  }

  n = N * M;
  s = A(s1, t1);
  for (int i = 1; i <= n; i++)
    dis[i] = inf;
  dis[s] = 0;
  que.push((node2){0, s});
  while (!que.empty()) {
    node2 u = que.top();
    que.pop();
    if (vis[u.to])
      continue;
    vis[u.to] = 1;
    int uu = u.to; // cout<<uu<<endl;
    for (int i = 0; i < mp[uu].size(); i++) {
      node v = mp[uu][i];
      if (vis[v.v])
        continue;
      if (dis[uu] + v.w < dis[v.v]) {
        dis[v.v] = dis[uu] + v.w;
        que.push((node2){dis[v.v], v.v});
      }
    }
  }
  printf("%d ", dis[A(s2, t2)]);
  return 0;
}