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

const int isDebugMode = 1;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge {
  long long to, cost;
};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e)                                                      \\
  (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e)                                                      \\
  (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
void pt() {
  cout << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head;
  pt();
  return;
}
template <class Head, class... Tail> void pt(Head &&head, Tail &&...tail) {
  cout << head << " ";
  pt(forward<Tail>(tail)...);
}
void dpt() {
  if (!isDebugMode)
    return;
  cout << endl;
  return;
}
template <class Head> void dpt(Head &&head) {
  if (!isDebugMode)
    return;
  cout << head;
  pt();
  return;
}
template <class Head, class... Tail> void dpt(Head &&head, Tail &&...tail) {
  if (!isDebugMode)
    return;
  cout << head << " ";
  pt(forward<Tail>(tail)...);
}
template <class T> void debug(T v) {
  if (!isDebugMode)
    return;
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
template <class T> void debug2(T v) {
  if (!isDebugMode)
    return;
  rep(i, v.size()) {
    rep(j, v[i].size()) cout << v[i][j] << " ";
    cout << endl;
  }
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> long long recgcd(T1 a, T2 b) {
  return a % b ? recgcd(b, a % b) : b;
}
bool valid(long long H, long long W, long long h, long long w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}

struct Graph {
public:
  ll V, logV;
  vector<vector<edge>> Glist;
  vector<vector<ll>> Gmatrix;
  vector<ll> LCA_dist;
  vector<vector<ll>> LCA_doubling;

  Graph(ll NodeNum) {
    V = NodeNum;
    Glist.resize(V);
  }
  void add(ll from, ll to, ll cost = 1) { Glist[from].push_back({to, cost}); }
  void bidadd(ll from, ll to, ll cost = 1) {
    Glist[from].push_back({to, cost});
    Glist[to].push_back({from, cost});
  }

  void express() {
    for (ll i = 0; i < Glist.size(); i++) {
      cout << i << " => {";
      eachdo(Glist[i], e) { cout << " " << e.to; }
      cout << " }" << endl;
    }
    return;
  }

  void UpdateGmatrix() {
    Gmatrix.resize(V);
    rep(i, V) {
      Gmatrix[i].resize(V, INF);
      eachdo(Glist[i], e) { Gmatrix[i][e.to] = e.cost; }
    }
  }

  vector<vector<ll>> WarshallFloyd() {
    UpdateGmatrix();
    vector<vector<ll>> dist = Gmatrix;
    rep(i, V) dist[i][i] = 0;
    rep(k, V) rep(i, V) rep(j, V) chmin(dist[i][j], dist[i][k] + dist[k][j]);
    return dist;
  }

  vector<bool> hasNegativeLoop() {
    vector<bool> ret(V, false);
    vector<ll> dist(V, 0);
    rep(loop, 2 * V - 1) {
      rep(i, V) eachdo(Glist[i], e) {
        if (dist[i] + e.cost < dist[e.to]) {
          dist[e.to] = dist[i] + e.cost;
          if (V - 1 <= loop)
            ret[e.to] = true;
        }
      }
    }
    return ret;
  }

  vector<ll> BellmanFord(ll start) {
    vector<ll> dist(V, INF);
    dist[start] = 0;
    rep(loop, V - 1) {
      rep(i, V) eachdo(Glist[i], e) {
        if (dist[i] + e.cost < dist[e.to]) {
          dist[e.to] = dist[i] + e.cost;
        }
      }
    }
    return dist;
  }

  vector<ll> Dijkstra(ll start) {
    vector<ll> dist(V, INF);
    priority_queue<P, vector<P>, greater<P>> que;
    dist[start] = 0;
    que.push(P(0, start));
    while (!que.empty()) {
      P p = que.top();
      que.pop();
      ll v = p.second;
      if (dist[v] < p.first)
        continue;
      rep(i, Glist[v].size()) {
        edge e = Glist[v][i];
        if (dist[v] + e.cost < dist[e.to]) {
          dist[e.to] = dist[v] + e.cost;
          que.push(P(dist[e.to], e.to));
        }
      }
    }
    return dist;
  }

  // Graph needs to be undirected tree.
  void dfs_in_build_LCA(ll node, ll parent = -1) {
    if (parent != -1)
      LCA_dist[node] = LCA_dist[parent] + 1;
    LCA_doubling[0][node] = parent;
    for (auto e : Glist[node]) {
      if (e.to == parent)
        continue;
      dfs_in_build_LCA(e.to, node);
    }
  }

  void build_LCA(ll root) {
    LCA_dist.assign(V, INF);
    logV = 1;
    while (1 << logV < V)
      logV++;
    LCA_dist.assign(V, INF);
    LCA_doubling.assign(logV, vector<ll>(V, -1));
    LCA_dist[root] = 0;
    dfs_in_build_LCA(root);
    for (ll i = 1; i < logV; i++) {
      for (ll j = 0; j < V; j++) {
        if (LCA_doubling[i - 1][j] == -1) {
          LCA_doubling[i][j] = -1;
          continue;
        }
        LCA_doubling[i][j] = LCA_doubling[i - 1][LCA_doubling[i - 1][j]];
      }
    }
  }

  ll LCA_query(ll u, ll v) {
    if (LCA_dist[v] < LCA_dist[u])
      swap(u, v);
    ll upstream = LCA_dist[v] - LCA_dist[u];
    for (ll i = 0; i < logV; i++) {
      if (upstream >> i & 1)
        v = LCA_doubling[i][v];
    }
    ll ng = -1, ok = V;
    while (ok - ng > 1) {
      ll half = (ng + ok) / 2;
      ll ua = u, va = v;
      for (ll i = 0; i < logV; i++) {
        if (half >> i & 1) {
          if (ua == -1 || va == -1) {
            ua = -1;
            va = -1;
            break;
          }
          ua = LCA_doubling[i][ua];
          va = LCA_doubling[i][va];
        }
      }

      if (ua == va)
        ok = half;
      else
        ng = half;
    }
    ll ret = u;
    for (ll i = 0; i < logV; i++) {
      if (ok >> i & 1)
        ret = LCA_doubling[i][ret];
    }
    return ret;
  }

  ll path_dist(ll u, ll v) {
    return LCA_dist[u] + LCA_dist[v] - 2 * LCA_dist[LCA_query(u, v)];
  }

  bool is_on_path(ll u, ll v, ll x) {
    return path_dist(u, x) + path_dist(x, v) == path_dist(u, v);
  }
};

ll H, W;

ll has(ll h, ll w) { return h * W + w; }
int main() {
  cin >> H >> W;
  ll ch, cw;
  cin >> ch >> cw;
  ll dh, dw;
  cin >> dh >> dw;
  vector<string> S(H);
  rep(i, H) cin >> S[i];
  --ch;
  --cw;
  --dh;
  --dw;
  Graph g(H * W);
  rep(i, H) rep(j, W) {
    if (S[i][j] == '#')
      continue;
    rep(k, 4) {
      if (valid(H, W, i + dx[k], j + dy[k])) {
        if (S[i + dx[k]][j + dy[k]] == '.') {
          g.add(has(i, j), has(i + dx[k], j + dy[k]), 0);
        }
      }
    }

    for (ll x = -2; x <= 2; x++)
      for (ll y = -2; y <= 2; y++) {
        if (valid(H, W, i + x, j + y)) {
          if (S[x + i][y + j] == '.') {
            g.add(has(i, j), has(i + x, j + y), 1);
          }
        }
      }
  }
  vector<ll> dist = g.Dijkstra(has(ch, cw));
  // debug(dist);
  pt(dist[has(dh, dw)] == INF ? -1 : dist[has(dh, dw)]);
  return 0;
}

Error: TLE

Fixed submission:

const int isDebugMode = 1;
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<long long, long long> P;
struct edge {
  long long to, cost;
};
const int inf = 1 << 27;
const long long INF = 1LL << 60;
const int COMBMAX = 1001001;
const long long MOD = 1000000007;
const long long dy[] = {-1, 0, 0, 1};
const long long dx[] = {0, -1, 1, 0};
string abc = "abcdefghijklmnopqrstuvwxyz";
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define eachdo(v, e) for (const auto &e : (v))
#define all(v) (v).begin(), (v).end()
#define lower_index(v, e)                                                      \\
  (long long)distance((v).begin(), lower_bound((v).begin(), (v).end(), e))
#define upper_index(v, e)                                                      \\
  (long long)distance((v).begin(), upper_bound((v).begin(), (v).end(), e))
long long mpow(long long a, long long n, long long mod = MOD) {
  long long res = 1;
  while (n > 0) {
    if (n & 1)
      res = res * a % mod;
    a = a * a % mod;
    n >>= 1;
  }
  return res;
}
void pt() {
  cout << endl;
  return;
}
template <class Head> void pt(Head &&head) {
  cout << head;
  pt();
  return;
}
template <class Head, class... Tail> void pt(Head &&head, Tail &&...tail) {
  cout << head << " ";
  pt(forward<Tail>(tail)...);
}
void dpt() {
  if (!isDebugMode)
    return;
  cout << endl;
  return;
}
template <class Head> void dpt(Head &&head) {
  if (!isDebugMode)
    return;
  cout << head;
  pt();
  return;
}
template <class Head, class... Tail> void dpt(Head &&head, Tail &&...tail) {
  if (!isDebugMode)
    return;
  cout << head << " ";
  pt(forward<Tail>(tail)...);
}
template <class T> void debug(T v) {
  if (!isDebugMode)
    return;
  rep(i, v.size()) cout << v[i] << " ";
  cout << endl;
}
template <class T> void debug2(T v) {
  if (!isDebugMode)
    return;
  rep(i, v.size()) {
    rep(j, v[i].size()) cout << v[i][j] << " ";
    cout << endl;
  }
}
template <class T1, class T2> inline bool chmin(T1 &a, T2 b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> inline bool chmax(T1 &a, T2 b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T1, class T2> long long recgcd(T1 a, T2 b) {
  return a % b ? recgcd(b, a % b) : b;
}
bool valid(long long H, long long W, long long h, long long w) {
  return 0 <= h && h < H && 0 <= w && w < W;
}

int main() {
  ll H, W;
  cin >> H >> W;
  vector<vector<ll>> dp(H, vector<ll>(W, INF));
  ll ch, cw;
  cin >> ch >> cw;
  ll dh, dw;
  cin >> dh >> dw;
  vector<string> S(H);
  rep(i, H) cin >> S[i];
  --ch;
  --cw;
  --dh;
  --dw;
  stack<pair<P, ll>> que;
  que.push(make_pair(P(ch, cw), 0));
  while (!que.empty()) {
    pair<P, ll> node = que.top();
    que.pop();
    ll h = node.first.first;
    ll w = node.first.second;
    ll dist = node.second;
    if (dp[h][w] <= dist || S[h][w] == '#')
      continue;
    dp[h][w] = dist;
    if (valid(H, W, h - 1, w) && S[h - 1][w] == '.')
      que.push(make_pair(P(h - 1, w), dist));
    if (valid(H, W, h + 1, w) && S[h + 1][w] == '.')
      que.push(make_pair(P(h + 1, w), dist));
    if (valid(H, W, h, w - 1) && S[h][w - 1] == '.')
      que.push(make_pair(P(h, w - 1), dist));
    if (valid(H, W, h, w + 1) && S[h][w + 1] == '.')
      que.push(make_pair(P(h, w + 1), dist));
    for (ll i = -2; i <= 2; i++)
      for (ll j = -2; j <= 2; j++) {

        if ((i == 0 && j == 0) || !valid(H, W, h + i, w + j))
          continue;
        que.push(make_pair(P(h + i, w + j), dist + 1));
      }
  }
  pt(dp[dh][dw] == INF ? -1 : dp[dh][dw]);
}