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

#include <bits/stdc++.h>

#define debug(x)                                                               \\
  cout << "DEBUG"                                                              \\
       << " " << #x << ":" << x << '\\n'
#define rep(i, n)                                                              \\
  for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) // 0-indexed昇順
#define rep1(i, n)                                                             \\
  for (int_fast32_t i = 1; i <= ((int_fast32_t)(n)); i++) // 1-indexed昇順
#define rrep(i, n)                                                             \\
  for (int_fast32_t i = ((int_fast32_t)(n)-1); i >= 0; i--) // 0-indexed降順
#define rrep1(i, n)                                                            \\
  for (int_fast32_t i = ((int_fast32_t)(n)); i >= 1; i--) // 1-indexed降順
#define all(x) (x).begin(), (x).end()

using namespace std;

template <typename T> using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>; // 2次元配列
using PII = pair<int_fast32_t, int_fast32_t>;

template <class T> using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/
vi mv_i = {1, -1, 0, 0};
vi mv_j = {0, 0, 1, -1};

/*MAIN==================================================*/
signed main() {
  int START_TIME = clock();
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化
  //    cout << fixed << setprecision(15);

  i32 n;
  cin >> n;
  vec<PII> p;
  rep(i, n * n) {
    i32 tmp, h, w;
    cin >> tmp;
    --tmp;
    h = tmp / n, w = tmp % n;
    p.emplace_back(h, w);
  }

  vii grid(n, vi(n, 1));
  vii mini_dist(n, vi(n, inf));
  queue<PII> que;
  vii dist(n, vi(n));
  rep(i, n) rep(j, n) dist[i][j] = min<i32>({i, n - i - 1, j, n - j - 1});

  i32 cnt = 0;

  for (i32 i = 0; i < n * n; ++i) {
    i32 si = p[i].first, sj = p[i].second;
    cnt += dist[si][sj];
    grid[si][sj] = 0;
    que.emplace(si, sj);

    i32 ui, uj, ni, nj;
    i32 minimum = inf;
    while (que.size()) {
      tie(ui, uj) = que.front();
      que.pop();
      rep(_, 4) {
        ni = ui + mv_i[_], nj = uj + mv_j[_];
        if (!(0 <= ni && ni < n && 0 <= nj && nj < n))
          continue;
        if (dist[ni][nj] > dist[ui][uj] + grid[ui][uj]) {
          dist[ni][nj] = dist[ui][uj] + grid[ui][uj];
          que.emplace(ni, nj);
        }
      }
    }
    //        debug(cnt);
    //        // debug
    //        rep(i,n) rep(j,n) cout << grid[i][j] << (j==n-1 ? '\\n' : ' ');
    //        cout << '\\n';
    //        rep(i,n) rep(j,n) cout << dist[i][j] << (j==n-1 ? "\\n" : "  ");
    //        cout << '\\n';
  }

  cout << cnt << '\\n';
  //    debug(debug_cnt);

  // printf("ExecutionTime: %d
  // /ms\\n",1000*(int)((clock()-START_TIME)/CLOCKS_PER_SEC));
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define debug(x)                                                               \\
  cout << "DEBUG"                                                              \\
       << " " << #x << ":" << x << '\\n'
#define rep(i, n)                                                              \\
  for (int_fast32_t i = 0; i < ((int_fast32_t)(n)); i++) // 0-indexed昇順
#define rep1(i, n)                                                             \\
  for (int_fast32_t i = 1; i <= ((int_fast32_t)(n)); i++) // 1-indexed昇順
#define rrep(i, n)                                                             \\
  for (int_fast32_t i = ((int_fast32_t)(n)-1); i >= 0; i--) // 0-indexed降順
#define rrep1(i, n)                                                            \\
  for (int_fast32_t i = ((int_fast32_t)(n)); i >= 1; i--) // 1-indexed降順
#define all(x) (x).begin(), (x).end()

using namespace std;

template <typename T> using vec = vector<T>;
using i32 = int_fast32_t;
using i64 = int_fast64_t;
using u32 = uint_fast32_t;
using u64 = uint_fast64_t;
using ll = long long;
using ld = long double;
using vi = vec<int_fast32_t>;
using vl = vec<int_fast64_t>;
using vld = vec<ld>;
using vii = vec<vi>; // 2次元配列
using PII = pair<int_fast32_t, int_fast32_t>;

template <class T> using maxheap = std::priority_queue<T>;
template <class T>
using minheap = std::priority_queue<T, std::vector<T>, std::greater<T>>;

template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

const ld Pi = std::acos(-1.0L);
constexpr ll infll = (1LL << 62) - 1;
constexpr int inf = (1 << 30) - 1;
const int mod = 1000000007;
// const int mod = 998244353;

/*FUNCs=================================================*/
vi mv_i = {1, -1, 0, 0};
vi mv_j = {0, 0, 1, -1};

/*MAIN==================================================*/
signed main() {
  int START_TIME = clock();
  cin.tie(nullptr);
  ios::sync_with_stdio(false); // cin cout 高速化
  //    cout << fixed << setprecision(15);

  i32 n;
  cin >> n;
  vec<PII> p;
  rep(i, n * n) {
    i32 tmp, h, w;
    cin >> tmp;
    --tmp;
    h = tmp / n;
    w = tmp % n;
    p.emplace_back(h, w);
    //        debug(h);debug(w);
  }

  vii grid(n, vi(n, 1));
  vii mini_dist(n, vi(n, inf));
  deque<PII> dq;
  vii dist, init_dist(n, vi(n, inf));

  i64 cnt = 0;

  i32 debug_cnt = 0;
  bool escaper = 0;
  for (i32 i = 0; i < n * n; ++i) {
    escaper = 0;
    i32 si = p[i].first, sj = p[i].second;
    dist = init_dist;
    dist[si][sj] = 0;
    dq.emplace_back(si, sj);

    grid[si][sj] = 0;

    i32 ui, uj, ni, nj;
    i32 minimum = inf;

    while (!dq.empty()) {
      ++debug_cnt;
      tie(ui, uj) = dq.front();
      dq.pop_front();
      if (dist[ui][uj] >= minimum)
        continue;
      //            debug(ui);debug(uj);

      rep(_, 4) {
        ni = ui + mv_i[_], nj = uj + mv_j[_];
        //                debug(ni);debug(nj);
        if (!(0 <= ni && ni < n && 0 <= nj && nj < n)) { // 外に隣接
          if (chmin(minimum, dist[ui][uj]))
            escaper = 1;
        } else {
          bool person = (grid[ni][nj] == 1);
          if (mini_dist[ni][nj] != inf) {
            chmin(minimum, mini_dist[ni][nj] + dist[ui][uj]);
            if (minimum == 0)
              escaper = 1;
          }
          if (dist[ni][nj] > dist[ui][uj]) {
            dist[ni][nj] = dist[ui][uj] + grid[ni][nj];
            if (person) {
              dq.emplace_back(ni, nj);
            } else {
              dq.push_front({ni, nj});
            }
          }
        }
        if (escaper)
          break;
      }
      if (escaper)
        break;
    }
    cnt += minimum;
    mini_dist[si][sj] = minimum;

    //        debug(minimum);
    //        // debug
    //        rep(i,n) rep(j,n) cout << grid[i][j] << (j==n-1 ? '\\n' : ' ');
    //        cout << '\\n';
    //        rep(i,n) rep(j,n) cout << dist[i][j] << (j==n-1 ? "\\n" : "  ");
    //        cout << '\\n';
  }

  cout << cnt << '\\n';
  //    debug(debug_cnt);

  // printf("ExecutionTime: %d
  // /ms\\n",1000*(int)((clock()-START_TIME)/CLOCKS_PER_SEC));
}