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

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
struct dijkstra {
  struct edge {
    int to;
    long long cost;
  };
  typedef pair<long long, long long> P;
  vector<vector<edge>> g;
  vector<long long> d, sub;
  priority_queue<P, vector<P>, greater<P>> que;
  dijkstra(int n) : g(n), d(n, 1e18 + 7), sub(n, -1) {}
  void add_edge(int from, int to, long long cost) {
    g[from].push_back({to, cost});
  }
  vector<long long> dist(int s) {
    d[s] = 0;
    que.push({0, s});
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first) {
        continue;
      } else {
        for (edge &e : g[v]) {
          if (d[e.to] > d[v] + e.cost) {
            d[e.to] = d[v] + e.cost;
            sub[e.to] = v;
            que.push({d[e.to], e.to});
          }
        }
      }
    }
    return d;
  }
};
int main() {
  int h, w;
  int sx, sy;
  int gx, gy;
  char m[1000][1000];
  bool used[1000][1000] = {};
  cin >> h >> w;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      cin >> m[i][j];
    }
  }
  dijkstra di(h * w);
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      if (m[i][j] == '#') {
        continue;
      }
      used[i][j] = 1;
      for (int a = -2; a <= 2; a++) {
        for (int b = -2; b <= 2; b++) {
          if (a == 0 && b == 0) {
            continue;
          }
          if (0 <= i + a && i + a < h && 0 <= j + b && j + b < w &&
              m[i + a][j + b] == '.') {
            if (!used[i + a][j + b]) {
              di.add_edge(i * w + j, (i + a) * w + (j + b),
                          (abs(a) + abs(b) <= 1 ? 1 : 2000000));
              di.add_edge((i + a) * w + (j + b), i * w + j,
                          (abs(a) + abs(b) <= 1 ? 1 : 2000000));
            }
          }
        }
      }
    }
  }
  vector<long long> ans = di.dist(sx * w + sy);
  cout << (ans[gx * w + gy] == 1e18 + 7 ? -1 : ans[gx * w + gy] / 2000000)
       << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;
long long mod = 998244353;
int main() {
  int h, w;
  int sx, sy;
  int gx, gy;
  char m[1000][1000];
  int d[1000][1000];
  cin >> h >> w;
  cin >> sx >> sy >> gx >> gy;
  sx--;
  sy--;
  gx--;
  gy--;
  for (int i = 0; i < h; i++) {
    for (int j = 0; j < w; j++) {
      d[i][j] = 1e9 + 7;
      cin >> m[i][j];
    }
  }
  d[sx][sy] = 0;
  queue<pair<pair<int, int>, int>> que;
  que.push(make_pair(make_pair(sx, sy), 0));
  int dx[4] = {-1, 0, 1, 0};
  int dy[4] = {0, -1, 0, 1};
  int cnt = 0;
  while (!que.empty()) {
    queue<pair<int, int>> mem;
    mem.push(make_pair(sx, sy));
    while (!que.empty()) {
      pair<pair<int, int>, int> hoge = que.front();
      que.pop();
      int nx = hoge.first.first;
      int ny = hoge.first.second;
      int nd = hoge.second;
      for (int i = 0; i < 4; i++) {
        int mx = nx + dx[i];
        int my = ny + dy[i];
        if (gx == mx && gy == my) {
          cout << cnt << endl;
          return 0;
        } else if (0 <= mx && mx < h && 0 <= my && my < w &&
                   d[mx][my] == 1e9 + 7 && m[mx][my] == '.') {
          d[mx][my] = nd + 1;
          que.push(make_pair(make_pair(mx, my), nd + 1));
        }
        if (0 <= mx && mx < h && 0 <= my && my < w) {
          if (m[mx][my] == '#') {
            mem.push(make_pair(nx, ny));
          }
        }
      }
    }
    while (!mem.empty()) {
      pair<int, int> hoge = mem.front();
      mem.pop();
      int nx = hoge.first;
      int ny = hoge.second;
      for (int i = -2; i <= 2; i++) {
        for (int j = -2; j <= 2; j++) {
          if (abs(i) + abs(j) <= 1) {
            continue;
          }
          int mx = nx + i;
          int my = ny + j;
          if (0 <= mx && mx < h && 0 <= my && my < w && m[mx][my] == '.' &&
              d[mx][my] == 1e9 + 7) {
            if (mx == gx && my == gy) {
              cout << cnt + 1 << endl;
              return 0;
            }
            que.push(make_pair(make_pair(mx, my), 0));
          }
        }
      }
    }
    cnt++;
  }
  cout << -1 << endl;
  return 0;
}