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

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

int mv[9][4] = {{1, 8, 6, 3}, {2, 0, 7, 4}, {3, 1, 8, 5},
                {4, 2, 0, 6}, {5, 3, 1, 7}, {6, 4, 2, 8},
                {7, 5, 3, 0}, {8, 6, 4, 1}, {0, 7, 5, 2}};

int main() {
  int a, b, c, d;

  while (1) {
    cin >> a >> b;
    if (a + b == 0)
      break;
    long long s = 0, g = 0, n = 1e8;
    for (int i = 0; i < 9; i++) {
      cin >> c;
      if (c == 0)
        d = i;
      c++;
      c *= n;
      s += c;
      n /= 10;
    }
    n = 1e8;
    for (int i = 0; i < 9; i++) {
      cin >> c;
      c++;
      c *= n;
      g += c;
      n /= 10;
    }

    priority_queue<pair<int, pair<int, int>>> pq;
    map<int, int> m;
    pq.push(mp(0, mp(s, d)));
    m[s] = 0;

    while (!pq.empty()) {
      pair<int, pair<int, int>> u = pq.top();
      int x = u.f * (-1);
      pq.pop();
      if (m[u.s.f] < x)
        continue;
      if (u.s.f == g)
        break;

      for (int k = 0; k < 4; k++) {
        int y = u.s.f, e = u.s.s, f, h, t, z;
        h = (y / (pow(10, 8 - mv[e][k])));
        t = h % 10;
        for (int j = 0; j < 9; j++) {
          int A = (y / pow(10, j));
          if (A % 10 == t) {
            y -= t * pow(10, j);
            y += pow(10, j);
            z = j;
            break;
          }
        }
        y -= pow(10, 8 - e);
        y += t * pow(10, 8 - e);
        if (k < 2)
          f = x + a;
        else
          f = x + b;
        if (m.find(y) != m.end() && m[y] <= f)
          continue;
        m[y] = f;
        pq.push(mp(f * (-1), mp(y, 8 - z)));
      }
    }
    cout << m[g] << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define f first
#define s second
#define mp make_pair
#define inf 1 << 30
using namespace std;

int mv[9][4] = {{1, 8, 6, 3}, {2, 0, 7, 4}, {3, 1, 8, 5},
                {4, 2, 0, 6}, {5, 3, 1, 7}, {6, 4, 2, 8},
                {7, 5, 3, 0}, {8, 6, 4, 1}, {0, 7, 5, 2}};

int main() {
  int a, b, d;
  char c;

  while (1) {
    cin >> a >> b;
    if (a + b == 0)
      break;
    string s = "", g = "";
    for (int i = 0; i < 9; i++) {
      cin >> c;
      s += c;
      if (c == '0')
        d = i;
    }
    for (int i = 0; i < 9; i++) {
      cin >> c;
      g += c;
    }

    int ans = inf;
    priority_queue<pair<int, pair<string, int>>> pq;
    map<string, int> m;
    pq.push(mp(0, mp(s, d)));
    m[s] = 0;

    while (!pq.empty()) {
      pair<int, pair<string, int>> u = pq.top();
      int x = u.f * (-1);
      pq.pop();
      if (m[u.s.f] < x)
        continue;

      if (u.s.f == g)
        break;

      for (int i = 0; i < 4; i++) {
        string t = u.s.f;
        swap(t[u.s.s], t[mv[u.s.s][i]]);
        if (i < 2) {
          if (m.find(t) != m.end() && m[t] <= (x + a))
            continue;
          m[t] = x + a;
          pq.push(mp((-1) * (x + a), mp(t, mv[u.s.s][i])));
        } else {
          if (m.find(t) != m.end() && m[t] <= (x + b))
            continue;
          m[t] = x + b;
          pq.push(mp((-1) * (x + b), mp(t, mv[u.s.s][i])));
        }
      }
    }
    cout << m[g] << endl;
  }
  return 0;
}