Problem p00918 (C++) — buggy submission, verdict: Memory Limit Exceeded

// template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

template <typename T> using max_pq = priority_queue<T, vector<T>, less<T>>;

template <typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline bool chmax(T &x, U y) {
  if (x >= y)
    return false;
  x = y;
  return true;
}

template <typename T, typename U> inline bool chmin(T &x, U y) {
  if (x <= y)
    return false;
  x = y;
  return true;
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  std::sort(std::begin(c), std::end(c));
  c.erase(std::unique(all(c)), std::end(c));
}

// }}}

using vi = vector<int>;

const string T[] = {"ABIGD", "BCAHE", "CDBIF", "DECAG", "EFDBH",
                    "FGECI", "GHFDA", "HIGEB", "IAHFC"};

int H[9][2], V[9][2];
int ch, cv;
vi s(9), g(9);
unordered_map<int, int> dist;

int pack(const vi &v) {
  int r = 0;
  rep(i, 9) r = r * 9 + v[i];
  return r;
}

vi unpack(int v) {
  vi r(9);
  rrep(i, 9) {
    r[i] = v % 9;
    v /= 9;
  }
  return r;
}

int main() {
  for (auto s : T) {
    rep(i, 2) {
      H[s[0] - 'A'][i] = s[i + 1] - 'A';
      V[s[0] - 'A'][i] = s[i + 3] - 'A';
    }
  }

  /*{
          vi v(9);
          iota(all(v), 0);
          do {
                  int t = conv1.size();
                  conv1[v] = t;
                  conv2[t] = v;
          } while (next_permutation(all(v)));
  }*/

  while (cin >> ch >> cv, ch) {
    rep(i, 9) cin >> s[i];
    rep(i, 9) cin >> g[i];
    min_pq<pair<int, int>> pq;
    dist.clear();
    dist[pack(s)] = 0;
    pq.emplace(0, pack(s));
    while (pq.size()) {
      int sum;
      int v_;
      tie(sum, v_) = pq.top();
      pq.pop();
      vi v = unpack(v_);
      if (v == g)
        break;
      if (dist[v_] < sum)
        continue;
      int p = find(all(v), 0) - begin(v);
      rep(i, 2) {
        vi nv = v;
        swap(nv[p], nv[H[p][i]]);
        if (!dist.count(pack(nv)))
          dist[pack(nv)] = INF;
        if (chmin(dist[pack(nv)], sum + ch)) {
          pq.emplace(sum + ch, pack(nv));
        }
        nv = v;
        swap(nv[p], nv[V[p][i]]);
        if (!dist.count(pack(nv)))
          dist[pack(nv)] = INF;
        if (chmin(dist[pack(nv)], sum + cv)) {
          pq.emplace(sum + cv, pack(nv));
        }
      }
    }
    cout << dist[pack(g)] << endl;
  }
}

Error: MLE

Fixed submission:

// template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define eb emplace_back
#define ef emplace_front
#define pb pop_back
#define pf pop_front
#define all(c) std::begin(c), std::end(c)
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second
#define popcnt __builtin_popcountll

using uint = unsigned;
using ll = long long;
using ull = unsigned long long;
using ld = long double;

template <typename T> using max_pq = priority_queue<T, vector<T>, less<T>>;

template <typename T> using min_pq = priority_queue<T, vector<T>, greater<T>>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

const int dx[] = {-1, 0, 1, 0};
const int dy[] = {0, -1, 0, 1};
const int dx8[] = {-1, -1, 0, 1, 1, 1, 0, -1};
const int dy8[] = {0, -1, -1, -1, 0, 1, 1, 1};

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline bool chmax(T &x, U y) {
  if (x >= y)
    return false;
  x = y;
  return true;
}

template <typename T, typename U> inline bool chmin(T &x, U y) {
  if (x <= y)
    return false;
  x = y;
  return true;
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  std::sort(std::begin(c), std::end(c));
  c.erase(std::unique(all(c)), std::end(c));
}

// }}}

using vi = vector<int>;

const string T[] = {"ABIGD", "BCAHE", "CDBIF", "DECAG", "EFDBH",
                    "FGECI", "GHFDA", "HIGEB", "IAHFC"};

int H[9][2], V[9][2];
map<vi, int> conv1;
map<int, vi> conv2;
int ch, cv;
vi s(9), g(9);
int dist[400000];

int main() {
  for (auto s : T) {
    rep(i, 2) {
      H[s[0] - 'A'][i] = s[i + 1] - 'A';
      V[s[0] - 'A'][i] = s[i + 3] - 'A';
    }
  }

  {
    vi v(9);
    iota(all(v), 0);
    do {
      int t = conv1.size();
      conv1[v] = t;
      conv2[t] = v;
    } while (next_permutation(all(v)));
  }

  return 0;

  while (cin >> ch >> cv, ch) {
    rep(i, 9) cin >> s[i];
    rep(i, 9) cin >> g[i];
    min_pq<pair<int, int>> pq;
    fill_n(dist, 400000, INF);
    dist[conv1[s]] = 0;
    pq.emplace(0, conv1[s]);
    while (pq.size()) {
      int sum;
      int v_;
      tie(sum, v_) = pq.top();
      pq.pop();
      vi v = conv2[v_];
      if (v == g)
        break;
      if (dist[v_] < sum)
        continue;
      int p = find(all(v), 0) - begin(v);
      rep(i, 2) {
        vi nv = v;
        swap(nv[p], nv[H[p][i]]);
        if (chmin(dist[conv1[nv]], sum + ch)) {
          pq.emplace(sum + ch, conv1[nv]);
        }
        nv = v;
        swap(nv[p], nv[V[p][i]]);
        if (chmin(dist[conv1[nv]], sum + cv)) {
          pq.emplace(sum + cv, conv1[nv]);
        }
      }
    }
    cout << dist[conv1[g]] << endl;
  }
}