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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e3 + 5;
char buff[maxn][maxn];
int dp[maxn][maxn];
int tag[maxn][maxn];
int cnt = 0;
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void bfs(int y, int x) {
  queue<pii> Q;
  tag[y][x] = ++cnt;
  Q.push({y, x});
  while (!Q.empty()) {
    auto tem = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = tem.second + dx[i], ny = tem.first + dy[i];
      if (nx >= 1 && nx <= m && ny >= 1 && ny <= n && buff[ny][nx] == '.' &&
          !tag[ny][nx]) {
        tag[ny][nx] = cnt;
        Q.push({ny, nx});
      }
    }
  }
}

const int MAXN = 1000005;
const int MAXM = 5000005;
struct EDGE {
  int nex, to;
  ll w;
} edge[MAXM];
int ecnt, head[MAXN];
inline void addedge(int u, int v, ll weight) {
  edge[++ecnt].nex = head[u];
  head[u] = ecnt;
  edge[ecnt].to = v;
  edge[ecnt].w = weight;
}
ll dis[MAXN];
bool spsa(int s) {
  queue<int> Q;
  Q.push(s);
  while (!Q.empty()) {
    int u = Q.front();
    Q.pop();
    for (int i = head[u]; i; i = edge[i].nex) {
      int v = edge[i].to;
      if (dis[v])
        continue;
      dis[v] = dis[u] + 1;
      Q.push(v);
    }
  }
  return 0;
}

map<pii, int> M;
int main() {
  cin >> n >> m;
  int sy, sx, dy, dx;
  cin >> sy >> sx >> dy >> dx;
  for (int i = 1; i <= n; i++)
    cin >> (buff[i] + 1);

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!tag[i][j] && buff[i][j] == '.')
        bfs(i, j);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (tag[i][j])
        for (int a = -2; a <= 2; a++) {
          for (int b = -2; b <= 2; b++) {
            int ny = i + a, nx = j + b;
            if (ny >= 1 && ny <= n && nx >= 1 && nx <= m && tag[ny][nx] &&
                tag[i][j] != tag[ny][nx]) {
              addedge(tag[ny][nx], tag[i][j], 1);
              addedge(tag[i][j], tag[ny][nx], 1);
            }
          }
        }
    }
  }

  spsa(tag[sy][sx]);
  if (dis[tag[dy][dx]] == 0 && tag[dy][dx] != tag[sy][sx])
    cout << -1 << endl;
  else
    cout << dis[tag[dy][dx]] << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
const int maxn = 1e3 + 5;
char buff[maxn][maxn];
int dp[maxn][maxn];
int tag[maxn][maxn];
int cnt = 0;
int n, m;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
void bfs(int y, int x) {
  queue<pii> Q;
  tag[y][x] = ++cnt;
  Q.push({y, x});
  while (!Q.empty()) {
    auto tem = Q.front();
    Q.pop();
    for (int i = 0; i < 4; i++) {
      int nx = tem.second + dx[i], ny = tem.first + dy[i];
      if (nx >= 1 && nx <= m && ny >= 1 && ny <= n && buff[ny][nx] == '.' &&
          !tag[ny][nx]) {
        tag[ny][nx] = cnt;
        Q.push({ny, nx});
      }
    }
  }
}

const int MAXN = 1000005;
const int MAXM = 5000005;
struct EDGE {
  int nex, to;
  ll w;
} edge[MAXM];
int ecnt, head[MAXN];
inline void addedge(int u, int v, ll weight) {
  edge[++ecnt].nex = head[u];
  head[u] = ecnt;
  edge[ecnt].to = v;
  edge[ecnt].w = weight;
}
ll s, dis[MAXN], u, v, w;
bool inqueue[MAXN];
bool spsa(int s) {
  deque<int> q;
  memset(inqueue, 0, sizeof inqueue);
  memset(dis, 63, sizeof dis);
  q.push_front(s);
  inqueue[s] = 1;
  dis[s] = 0;
  while (!q.empty()) {
    int now = q.front();
    q.pop_front();
    inqueue[now] = 0;
    for (int i = head[now]; i; i = edge[i].nex) {
      int y = edge[i].to;
      if (dis[now] + edge[i].w < dis[y]) {
        dis[y] = dis[now] + edge[i].w;
        if (!inqueue[y]) {
          inqueue[y] = 1;
          if (dis[y] < q.front())
            q.push_front(y);
          else
            q.push_back(y);
        }
      }
    }
  }
  return 0;
}

map<pii, int> M;
int main() {
  cin >> n >> m;
  int sy, sx, dy, dx;
  cin >> sy >> sx >> dy >> dx;
  for (int i = 1; i <= n; i++)
    cin >> (buff[i] + 1);

  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= m; j++)
      if (!tag[i][j] && buff[i][j] == '.')
        bfs(i, j);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= m; j++) {
      if (tag[i][j])
        for (int a = -2; a <= 2; a++) {
          for (int b = -2; b <= 2; b++) {
            int ny = i + a, nx = j + b;
            if (ny >= 1 && ny <= n && nx >= 1 && nx <= m && tag[ny][nx] &&
                tag[i][j] != tag[ny][nx] && !M[{tag[ny][nx], tag[i][j]}]) {
              addedge(tag[ny][nx], tag[i][j], 1);
              addedge(tag[i][j], tag[ny][nx], 1);
              M[{tag[i][j], tag[ny][nx]}] = 1;
              M[{tag[ny][nx], tag[i][j]}] = 1;
            }
          }
        }
    }
  }

  spsa(tag[sy][sx]);
  if (dis[tag[dy][dx]] > 1e9)
    cout << -1 << endl;
  else
    cout << dis[tag[dy][dx]] << endl;

  return 0;
}