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

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int movey[] = {0, 0, 1, -1};
int movex[] = {1, -1, 0, 0};
int n;
void dfs(vector<vector<pii>> &board, int posx, int posy) {
  int a = (board[posy][posx].second ^ 1);
  rep(i, 4) {
    int x = posx + movex[i], y = posy + movey[i];
    if (x >= n || y >= n || x < 0 || y < 0)
      continue;
    if (board[y][x].first > board[posy][posx].first + a) {
      board[y][x].first = board[posy][posx].first + a;
      dfs(board, x, y);
    }
  }
}
int main() {
  cin >> n;
  vector<vector<pii>> board(n, vector<pii>(n, make_pair(0, 0)));
  rep(y, n) {
    rep(x, n) {
      int num = min({x, y, n - x - 1, n - y - 1});
      board[y][x].first = num;
    }
  }
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    int posy = p / n, posx = p % n;
    board[posy][posx].second = 1;
    ans += board[posy][posx].first;
    dfs(board, posx, posy);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <bitset>    // bitset
#include <cctype>    // isupper, islower, isdigit, toupper, tolower
#include <cmath>     //sqrt pow
#include <cstdint>   // int64_t, int*_t
#include <cstdio>    // printf
#include <deque>     // deque
#include <iostream>  // cout, endl, cin
#include <map>       // map
#include <queue>     // queue, priority_queue
#include <set>       // set
#include <stack>     // stack
#include <string>    // string, to_string, stoi
#include <tuple>     // tuple, make_tuple
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <utility>       // pair, make_pair
#include <vector>        // vector

#define rep(i, n) for (int i = 0; i < n; i++)
#define vi vector<int>
#define vvi vector<vi>
#define pii pair<int, int>
#define mod 1000000007
using ll = long long;
using namespace std;
int main() {
  int n;
  cin >> n;
  vi p(n * n);
  vvi board(n, vi(n, 1));
  vi movex{1, -1, 0, 0}, movey{0, 0, 1, -1};
  int ans = 0;
  rep(i, n * n) {
    int p;
    cin >> p;
    p--;
    int sx = p % n, sy = p / n;
    board[sy][sx] = 0;

    priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                   greater<pair<int, pair<int, int>>>>
        que;
    // <最短距離, 頂点>
    que.push(
        make_pair(0, make_pair(sy, sx))); // 0 をスタート地点として距離を0とする
    vvi distance(n, vi(n, -1));
    distance[sy][sx] = 0;
    while (!que.empty()) {
      pair<int, pair<int, int>> info = que.top();
      que.pop();
      int posx = info.second.second, posy = info.second.first;
      if (distance[posy][posx] < info.first)
        continue;
      rep(j, 4) {
        int x = movex[j] + posx, y = movey[j] + posy;
        if (x < 0 || x >= n || y < 0 || y >= n)
          continue;
        int kyori = board[y][x] + distance[posy][posx];
        if (distance[y][x] == -1 || distance[y][x] > kyori) {
          que.push(make_pair(kyori, make_pair(y, x)));
          distance[y][x] = kyori;
        }
      }
    }
    int mn = 250000;
    rep(i, n) {
      mn = distance[0][i] < mn ? distance[0][i] : mn;
      mn = distance[n - 1][i] < mn ? distance[n - 1][i] : mn;
      mn = distance[i][0] < mn ? distance[i][0] : mn;
      mn = distance[i][n - 1] < mn ? distance[i][n - 1] : mn;
    }
    ans += mn;
  }
  cout << ans << endl;
}