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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

typedef unsigned int uint;
typedef pair<int, uint> iu;

uint encode(const vector<int> &v) {
  uint res = 0;
  for (int i = (int)v.size() - 1; i >= 0; --i)
    res = (res << 4) + v[i];
  return res;
}
vector<int> decode(uint e) {
  vector<int> res;
  for (; e; e >>= 4)
    res.push_back(e & 0xf);
  return res;
}
int main() {
  int n, c[16][16];
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cin >> c[i][j];

  vector<int> ori;
  for (int i = 1; i <= n; ++i)
    ori.push_back(i);
  int ori_e = encode(ori);

  int res = 0;
  map<int, int> dis;
  priority_queue<iu, vector<iu>, greater<iu>> q;
  dis[ori_e] = 0;
  q.push(iu(0, ori_e));
  while (!q.empty()) {
    iu t = q.top();
    q.pop();
    int cost = t.first;
    uint e = t.second;
    if (cost > dis[e])
      continue;

    max_swap(res, cost);

    vector<int> v = decode(e);
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        swap(v[i], v[j]);
        int nc = cost + c[v[i]][v[j]];
        uint ne = encode(v);
        swap(v[i], v[j]);

        if (!dis.count(ne) || nc < dis[ne]) {
          dis[ne] = nc;
          q.push(iu(nc, ne));
        }
      }
    }
  }
  cout << res << endl;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype
template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define CLEAR(arr, val) memset(arr, val, sizeof(arr))

#define rep(i, n) for (int i = 0; i < n; ++i)

template <class T> void max_swap(T &a, const T &b) { a = max(a, b); }
template <class T> void min_swap(T &a, const T &b) { a = min(a, b); }

typedef long long ll;
typedef pair<int, int> pint;

const double EPS = 1e-8;
const double PI = acos(-1.0);
const int dx[] = {0, 1, 0, -1};
const int dy[] = {1, 0, -1, 0};
bool valid_pos(int x, int y, int w, int h) {
  return 0 <= x && x < w && 0 <= y && y < h;
}

int encode(const vector<int> &v) {
  int res = 0;
  for (int i = (int)v.size() - 1; i >= 0; --i)
    res = (res << 4) + v[i];
  return res;
}
vector<int> decode(int e) {
  vector<int> res;
  for (; e; e >>= 4)
    res.push_back(e & 0xf);
  return res;
}
int main() {
  int n, c[16][16];
  cin >> n;
  for (int i = 1; i <= n; ++i)
    for (int j = 1; j <= n; ++j)
      cin >> c[i][j];

  vector<int> ori;
  for (int i = 1; i <= n; ++i)
    ori.push_back(i);
  int ori_e = encode(ori);

  int res = 0;
  map<int, int> dis;
  priority_queue<pint, vector<pint>, greater<pint>> q;
  dis[ori_e] = 0;
  q.push(pint(0, ori_e));
  while (!q.empty()) {
    pint t = q.top();
    q.pop();
    int cost = t.first, e = t.second;
    if (cost > dis[e])
      continue;

    max_swap(res, cost);

    vector<int> v = decode(e);
    for (int i = 0; i < n; ++i) {
      for (int j = i + 1; j < n; ++j) {
        swap(v[i], v[j]);
        int nc = cost + c[v[i]][v[j]];
        int ne = encode(v);
        swap(v[i], v[j]);

        if (!dis.count(ne) || nc < dis[ne]) {
          dis[ne] = nc;
          q.push(pint(nc, ne));
        }
      }
    }
  }
  cout << res << endl;
}