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

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

struct State {
  string s;
  ll c;
  int p;
  State(string &as, ll ac, int ap) {
    s = as;
    c = ac;
    p = ap;
  }
  bool operator<(const State &right) const { return c > right.c; }
};

int main() {

  while (true) {
    int ch, cv;
    cin >> ch >> cv;
    if (ch == 0 && cv == 0)
      break;

    string cur = "0123456789";
    int curz = -1;
    string goal = "0123456789";
    for (int i = 0; i < 9; i++) {
      int d;
      cin >> d;
      cur[i] = '0' + d;
      if (d == 0) {
        curz = i;
      }
    }
    assert(curz != -1);
    for (int i = 0; i < 9; i++) {
      int d;
      cin >> d;
      goal[i] = '0' + d;
    }

    unordered_map<string, ll> mc;
    priority_queue<State> que;
    que.emplace(cur, 0, curz);
    mc[cur] = 0;

    while (que.size()) {
      State s = que.top();
      que.pop();

      if (mc[s.s] < s.c)
        continue;
      if (s.s == goal) {
        cout << s.c << endl;
        break;
      }

      swap(s.s[s.p], s.s[(s.p + 1) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + ch) {
        mc[s.s] = s.c + ch;
        que.emplace(s.s, s.c + ch, (s.p + 1) % 9);
      }
      swap(s.s[s.p], s.s[(s.p + 1) % 9]);

      swap(s.s[s.p], s.s[(9 + s.p - 1) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + ch) {
        mc[s.s] = s.c + ch;
        que.emplace(s.s, s.c + ch, (9 + s.p - 1) % 9);
      }
      swap(s.s[s.p], s.s[(9 + s.p - 1) % 9]);

      swap(s.s[s.p], s.s[(s.p + 3) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + cv) {
        mc[s.s] = s.c + cv;
        que.emplace(s.s, s.c + cv, (s.p + 3) % 9);
      }
      swap(s.s[s.p], s.s[(s.p + 3) % 9]);

      swap(s.s[s.p], s.s[(9 + s.p - 3) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + cv) {
        mc[s.s] = s.c + cv;
        que.emplace(s.s, s.c + cv, (9 + s.p - 3) % 9);
      }
      swap(s.s[s.p], s.s[(9 + s.p - 3) % 9]);
    }
  }

  return 0;
}

Error: TLE

Fixed submission:

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

struct State {
  vector<int> s;
  ll c;
  int p;
  State(vector<int> &as, ll ac, int ap) {
    s = as;
    c = ac;
    p = ap;
  }
  bool operator<(const State &right) const { return c > right.c; }
};

int main() {

  while (true) {
    int ch, cv;
    cin >> ch >> cv;
    if (ch == 0 && cv == 0)
      break;

    vector<int> cur(9);
    int curz = -1;
    vector<int> goal(9);
    for (int i = 0; i < 9; i++) {
      cin >> cur[i];
      if (cur[i] == 0) {
        curz = i;
      }
    }
    assert(curz != -1);
    for (int i = 0; i < 9; i++) {
      cin >> goal[i];
    }

    map<vector<int>, ll> mc;
    priority_queue<State> que;
    que.emplace(cur, 0, curz);
    mc[cur] = 0;

    while (que.size()) {
      State s = que.top();
      que.pop();

      if (mc[s.s] < s.c)
        continue;
      bool ok = true;
      for (int i = 0; i < 9; i++) {
        if (s.s[i] != goal[i]) {
          ok = false;
          break;
        }
      }
      if (ok) {
        cout << s.c << endl;
        break;
      }

      swap(s.s[s.p], s.s[(s.p + 1) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + ch) {
        mc[s.s] = s.c + ch;
        que.emplace(s.s, s.c + ch, (s.p + 1) % 9);
      }
      swap(s.s[s.p], s.s[(s.p + 1) % 9]);

      swap(s.s[s.p], s.s[(9 + s.p - 1) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + ch) {
        mc[s.s] = s.c + ch;
        que.emplace(s.s, s.c + ch, (9 + s.p - 1) % 9);
      }
      swap(s.s[s.p], s.s[(9 + s.p - 1) % 9]);

      swap(s.s[s.p], s.s[(s.p + 3) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + cv) {
        mc[s.s] = s.c + cv;
        que.emplace(s.s, s.c + cv, (s.p + 3) % 9);
      }
      swap(s.s[s.p], s.s[(s.p + 3) % 9]);

      swap(s.s[s.p], s.s[(9 + s.p - 3) % 9]);
      if (mc.find(s.s) == mc.end() || mc[s.s] > s.c + cv) {
        mc[s.s] = s.c + cv;
        que.emplace(s.s, s.c + cv, (9 + s.p - 3) % 9);
      }
      swap(s.s[s.p], s.s[(9 + s.p - 3) % 9]);
    }
  }

  return 0;
}