Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define RG register
#define maxn 1000005
#define maxm 205
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, m, k, sum;
char s[1005][1005];
int vis[1005][1005], cnt, X[4] = {0, 0, 1, -1}, Y[4] = {1, -1, 0, 0};
bool isb[1005][1005];
queue<pair<int, int>> Q;
void dfs(int x, int y) {
  int i, xx, yy;
  vis[x][y] = cnt;
  for (i = 0; i < 4; i++) {
    xx = x + X[i], yy = y + Y[i];
    if (xx > n || xx < 1)
      continue;
    if (yy > m || yy < 1)
      continue;
    if (s[xx][yy] == '#')
      continue;
    if (vis[xx][yy])
      continue;
    dfs(xx, yy);
  }
}
int inde[2000005], tot;
struct p {
  int u, v, w, nxt;
} e[20000005];
map<pair<int, int>, int> mp;
void add(int u, int v, int w) {
  e[++tot] = (p){u, v, w, inde[u]}, inde[u] = tot;
}
void ins(int x, int y) {
  int u, v;
  for (int i = 0; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int xx = x + i, yy = y + j;
      if (xx > n || xx < 1)
        continue;
      if (yy > m || yy < 1)
        continue;
      if (s[xx][yy] == '#')
        continue;
      if (vis[x][y] == vis[xx][yy])
        continue;
      u = vis[x][y], v = vis[xx][yy];
      add(u, v, 1), add(v, u, 1);
    }
  }
}
bool inst[2000005];
int dis[2000005], cx, cy, dx, dy;
struct node {
  int w, now;
  inline bool operator<(const node &x) const { return w > x.w; }
};
priority_queue<node> que;
void dij(int u) {
  for (int i = 1; i <= cnt; i++)
    dis[i] = 0x3f3f3f3f;
  dis[u] = 0;
  int i, v;
  que.push(node{0, u});
  while (!que.empty()) {
    node x = que.top();
    que.pop();
    u = x.now;
    if (inst[u])
      continue;
    if (u == vis[dx][dy])
      break;
    inst[u] = 1;
    for (i = inde[u]; i; i = e[i].nxt) {
      v = e[i].v;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        que.push(node{dis[v], v});
      }
    }
  }
}
int main() {
  int i, j, x;
  n = read(), m = read();
  cx = read(), cy = read(), dx = read(), dy = read();
  for (i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      if (i == 1 || i == n || j == 1 || j == m || s[i - 1][j] == '#' ||
          s[i][j - 1] == '#' || s[i][j + 1] == '#' || s[i + 1][j] == '#')
        isb[i][j] = 1;
      if (vis[i][j])
        continue;
      cnt++;
      dfs(i, j);
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      if (isb[i][j])
        ins(i, j);
    }
  }
  dij(vis[cx][cy]);
  if (dis[vis[dx][dy]] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\\n", dis[vis[dx][dy]]);
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define inf 0x7fffffff
#define RG register
#define maxn 1000005
#define maxm 205
using namespace std;
typedef long long ll;
const ll mod = 1e9 + 7;
inline ll read() {
  ll x = 0, f = 1;
  char ch = getchar();
  while (ch > '9' || ch < '0') {
    if (ch == '-')
      f = -1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = x * 10 + ch - '0';
    ch = getchar();
  }
  return x * f;
}

int n, m, k, sum;
char s[1005][1005];
int vis[1005][1005], cnt, X[4] = {0, 0, 1, -1}, Y[4] = {1, -1, 0, 0};
bool isb[1005][1005];
queue<pair<int, int>> Q;
void bfs(int x, int y) {
  int i, xx, yy;
  Q.push({x, y});
  while (!Q.empty()) {
    x = Q.front().first, y = Q.front().second;
    Q.pop();
    vis[x][y] = cnt;
    for (i = 0; i < 4; i++) {
      xx = x + X[i], yy = y + Y[i];
      if (xx > n || xx < 1)
        continue;
      if (yy > m || yy < 1)
        continue;
      if (vis[xx][yy])
        continue;
      if (s[xx][yy] == '#')
        continue;
      Q.push({xx, yy});
    }
  }
}
int inde[2000005], tot;
struct p {
  int u, v, w, nxt;
} e[20000005];
map<pair<int, int>, int> mp;
void add(int u, int v, int w) {
  e[++tot] = (p){u, v, w, inde[u]}, inde[u] = tot;
}
void ins(int x, int y) {
  int u, v;
  for (int i = 0; i <= 2; i++) {
    for (int j = -2; j <= 2; j++) {
      int xx = x + i, yy = y + j;
      if (xx > n || xx < 1)
        continue;
      if (yy > m || yy < 1)
        continue;
      if (s[xx][yy] == '#')
        continue;
      if (vis[x][y] == vis[xx][yy])
        continue;
      u = vis[x][y], v = vis[xx][yy];
      add(u, v, 1), add(v, u, 1);
    }
  }
}
bool inst[2000005];
int dis[2000005], cx, cy, dx, dy;
struct node {
  int w, now;
  inline bool operator<(const node &x) const { return w > x.w; }
};
priority_queue<node> que;
void dij(int u) {
  for (int i = 1; i <= cnt; i++)
    dis[i] = 0x3f3f3f3f;
  dis[u] = 0;
  int i, v;
  que.push(node{0, u});
  while (!que.empty()) {
    node x = que.top();
    que.pop();
    u = x.now;
    if (inst[u])
      continue;
    if (u == vis[dx][dy])
      break;
    inst[u] = 1;
    for (i = inde[u]; i; i = e[i].nxt) {
      v = e[i].v;
      if (dis[v] > dis[u] + e[i].w) {
        dis[v] = dis[u] + e[i].w;
        que.push(node{dis[v], v});
      }
    }
  }
}
int main() {
  int i, j, x;
  n = read(), m = read();
  cx = read(), cy = read(), dx = read(), dy = read();
  for (i = 1; i <= n; i++)
    scanf("%s", s[i] + 1);
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      if (i == 1 || i == n || j == 1 || j == m || s[i - 1][j] == '#' ||
          s[i][j - 1] == '#' || s[i][j + 1] == '#' || s[i + 1][j] == '#')
        isb[i][j] = 1;
      if (vis[i][j])
        continue;
      cnt++;
      bfs(i, j);
    }
  }
  for (i = 1; i <= n; i++) {
    for (j = 1; j <= m; j++) {
      if (s[i][j] == '#')
        continue;
      if (isb[i][j])
        ins(i, j);
    }
  }
  dij(vis[cx][cy]);
  if (dis[vis[dx][dy]] == 0x3f3f3f3f)
    puts("-1");
  else
    printf("%d\\n", dis[vis[dx][dy]]);
  return 0;
}