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

#include <algorithm>
#include <iostream>
#include <queue>

#define INF 300000000

using namespace std;

int n, m;
int *used;
int tpow[16] = {1,    3,     9,     27,     81,     243,     729,     2187,
                6561, 19683, 59049, 177147, 531441, 1594323, 4782969, 14348907};

int main(int argc, char const *argv[]) {
  cin >> n >> m;
  while (n != 0 || m != 0) {
    int N = tpow[n];
    used = new int[N];
    fill(used, used + N, INF);
    int ini = 0;
    for (int i = 0; i < 3; i++) {
      int a;
      cin >> a;
      for (int j = 0; j < a; j++) {
        int b;
        cin >> b;
        ini += i * tpow[b - 1];
      }
    }
    queue<int> q;
    q.push(ini);
    used[ini] = 0;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      if (p == 0 || p == N - 1) {
        cout << used[p] << endl;
        break;
      }
      if (used[p] > m) {
        cout << -1 << endl;
        break;
      }
      int a[3];
      a[0] = -1;
      a[1] = -1;
      a[2] = -1;
      for (int i = 0; i < n; i++) {
        a[(p % tpow[i + 1] - p % tpow[i]) / tpow[i]] = i;
      }
      if (a[0] != -1) {
        if (a[1] == -1 || a[0] > a[1]) {
          int pp = p;
          pp += tpow[a[0]];
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
      if (a[2] != -1) {
        if (a[1] == -1 || a[2] > a[1]) {
          int pp = p;
          pp -= tpow[a[2]];
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
      if (a[1] != -1) {
        if (a[0] == -1 || a[1] > a[0]) {
          int pp = p;
          pp -= tpow[a[1]];
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
        if (a[2] == -1 || a[1] > a[2]) {
          int pp = p;
          pp += tpow[a[1]];
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
    }
    delete used;
    cin >> n >> m;
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>

#define INF 300000000

using namespace std;

int n, m;
int *used;

int main(int argc, char const *argv[]) {
  cin >> n >> m;
  while (n != 0 || m != 0) {
    int N = int(pow(3, n));
    used = new int[N];
    fill(used, used + N, INF);
    int ini = 0;
    for (int i = 0; i < 3; i++) {
      int a;
      cin >> a;
      for (int j = 0; j < a; j++) {
        int b;
        cin >> b;
        ini += i * int(pow(3, b - 1));
      }
    }
    queue<int> q;
    q.push(ini);
    used[ini] = 0;
    while (!q.empty()) {
      int p = q.front();
      q.pop();
      if (p == 0 || p == N - 1) {
        cout << used[p] << endl;
        break;
      }
      if (used[p] > m) {
        cout << -1 << endl;
        break;
      }
      int a[3];
      a[0] = -1;
      a[1] = -1;
      a[2] = -1;
      for (int i = 0; i < n; i++) {
        a[(p % int(pow(3, i + 1)) - p % int(pow(3, i))) / int(pow(3, i))] = i;
      }
      if (a[0] != -1) {
        if (a[1] == -1 || a[0] > a[1]) {
          int pp = p;
          pp += int(pow(3, a[0]));
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
      if (a[2] != -1) {
        if (a[1] == -1 || a[2] > a[1]) {
          int pp = p;
          pp -= int(pow(3, a[2]));
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
      if (a[1] != -1) {
        if (a[0] == -1 || a[1] > a[0]) {
          int pp = p;
          pp -= int(pow(3, a[1]));
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
        if (a[2] == -1 || a[1] > a[2]) {
          int pp = p;
          pp += int(pow(3, a[1]));
          if (used[pp] == INF) {
            q.push(pp);
            used[pp] = used[p] + 1;
          }
        }
      }
    }
    delete used;
    cin >> n >> m;
  }
  return 0;
}