Problem p01478 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
const int INF = 1000000000;

inline int swap(int cur, int i, int j) {
  int result = cur & ~(7 << (i * 3)) & ~(7 << (j * 3));
  result |= ((cur >> (i * 3)) & 7) << (j * 3);
  result |= ((cur >> (j * 3)) & 7) << (i * 3);
  return result;
}

void recur(map<int, int> &mp, int cur, int mask) {
  if (mask == 0) {
    mp.insert(pii(cur, mp.size()));
  } else {
    for (int i = 0; i < 8; ++i) {
      if (!(mask & (1 << i))) {
        continue;
      }
      recur(mp, (cur << 3) | i, mask & ~(1 << i));
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> c[i][j];
    }
  }
  int init = 0;
  for (int i = 0; i < n; ++i) {
    init |= i << (i * 3);
  }
  map<int, int> mp;
  recur(mp, 0, (1 << n) - 1);
  vector<int> minval(mp.size(), INF);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, init));
  minval[mp[init]] = 0;
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();
    int v = p.second, cost = p.first;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        int w = swap(v, i, j), wi = mp[w];
        int nc = cost + c[i][j];
        if (nc < minval[wi]) {
          pq.push(pii(nc, w));
          minval[wi] = nc;
        }
      }
    }
  }
  int answer = 0;
  for (int i = 0; i < minval.size(); ++i) {
    answer = max(answer, minval[i]);
  }
  cout << answer << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <vector>

using namespace std;
typedef pair<int, int> pii;
const int INF = 1000000000;

inline int swap(int cur, int i, int j) {
  int result = cur & ~(7 << (i * 3)) & ~(7 << (j * 3));
  result |= ((cur >> (i * 3)) & 7) << (j * 3);
  result |= ((cur >> (j * 3)) & 7) << (i * 3);
  return result;
}

void recur(map<int, int> &mp, int cur, int mask) {
  if (mask == 0) {
    mp.insert(pii(cur, mp.size()));
  } else {
    for (int i = 0; i < 7; ++i) {
      if (!(mask & (1 << i))) {
        continue;
      }
      recur(mp, (cur << 3) | i, mask & ~(1 << i));
    }
  }
}

int main() {
  int n;
  cin >> n;
  vector<vector<int>> c(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> c[i][j];
    }
  }
  int init = 0;
  for (int i = 0; i < n; ++i) {
    init |= i << (i * 3);
  }
  map<int, int> mp;
  recur(mp, 0, (1 << n) - 1);
  vector<int> minval(mp.size(), INF);
  priority_queue<pii, vector<pii>, greater<pii>> pq;
  pq.push(pii(0, init));
  minval[mp[init]] = 0;
  while (!pq.empty()) {
    pii p = pq.top();
    pq.pop();
    int v = p.second, cost = p.first;
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        int w = swap(v, i, j), wi = mp[w];
        int nc = cost + c[i][j];
        if (nc < minval[wi]) {
          pq.push(pii(nc, w));
          minval[wi] = nc;
        }
      }
    }
  }
  int answer = 0;
  for (int i = 0; i < minval.size(); ++i) {
    answer = max(answer, minval[i]);
  }
  cout << answer << endl;
  return 0;
}