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

#line 1 "cplib/template.cpp"
#include <bits/stdc++.h>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()

template <class T> bool chmax(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmin(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  } else
    return false;
}

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vl = vector<ll>;
using Pi = pair<int, int>;
using Pl = pair<ll, ll>;
using vpi = vector<Pi>;
using vpl = vector<Pl>;

#line 1 "cplib/debug.cpp"
#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  cout << "{" << p.first << "," << p.second << "}" << '\\n';
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}
#line 1 "cplib/graph/template.cpp"
template <class T> struct Edge {
  int from, to;
  T cost;
  Edge(int from, int to, T cost) : from(from), to(to), cost(cost) {}
  Edge() {}
};
template <class T> struct Graph {
  int n;
  vector<vector<Edge<T>>> g;
  Graph(int n) : n(n) { g.resize(n); }
  void add_edge(int from, int to) { g[from].emplace_back(from, to, 1); }
  void add_edge(int from, int to, T cost) {
    g[from].emplace_back(from, to, cost);
  }
  vector<Edge<T>> &operator[](int i) { return g[i]; }
};
#line 2 "cplib/graph/dijkstra.cpp"
template <class T> struct Dijkstra {
#define INF (1LL << 55)
  using P = pair<T, int>;
  Graph<T> G;
  vector<T> d;
  vector<int> prev;
  Dijkstra(Graph<T> &G) : G(G) {}
  void solve(int s) {
    d.assign(G.n, INF);
    prev.assign(G.n, -1);
    d[s] = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(P(0, s));
    while (que.size()) {
      P p = que.top();
      que.pop();
      int v = p.second;
      if (d[v] < p.first)
        continue;
      for (int i = 0; i < G[v].size(); i++) {
        Edge<T> &e = G[v][i];
        if (d[e.to] > d[v] + e.cost) {
          d[e.to] = d[v] + e.cost;
          prev[e.to] = v;
          que.push(P(d[e.to], e.to));
        }
      }
    }
  }
  bool reachable(int t) { return d[t] != INF; }
  vector<int> get_path(int t) {
    vector<int> path;
    for (; t != -1; t = prev[t]) {
      path.push_back(t);
    }
    reverse(path.begin(), path.end());
    return path;
  }
  T &operator[](int i) { return d[i]; }
};
#line 3 "main.cpp"

int h, w;
int sx, sy, gx, gy;
string g[1000];

int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};

int main() {

  cin.tie(0);
  ios::sync_with_stdio(false);

  cin >> h >> w;
  cin >> sx >> sy >> gx >> gy;
  sx--, sy--, gx--, gy--;
  REP(i, h) cin >> g[i];

  auto id = [=](int x, int y) -> int { return x * w + y; };
  auto inside = [=](int x, int y) -> bool {
    return x >= 0 && x < h && y >= 0 && y < w;
  };

  Graph<ll> G(h * w);
  REP(i, h) REP(j, w) {
    if (g[i][j] == '#')
      continue;
    REP(d, 4) {
      int nx = i + dx[d];
      int ny = j + dy[d];
      if (inside(nx, ny) && g[nx][ny] == '.') {
        int from = id(i, j);
        int to = id(nx, ny);
        G.add_edge(from, to, 0);
      }
    }
    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        if (abs(dx) + abs(dy) > 1) {
          int nx = i + dx;
          int ny = j + dy;
          if (inside(nx, ny) && g[nx][ny] == '.') {
            int from = id(i, j);
            int to = id(nx, ny);
            G.add_edge(from, to, 1);
          }
        }
      }
    }
  }

  Dijkstra<ll> D(G);
  D.solve(id(sx, sy));
  if (!D.reachable(id(gx, gy))) {
    cout << -1 << '\\n';
  } else {
    cout << D[id(gx, gy)] << '\\n';
  }
  return 0;
}


Error: TLE

Fixed submission:

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

using ll = long long;
#define REP(i, n) for (int i = 0; i < n; i++)
#define mp make_pair
#define pb push_back
#define eb emplace_back
#define all(x) (x).begin(), (x).end()
#define debug(arr) cout << #arr << " = " << arr << '\\n'
#define debug2(a, b)                                                           \\
  cout << "[" << #a << "," << #b << "] = "                                     \\
       << "[" << a << "," << b << "]" << '\\n'

template <class T> bool chmin(T &a, T b) {
  if (b < a) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> bool chmax(T &a, T b) {
  if (b > a) {
    a = b;
    return true;
  } else
    return false;
}
template <class T> T max_element(T[], int);
template <class T> T min_element(T[], int);
template <class T> pair<T, int> lower_bound(T arr[], int n, T val);
template <class T> pair<T, int> upper_bound(T arr[], int n, T val);
template <class T> int get_range_cnt(vector<T> &a, T lb, T ub);
template <class T> vector<pair<T, int>> runlength(vector<T> arr);
vector<pair<char, int>> runlength(string s);
template <class T> ostream &operator<<(ostream &, const vector<T> &);
template <class T> ostream &operator<<(ostream &, const vector<vector<T>> &);
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &p);
template <class T> istream &operator>>(istream &, vector<T> &);

int h, w;
int cx, cy, gx, gy;
string g[1010];
int used[1010][1010];
int dist[1010][1010];
using P = pair<int, int>; // edge,cost
// vector<P> graph[]
void read() {
  cin >> h >> w;
  cin >> cx >> cy >> gx >> gy;
  cx--, cy--, gx--, gy--;
  REP(i, h) cin >> g[i];
}
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, -1, 0, 1};
bool inside(int x, int y) { return x >= 0 && x < h && y >= 0 && y < w; }
int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);
  // cout << fixed << setprecision(20);
  // int t;cin >> t; REP(_,t) main2();
  read();

  queue<P> q;
  q.emplace(cx, cy);
  // used[cx][cy]++;
  REP(i, 1010) REP(j, 1010) dist[i][j] = 1e9;
  dist[cx][cy] = 0;
  int cnt = 0;
  while (q.size()) {
    if (cnt++ > 1e7)
      break;
    auto [x, y] = q.front();
    // if(x == gx and y == gy){
    //   cout << dist[x][y] << '\\n';
    //   return 0;
    // }

    q.pop();

    REP(d, 4) {

      int nx = x + dx[d];
      int ny = y + dy[d];

      if (inside(nx, ny) && g[nx][ny] == '.' && (dist[nx][ny] > dist[x][y])) {
        dist[nx][ny] = dist[x][y];
        q.emplace(nx, ny);
        // used[nx][ny]++;
      }
    }

    for (int dx = -2; dx <= 2; dx++) {
      for (int dy = -2; dy <= 2; dy++) {
        if (abs(dx) + abs(dy) > 1) {

          int nx = x + dx;
          int ny = y + dy;

          if (inside(nx, ny) && g[nx][ny] == '.' &&
              (dist[nx][ny] > dist[x][y] + 1)) {
            dist[nx][ny] = dist[x][y] + 1;
            q.emplace(nx, ny);
            // used[nx][ny]++;
          }
        }
      }
    }
  }
  if (dist[gx][gy] == 1e9) {
    cout << -1 << '\\n';
  } else {
    cout << dist[gx][gy] << '\\n';
  }
  return 0;
}

/////////////////////////////////////////////////////////////////////////////////
// for debug & input
template <class T> ostream &operator<<(ostream &out, const vector<T> &arr) {
  cout << "{";
  for (int i = 0; i < arr.size(); i++)
    cout << (!i ? "" : ", ") << arr[i];
  cout << "}";
  return out;
}
template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &arr) {
  cout << "{\\n";
  for (auto &vec : arr)
    cout << "  " << vec << ",\\n";
  cout << "}";
  return out;
}
template <class S, class T>
ostream &operator<<(ostream &out, const pair<S, T> &p) {
  cout << "{" << p.first << "," << p.second << "}";
  return out;
}
template <class T> istream &operator>>(istream &in, vector<T> &arr) {
  for (auto &i : arr)
    cin >> i;
  return in;
}

// for arrays.
template <class T> T max_element(T arr[], int n) {
  T res = numeric_limits<T>::min();
  for (int i = 0; i < n; i++)
    res = max(res, arr[i]);
  return res;
}
template <class T> T min_element(T arr[], int n) {
  T res = numeric_limits<T>::max();
  for (int i = 0; i < n; i++)
    res = min(res, arr[i]);
  return res;
}
template <class T> pair<T, int> lower_bound(T arr[], int n, T val) {
  int l = 0, r = n;
  while (r - l) {
    int mid = (r + l) / 2;
    if (arr[mid] >= val)
      r = mid;
    else
      l = mid;
  }
  return make_pair(arr[r], r);
}
template <class T> pair<T, int> upper_bound(T arr[], int n, T val) {
  int l = 0, r = n;
  while (r - l) {
    int mid = (r + l) / 2;
    if (arr[mid] > val)
      r = mid;
    else
      l = mid;
  }
  return make_pair(arr[r], r);
}
// [lb,ub)の範囲の個数を数える
// last checked : 2020.5.8
template <class T> int get_range_cnt(vector<T> &a, T lb, T ub) {
  int n = a.size();
  int r = n - (int)(a.end() - (lower_bound(a.begin(), a.end(), ub)));
  int l = n - (int)(a.end() - (lower_bound(a.begin(), a.end(), lb)));
  return r - l;
}
template <class T> vector<pair<T, int>> runlength(vector<T> arr) {
  vector<pair<T, int>> res;
  int n = arr.size();
  T tmp = arr[0];
  int cont = 1;
  for (int i = 1; i < n; i++) {
    if (arr[i] != tmp) {
      res.push_back(make_pair(tmp, cont));
      tmp = arr[i], cont = 1;
    } else
      cont++;
  }
  res.push_back(make_pair(tmp, cont));
  return res;
}
vector<pair<char, int>> runlength(string s) {
  vector<char> arr(s.size());
  for (int i = 0; i < s.size(); i++)
    arr[i] = s[i];
  return runlength(arr);
}