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

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> P2;

int h, w;
int g[30][30];
int d[30][30][25];

int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

int r[4][6] = {{1, 5, 2, 3, 0, 4},
               {3, 1, 0, 5, 4, 2},
               {4, 0, 2, 3, 5, 1},
               {2, 1, 5, 0, 4, 3}};

vector<int> trans(int n) {
  vector<int> res;
  while (n > 0) {
    res.push_back(n % 6);
    n /= 6;
  }
  while (res.size() < 6)
    res.push_back(0);
  return res;
}

int rot(int n, int dir) {
  int tmp[6];
  vector<int> die = trans(n);
  for (int i = 0; i < 6; i++)
    tmp[i] = die[r[dir][i]];

  int res = 0, k = 1;
  for (int i = 0; i < 6; i++) {
    res += tmp[i] * k;
    k *= 6;
  }
  return res;
}

int main() {
  while (cin >> h >> w, h || w) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        cin >> g[i][j];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < 25; k++)
          d[i][j][k] = 10000000;
      }
    }

    int sy, sx, gy, gx;
    cin >> sy >> sx >> gy >> gx;

    priority_queue<P2, vector<P2>, greater<P2>> q;
    map<int, int> m1, m2;
    m1[44790] = 1;
    m2[1] = 44790;
    q.push(P2(P(0, 1), P(sy, sx)));
    d[sy][sx][1] = 0;
    int cnt = 2;

    int hoge = 0;
    while (!q.empty()) {
      P2 p = q.top();
      q.pop();
      int s = p.first.first, num = p.first.second;
      int y = p.second.first, x = p.second.second;

      // cout << num << endl;

      for (int i = 0; i < 4; i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || nx < 0 || h <= ny || w <= nx)
          continue;
        int nnum = rot(m2[num], i);

        vector<int> die = trans(nnum);
        if (!m1[nnum]) {
          m1[nnum] = cnt;
          m2[cnt] = nnum;
          cnt++;
        }
        nnum = m1[nnum];

        if (d[ny][nx][nnum] > d[y][x][num] + g[ny][nx] * (die[5] + 1)) {
          d[ny][nx][nnum] = d[y][x][num] + g[ny][nx] * (die[5] + 1);
          q.push(P2(P(d[ny][nx][nnum], nnum), P(ny, nx)));
        }
      }
    }

    int m = 10000000;
    for (int i = 0; i < 25; i++)
      m = min(m, d[gy][gx][i]);
    cout << m << endl;
  }
}

Error: TLE

Fixed submission:

#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
using namespace std;

typedef pair<int, int> P;
typedef pair<P, P> P2;

int h, w;
int g[30][30];
int d[30][30][25];

int dy[] = {-1, 0, 1, 0}, dx[] = {0, 1, 0, -1};

int r[4][6] = {{1, 5, 2, 3, 0, 4},
               {3, 1, 0, 5, 4, 2},
               {4, 0, 2, 3, 5, 1},
               {2, 1, 5, 0, 4, 3}};

vector<int> trans(int n) {
  vector<int> res;
  while (n > 0) {
    res.push_back(n % 6);
    n /= 6;
  }
  while (res.size() < 6)
    res.push_back(0);
  return res;
}

int rot(int n, int dir) {
  int tmp[6];
  vector<int> die = trans(n);
  for (int i = 0; i < 6; i++)
    tmp[i] = die[r[dir][i]];

  int res = 0, k = 1;
  for (int i = 0; i < 6; i++) {
    res += tmp[i] * k;
    k *= 6;
  }
  return res;
}

int main() {
  while (cin >> h >> w, h || w) {
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++)
        cin >> g[i][j];
    }

    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        for (int k = 0; k < 25; k++)
          d[i][j][k] = 10000000;
      }
    }

    int sy, sx, gy, gx;
    cin >> sy >> sx >> gy >> gx;

    priority_queue<P2> q;
    map<int, int> m1, m2;
    m1[44790] = 1;
    m2[1] = 44790;
    q.push(P2(P(0, 1), P(sy, sx)));
    d[sy][sx][1] = 0;
    int cnt = 2;

    while (!q.empty()) {
      P2 p = q.top();
      q.pop();
      int s = p.first.first, num = p.first.second;
      int y = p.second.first, x = p.second.second;

      // cout << num << endl;

      for (int i = 0; i < 4; i++) {
        int ny = y + dy[i], nx = x + dx[i];
        if (ny < 0 || nx < 0 || h <= ny || w <= nx)
          continue;
        int nnum = rot(m2[num], i);

        vector<int> die = trans(nnum);
        if (!m1[nnum]) {
          m1[nnum] = cnt;
          m2[cnt] = nnum;
          cnt++;
        }
        nnum = m1[nnum];

        if (d[ny][nx][nnum] > d[y][x][num] + g[ny][nx] * (die[5] + 1)) {
          d[ny][nx][nnum] = d[y][x][num] + g[ny][nx] * (die[5] + 1);
          q.push(P2(P(d[ny][nx][nnum], nnum), P(ny, nx)));
        }
      }
    }

    int m = 10000000;
    for (int i = 0; i < 25; i++)
      m = min(m, d[gy][gx][i]);
    cout << m << endl;
  }
}