Problem p02670 (C++)

Buggy submission (status: Runtime Error):
// In the Name of Allah
// Ya Ali

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair

const int maxn = 510;
const int INF = 1e18;

int n, p[maxn * maxn], dis[maxn * maxn], rr, ans = 0;
vector<int> g[maxn * maxn];
bool mark[maxn];

int ver(int i, int j) {
  if (i < 1 || i > n || j < 1 || j > n)
    return rr;
  return ((i - 1) * n) + j;
}

void bfs(int root) {
  deque<int> qu;

  qu.pb(root);

  while (qu.size()) {

    int v = qu.front();
    qu.pop_front();

    for (auto uu : g[v]) {
      int u = uu, w = mark[v];

      if ((dis[v] + w) < dis[u]) {
        dis[u] = dis[v] + w;

        if (w == 0)
          qu.push_front(u);
        else
          qu.push_back(u);
      }
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    cin >> p[i];
    dis[i] = INF;
    mark[i] = true;
  }

  rr = (n * n) + 1;

  dis[rr] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int v = ((i - 1) * n) + j;
      int v1 = ver(i - 1, j), v2 = ver(i + 1, j), v3 = ver(i, j + 1),
          v4 = ver(i, j - 1);
      g[v].pb(v1);
      g[v1].pb(v);
      g[v].pb(v2);
      g[v2].pb(v);
      g[v].pb(v3);
      g[v3].pb(v);
      g[v].pb(v4);
      g[v4].pb(v);
    }
  }

  bfs(rr);

  for (int i = 1; i <= n * n; i++) {
    ans += dis[p[i]];
    mark[p[i]] = false;
    bfs(p[i]);
  }

  cout << ans;

  return 0;
}


Fixed submission:
// In the Name of Allah
// Ya Ali

#include <bits/stdc++.h>

using namespace std;

#define int long long
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define pb push_back
#define mp make_pair

const int maxn = 510;
const int INF = 1e18;

int n, p[maxn * maxn], dis[maxn * maxn], rr, ans = 0;
vector<int> g[maxn * maxn];
bool mark[maxn * maxn];

int ver(int i, int j) {
  if (i < 1 || i > n || j < 1 || j > n)
    return rr;
  return ((i - 1) * n) + j;
}

void bfs(int root) {
  deque<int> qu;

  qu.pb(root);

  while (qu.size()) {

    int v = qu.front();
    qu.pop_front();

    for (auto uu : g[v]) {
      int u = uu, w = mark[v];

      if ((dis[v] + w) < dis[u]) {
        dis[u] = dis[v] + w;

        if (w == 0)
          qu.push_front(u);
        else
          qu.push_back(u);
      }
    }
  }
}

signed main() {
  ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);

  cin >> n;
  for (int i = 1; i <= n * n; i++) {
    cin >> p[i];
    dis[i] = INF;
    mark[i] = true;
  }

  rr = (n * n) + 1;

  dis[rr] = 0;

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      int v = ((i - 1) * n) + j;
      int v1 = ver(i - 1, j), v2 = ver(i + 1, j), v3 = ver(i, j + 1),
          v4 = ver(i, j - 1);
      g[v].pb(v1);
      g[v1].pb(v);
      g[v].pb(v2);
      g[v2].pb(v);
      g[v].pb(v3);
      g[v3].pb(v);
      g[v].pb(v4);
      g[v4].pb(v);
    }
  }

  bfs(rr);

  for (int i = 1; i <= n * n; i++) {
    ans += dis[p[i]];
    mark[p[i]] = false;
    bfs(p[i]);
  }

  cout << ans;

  return 0;
}