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

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using piii = pair<int, pii>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int N;
vector<int> P;
// vector<string> S;
void input() {
  fast_io();
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    P.push_back(p);
  }
}

int dist(int i, int j) {
  int ret = N;
  ret = min(ret, i);
  ret = min(ret, N - 1 - i);
  ret = min(ret, j);
  ret = min(ret, N - 1 - j);
  return ret;
}

int dist_2(int nx, int ny, int x, int y) { return abs(nx - x) + abs(ny - y); }

vector<vector<bool>> seen;
vector<vector<int>> bfs(int const &x, int const &y, vector<vector<int>> D) {
  queue<piii> q;
  q.emplace(0, make_pair(x, y));
  while (!q.empty()) {
    int d = q.front().first;
    int cx = q.front().second.first;
    int cy = q.front().second.second;
    q.pop();
    for (int k = 0; k < 4; k++) {
      int nx = cx + dx[k];
      int ny = cy + dy[k];
      if (nx == x && ny == y)
        continue;
      if (nx < 0 || nx >= N)
        continue;
      if (ny < 0 || ny >= N)
        continue;
      // int nd = dist_2(nx,ny,x,y)-1;
      int nd;
      if (seen[nx][ny])
        nd = d;
      else
        nd = d + 1;
      if (D[nx][ny] > (D[cx][cy] + nd - 1)) {
        D[nx][ny] = max(D[cx][cy] + nd - 1, 0);
        q.emplace(nd, make_pair(nx, ny));
      }
    }
  }
  return D;
}

vector<vector<int>> D;
void dfs(int x, int y) {
  int d = D[x][y] + !seen[x][y];
  for (int k = 0; k < 4; k++) {
    int nx = x + dx[k];
    int ny = y + dy[k];
    if (nx < 0 || nx >= N)
      continue;
    if (ny < 0 || ny >= N)
      continue;

    if (D[nx][ny] > d) {
      D[nx][ny] = d;
      dfs(nx, ny);
    }
  }
}

int solve() {
  D.assign(N, vector<int>(N, 0));
  seen.assign(N, vector<bool>(N, false));
  for (int i = 0; i < N; i++)
    for (int j = 0; j < N; j++)
      D[i][j] = dist(i, j);
  ll ans = 0;
  for (int i = 0; i < N * N; i++) {
    int x = P[i] / N;
    int y = P[i] % N;
    ans += D[x][y];
    seen[x][y] = true;
    dfs(x, y);
    // for(auto d:D) cout << d << endl;
  }
  cout << ans << endl;
  return 0;
}

int main() {
  input();
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#ifdef __DEBUG__
#define DBG(X) cout << #X << " = " << (X) << endl;
#define SAY(X) cout << (X) << endl;
#else
#define DBG(X)
#define SAY(X)
#endif

using namespace std;

using ll = long long int;
using ull = unsigned long long int;
using ld = long double;
using pii = pair<int, int>;
using Piii = pair<int, pii>;
using pll = pair<ll, ll>;

int dx[] = {1, 0, -1, 0, 1, -1, 1, -1};
int dy[] = {0, 1, 0, -1, 1, 1, -1, -1};
const int INT_INF = (int)(2e9);
const ll LL_INF = (ll)(2e18);

inline void fast_io() {
  ios_base::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
};
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const pair<T, S> p) {
  cout << "[" << p.first << ";" << p.second << "]";
  return os;
}
template <typename T, typename S>
inline ostream &operator<<(ostream &os, const map<T, S> p) {
  for (auto el : p)
    cout << "[" << el.first << ";" << el.second << "]";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const vector<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T>
inline ostream &operator<<(ostream &os, const deque<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline ostream &operator<<(ostream &os, const set<T> &v) {
  for (auto el : v)
    cout << el << " ";
  return os;
}
template <typename T> inline vector<T> fetch_vec(int sz) {
  vector<T> ret(sz);
  for (auto &elem : ret)
    cin >> elem;
  return ret;
}

int N;
vector<int> P;
// vector<string> S;
void input() {
  fast_io();
  cin >> N;
  for (int i = 0; i < N * N; i++) {
    int p;
    cin >> p;
    p--;
    P.push_back(p);
  }
}

int dijkstra(int x, int y, vector<string> s) {
  auto cmp = [](Piii a, Piii b) { return a.first > b.first; };
  priority_queue<Piii, vector<Piii>, decltype(cmp)> pq(cmp);
  vector<vector<int>> dist(N, vector<int>(N, 1000));
  dist[x][y] = 0;
  pq.emplace(0, make_pair(x, y));
  while (!pq.empty()) {
    int cost;
    pii cur;
    tie(cost, cur) = pq.top();
    pq.pop();
    if (cur.first == 0 || cur.first == N - 1 || cur.second == 0 ||
        cur.second == N - 1)
      return cost;
    if (dist[cur.first][cur.second] < cost)
      continue;
    for (int k = 0; k < 4; k++) {
      int nx = cur.first + dx[k];
      int ny = cur.second + dy[k];
      int nc = cost;
      if (nx < 0 || nx >= N)
        continue;
      if (ny < 0 || ny >= N)
        continue;
      if (s[nx][ny] == '#')
        nc++;
      if (dist[nx][ny] <= nc)
        continue;
      dist[nx][ny] = nc;
      pq.emplace(nc, make_pair(nx, ny));
    }
  }
  return dist[0][0];
}

int solve() {
  string init(N, '.');
  vector<string> S(N, init);
  reverse(P.begin(), P.end());
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    int x = P[i] / N;
    int y = P[i] % N;
    DBG(P[i])
    DBG(make_pair(x, y))
    ans += dijkstra(x, y, S);
    S[x][y] = '#';
    DBG(S)
  }
  cout << ans << endl;

  return 0;
}

int main() {
  input();
  solve();
  return 0;
}