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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int64_t min(int64_t a, int64_t b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> s(n, vector<int>(n, 1));
  int64_t ans = 0;
  vector<int> p(n * n);
  int v[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};

  for (int i = 0; i < n * n; i++) {
    cin >> p.at(i);
  }

  vector<vector<int>> grid(n, vector<int>(n, 0));
  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      grid.at(i).at(j) = min(min(i, n - 1 - i), min(j, n - 1 - j));
    }
  }
  for (size_t ii = 0; ii < n * n; ii++) {
    int x = (p.at(ii) - 1) % n;
    int y = (p.at(ii) - 1) / n;
    s.at(x).at(y) = 0;
    ans += grid.at(x).at(y);
    for (size_t i = 0; i < 4; i++) {
      if (x + v[i][0] != -1 && x + v[i][0] != n && y + v[i][1] != -1 &&
          y + v[i][1] != n) {
        grid.at(x).at(y) =
            min(grid.at(x).at(y), grid.at(x + v[i][0]).at(y + v[i][1]) +
                                      s.at(x + v[i][0]).at(y + v[i][1]));
      }
    }
    queue<pair<int, int>> wait;
    wait.push(make_pair(x, y));
    while (wait.empty() == 0) {
      int nx = wait.front().first;
      int ny = wait.front().second;
      wait.pop();
      for (size_t i = 0; i < 4; i++) {
        int newx = nx + v[i][0];
        int newy = ny + v[i][1];
        if (newx == -1 || newx == n || newy == -1 || newy == n) {
          continue;
        }

        int judge = grid.at(nx).at(ny) + s.at(nx).at(ny);
        if (judge < grid.at(newx).at(newy)) {
          grid.at(newx).at(newy) = judge;
          wait.push(make_pair(newx, newy));
        }
      }
    }
  }
  cout << ans << endl;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <numeric>
#include <queue>
#include <string>
#include <vector>
using namespace std;

int64_t min(int64_t a, int64_t b) {
  if (a > b) {
    return b;
  } else {
    return a;
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> s(n, vector<int>(n, 1));
  int64_t ans = 0;
  vector<int> p(n * n);
  int v[4][2] = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
  for (int i = 0; i < n * n; i++) {
    cin >> p.at(i);
  }
  for (size_t i = 0; i < p.size(); i++) {
    int x, y;
    x = (p.at(i) - 1) % n;
    y = (p.at(i) - 1) / n;
    s.at(x).at(y) = 0;
    int hoge = min(min(x, n - x), min(y, n - y));
    vector<vector<int>> grid(n, vector<int>(n, hoge));
    int temp = hoge;
    grid.at(x).at(y) = 0;
    queue<pair<int, int>> wait;
    wait.push(make_pair(x, y));
    while (wait.empty() == 0) {

      pair<int, int> now = wait.front();
      wait.pop();
      if (grid.at(now.first).at(now.second) >= temp) {
        continue;
      }

      for (int j = 0; j < 4; j++) {
        int nx = now.first + v[j][0];
        int ny = now.second + v[j][1];
        if (nx == -1 || nx == n || ny == -1 || ny == n) {
          temp = min(temp, grid.at(now.first).at(now.second));
        } else {
          if (grid.at(nx).at(ny) > grid.at(now.first).at(now.second)) {
            grid.at(nx).at(ny) =
                grid.at(now.first).at(now.second) + s.at(nx).at(ny);
            wait.push(make_pair(nx, ny));
          }
        }
      }
      if (temp == 0) {
        break;
      }
    }
    ans += temp;
  }
  cout << ans << endl;
}