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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
// #define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using lint = long long;
typedef pair<int, int> P;
const lint inf = 1e9 + 7;
const int MOD = 1000000007;
struct Point {
  int x;
  int y;
  void build(int a, int b) {
    x = a;
    y = b;
  }
  vector<Point> next;
  void get_next(int const &n) {
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx > 0 && nx <= n) {
        if (ny > 0 && ny <= n) {
          Point buf;
          buf.build(nx, ny);
          next.push_back(buf);
        }
      }
    }
  }
};

signed main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  rep(i, n * n) cin >> p[i];
  vector<vector<int>> cost(n + 5, vector<int>(n + 5, inf));
  rep1(i, n) {
    rep1(j, n) {
      int bufy = min(i - 1, n - i);
      int bufx = min(j - 1, n - j);
      cost[i][j] = min(bufy, bufx);
    }
  }
  int res = 0;
  vector<vector<int>> seki(n + 5, vector<int>(n + 5, 1));
  vector<int> dx = {1, 0, -1, 0};
  vector<int> dy = {0, 1, 0, -1};
  for (int l = 0; l < n * n; ++l) {
    int val = p[l];
    int x = (val - 1) % n + 1;
    int y = (val - 1) / n + 1;
    seki[y][x] = 0;
    res += cost[y][x];
    queue<P> que;
    que.push({x, y});
    while (!que.empty()) {
      P buf = que.front();
      que.pop();
      rep(i, 4) {
        int nx = buf.first + dx[i];
        int ny = buf.second + dy[i];
        if (nx > 0 && nx <= n && ny > 0 && ny <= n) {
          if (cost[ny][nx] >
              cost[buf.second][buf.first] + seki[buf.second][buf.first]) {
            cost[ny][nx] =
                cost[buf.second][buf.first] + seki[buf.second][buf.first];
            que.push({nx, ny});
          }
        }
      }
    }
  }
  cout << res << "\\n";
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
// #define int long long
#define rep(i, n) for (int i = 0; i < n; ++i)
#define rep1(i, n) for (int i = 1; i <= n; ++i)
#define ALL(x) x.begin(), x.end()
using ll = long long;
using lint = long long;
typedef pair<int, int> P;
const lint inf = 1e9 + 7;
const int MOD = 1000000007;
struct Point {
  int x;
  int y;
  void build(int a, int b) {
    x = a;
    y = b;
  }
  vector<Point> next;
  void get_next(int const &n) {
    vector<int> dx = {1, 0, -1, 0};
    vector<int> dy = {0, 1, 0, -1};
    rep(i, 4) {
      int nx = x + dx[i];
      int ny = y + dy[i];
      if (nx > 0 && nx <= n) {
        if (ny > 0 && ny <= n) {
          Point buf;
          buf.build(nx, ny);
          next.push_back(buf);
        }
      }
    }
  }
};

signed main() {
  int n;
  cin >> n;
  vector<int> p(n * n);
  rep(i, n * n) cin >> p[i];
  vector<vector<int>> cost(n + 5, vector<int>(n + 5, inf));
  rep1(i, n) {
    rep1(j, n) {
      int bufy = min(i - 1, n - i);
      int bufx = min(j - 1, n - j);
      cost[i][j] = min(bufy, bufx);
    }
  }
  int res = 0;
  vector<vector<int>> seki(n + 5, vector<int>(n + 5, 1));
  for (int l = 0; l < n * n; ++l) {
    int val = p[l];
    int x = (val - 1) % n + 1;
    int y = (val - 1) / n + 1;
    seki[y][x] = 0;
    res += cost[y][x];
    Point s;
    s.build(x, y);
    queue<Point> que;
    que.push(s);
    while (!que.empty()) {
      Point buf = que.front();
      que.pop();
      buf.get_next(n);
      int num = buf.next.size();
      rep(i, num) {
        if (cost[buf.next[i].y][buf.next[i].x] >
            cost[buf.y][buf.x] + seki[buf.y][buf.x]) {
          cost[buf.next[i].y][buf.next[i].x] =
              cost[buf.y][buf.x] + seki[buf.y][buf.x];
          que.push(buf.next[i]);
        }
      }
    }
  }
  cout << res << "\\n";
  return 0;
}