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

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

ll MAX_H = 1e3;
ll MAX_W = 1e3;
vll a(MAX_H *MAX_W, -1);
vector<string> s(MAX_H);

ll h, w;

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  REP(i, h) cin >> s[i];

  auto valid = [&](ll x, ll y) {
    if (x < 0 || w <= x || y < 0 || h <= y)
      return false;
    return true;
  };

  vll d(h * w, LLINF);
  deque<vll> que;
  vll v = {cw, ch, 0};
  que.push_back(v);
  d[ch * w + cw] = 0;

  while (!que.empty()) {
    ll x = que.front()[0];
    ll y = que.front()[1];
    ll dist = que.front()[2];
    que.pop_front();

    REP(i, 4) {
      ll nx = x + dx[i];
      ll ny = y + dy[i];
      if (valid(nx, ny) && s[ny][nx] == '.' && d[ny * w + nx] > dist) {
        d[ny * w + nx] = dist;
        vll p = {nx, ny, dist};
        que.push_front(p);
      }
    }

    for (ll dxx = -2; dxx <= 2; dxx++) {
      for (ll dyy = -2; dyy <= 2; dyy++) {
        ll nx = x + dxx;
        ll ny = y + dyy;
        if (valid(nx, ny) && s[ny][nx] == '.' && d[ny * w + nx] > dist + 1) {
          d[ny * w + nx] = dist + 1;
          vll p = {nx, ny, dist + 1};
          que.push_back(p);
        }
      }
    }
  }

  //  REP(i,h){
  //    REP(j,w)cout<<d[i*w+j]<<" ";
  //    cout<<endl;
  //  }

  ll ans = d[dh * w + dw];
  if (ans == LLINF)
    ans = -1;
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
typedef vector<ll> vll;
typedef vector<ld> vld;
typedef vector<pll> vpl;
typedef vector<vll> vvll;

#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define FI first
#define SE second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define REP1(i, n) for (int i = 1; i < ((int)n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define PB push_back
#define EB emplace_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (lower_bound(c.begin(), c.end(), x) - c.begin())
#define yes cout << "Yes" << endl
#define YES cout << "YES" << endl
#define no cout << "No" << endl
#define NO cout << "NO" << endl
#define Decimal fixed << setprecision(20)
#define INF 1000000000
#define LLINF 1000000000000000000LL

const int inf = 1e9;
const ll linf = 1LL << 50;
const double eps = 1e-10;
const int MOD = 1e9 + 7;
const int dx[4] = {-1, 0, 1, 0};
const int dy[4] = {0, -1, 0, 1};

ll MAX_H = 1e3;
ll MAX_W = 1e3;
vll a(MAX_H *MAX_W, -1);
vector<string> s(MAX_H);

ll h, w;

bool dfs(ll x, ll y, ll sz) {
  if (s[y][x] == '#')
    return false;
  if (a[y * w + x] != -1)
    return false;

  a[y * w + x] = sz;

  REP(i, 4) {
    ll nx = x + dx[i];
    ll ny = y + dy[i];
    if (nx < 0 || w <= nx || ny < 0 || h <= ny)
      continue;

    dfs(nx, ny, sz);
  }

  return true;
}

// O(|E|log|V|)
struct Dijkstra {
  struct edge {
    ll to;
    ll cost;

    edge(ll x, ll y) : to(x), cost(y) {}
  };

  ll n;
  vector<vector<edge>> graph;
  vll dist;
  Dijkstra(ll n) : n(n), graph(n), dist(n, LLINF) {}

  void addEdge(ll from, ll to, ll cost) {
    graph[from].push_back(edge(to, cost));
  }

  void initiarizeDist() { dist = vll(n, LLINF); }

  void calc(ll start) {
    priority_queue<pll> que;
    que.push(pll(0, start));

    while (!que.empty()) {
      pll p = que.top();
      que.pop();

      ll d = p.FI;
      ll now = p.SE;

      if (dist[now] < d)
        continue;

      for (auto e : graph[now]) {
        if (dist[e.to] > d + e.cost) {
          dist[e.to] = d + e.cost;
          que.push(pll(dist[e.to], e.to));
        }
      }
    }
  }

  ll getDist(ll x) { return dist[x]; }
};

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(NULL);

  cin >> h >> w;
  ll ch, cw, dh, dw;
  cin >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  REP(i, h) cin >> s[i];

  ll sz = 0;
  REP(y, h) {
    REP(x, w) {
      bool res = dfs(x, y, sz);
      if (res)
        sz++;
    }
  }

  //  REP(y,h){
  //    REP(x,w)cout<<a[y*w+x]<<" ";
  //    cout<<endl;
  //  }

  if (sz == 0)
    cout << -1 << endl;

  Dijkstra di(sz);
  map<pll, ll> ex;

  REP(y, h) {
    REP(x, w) {
      if (a[y * w + x] == -1)
        continue;

      for (ll dxx = -2; dxx <= 2; dxx++) {
        for (ll dyy = -2; dyy <= 2; dyy++) {
          ll nx = x + dxx;
          ll ny = y + dyy;
          if (nx < 0 || w <= nx || ny < 0 || h <= ny)
            continue;

          if (a[ny * w + nx] == -1)
            continue;

          if (a[y * w + x] != a[ny * w + nx]) {
            ll to = a[y * w + x];
            ll from = a[ny * w + nx];
            if (to > from)
              swap(to, from);

            if (ex[pll(to, from)] == 0) {
              di.addEdge(to, from, 1);
              di.addEdge(from, to, 1);
              ex[pll(to, from)] = 1;
            }
          }
        }
      }
    }
  }

  // REP(k,sz)REP(i,sz)REP(j,sz)dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
  ll from = a[ch * w + cw];
  ll to = a[dh * w + dw];

  di.calc(from);
  ll ans = di.getDist(to);
  if (ans == LLINF)
    ans = -1;
  if (from == to)
    ans = 0;
  cout << ans << endl;
}