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

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

ll encode(const vector<int> &vec) {
  ll res = 0;
  for (int i = 0; i < 9; i++) {
    res = res * 10 + vec[i];
  }
  return res;
}

void decode(ll code, vector<int> &vec) {
  for (int i = 0; i < 9; i++) {
    vec[8 - i] = code % 10;
    code /= 10;
  }
}

int main() {
  ios::sync_with_stdio(false);

  int ch, cv;
  while (cin >> ch >> cv && ch + cv) {
    map<ll, int> dist;
    vector<int> start(9);
    vector<int> goal(9);
    vector<int> state(9);

    REP(i, 9) cin >> start[i];
    REP(i, 9) cin >> goal[i];

    priority_queue<pair<int, ll>> que;

    ll start_code = encode(start);
    que.push(make_pair(0, start_code));
    dist[start_code] = 0;

    while (!que.empty()) {
      int c = -que.top().first;
      ll code = que.top().second;
      que.pop();

      decode(code, state);

      if (goal == state) {
        cout << c << endl;
        break;
      }

      if (c > dist[code]) {
        continue;
      }

      int p = find(ALL(state), 0) - state.begin();
      int d[] = {-3, -1, 1, 3};

      for (int i = 0; i < 4; i++) {
        int nc = c + (abs(d[i]) == 1 ? ch : cv);
        int np = (p + d[i] + 9) % 9;

        swap(state[p], state[np]);

        ll next_code = encode(state);
        if (dist.count(next_code) == 0 || dist[next_code] > nc) {
          dist[next_code] = nc;
          que.push(make_pair(-nc, next_code));
        }

        swap(state[np], state[p]);
      }
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

#define REP2(i, m, n) for (int i = (int)(m); i < (int)(n); i++)
#define REP(i, n) REP2(i, 0, n)
#define ALL(S) (S).begin(), (S).end()
typedef long long ll;

int main() {
  ios::sync_with_stdio(false);

  int ch, cv;
  while (cin >> ch >> cv && ch + cv) {
    map<vector<int>, int> dist;
    vector<int> start(9);
    vector<int> goal(9);

    REP(i, 9) cin >> start[i];
    REP(i, 9) cin >> goal[i];

    priority_queue<pair<int, vector<int>>> que;

    que.push(make_pair(0, start));
    dist[start] = 0;

    while (!que.empty()) {
      int c = -que.top().first;
      vector<int> s = que.top().second;
      que.pop();

      if (goal == s) {
        cout << c << endl;
        break;
      }

      if (c > dist[s]) {
        continue;
      }

      int p = find(ALL(s), 0) - s.begin();
      int d[] = {-3, -1, 1, 3};

      for (int i = 0; i < 4; i++) {
        int nc = c + (abs(d[i]) == 1 ? ch : cv);
        int np = (p + d[i] + 9) % 9;

        swap(s[p], s[np]);

        if (dist.count(s) == 0 || dist[s] > nc) {
          dist[s] = nc;
          que.push(make_pair(-nc, s));
        }

        swap(s[np], s[p]);
      }
    }
  }
  return 0;
}