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

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int ch, cv;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> ch >> cv, ch) {
    unordered_map<string, int> memo;
    string xs, g;
    REP(i, 9) {
      string s;
      cin >> s;
      xs += s;
    }
    REP(i, 9) {
      string s;
      cin >> s;
      g += s;
    }

    priority_queue<pair<int, string>, vector<pair<int, string>>,
                   greater<pair<int, string>>>
        pq;
    pq.push(MP(0, xs));
    memo[xs] = 0;
    while (!pq.empty()) {
      auto p = pq.top();
      pq.pop();
      if (memo[p.second] < p.first)
        continue;
      if (p.second == g)
        break;

      int i;
      for (i = 0;; ++i)
        if (p.second[i] == '0')
          break;

      string tmp = p.second;
      int d = p.first + ch;
      swap(tmp[i], tmp[(i + 8) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
      tmp = p.second;
      swap(tmp[i], tmp[(i + 1) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }

      tmp = p.second;
      d = p.first + cv;
      swap(tmp[i], tmp[(i + 3) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
      tmp = p.second;
      swap(tmp[i], tmp[(i + 6) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
    }
    cout << memo[g] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);

int ch, cv;
int main() {
  cin.tie(0);
  ios_base::sync_with_stdio(false);

  while (cin >> ch >> cv, ch) {
    map<string, int> memo;
    string xs, g;
    REP(i, 9) {
      string s;
      cin >> s;
      xs += s;
    }
    REP(i, 9) {
      string s;
      cin >> s;
      g += s;
    }

    priority_queue<pair<int, string>, vector<pair<int, string>>,
                   greater<pair<int, string>>>
        pq;
    pq.push(MP(0, xs));
    memo[xs] = 0;
    while (!pq.empty()) {
      auto p = pq.top();
      pq.pop();
      if (memo[p.second] < p.first)
        continue;
      if (p.second == g)
        break;

      int i;
      for (i = 0;; ++i)
        if (p.second[i] == '0')
          break;

      string tmp = p.second;
      int d = p.first + ch;
      swap(tmp[i], tmp[(i + 8) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
      tmp = p.second;
      swap(tmp[i], tmp[(i + 1) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }

      tmp = p.second;
      d = p.first + cv;
      swap(tmp[i], tmp[(i + 3) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
      tmp = p.second;
      swap(tmp[i], tmp[(i + 6) % 9]);
      if (!memo.count(tmp) || memo[tmp] > d) {
        memo[tmp] = d;
        pq.push(MP(d, tmp));
      }
    }
    cout << memo[g] << endl;
  }

  return 0;
}