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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int rol[4][4] = {{4, 5, 2, 0}, {1, 0, 3, 5}, {0, 2, 5, 4}, {5, 3, 0, 1}};
vi a;
void roll(int b) {
  int tmp = a[2 + rol[b][0]];
  rep(i, 3) a[2 + rol[b][i]] = a[2 + rol[b][i + 1]];
  a[2 + rol[b][3]] = tmp;
}
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vvi in(n, vi(m));
    rep(i, n) rep(j, m) cin >> in[i][j];
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    vi q(8);
    q[0] = sx;
    q[1] = sy;
    q[2] = 1;
    q[3] = 2;
    q[4] = 3;
    q[5] = 5;
    q[6] = 4;
    q[7] = 6;
    int ma[11][11][666];
    rep(i, 11) rep(j, 11) rep(k, 666) ma[i][j][k] = inf;
    ma[sx][sy][123] = 0;
    int out = inf;
    priority_queue<pair<int, vi>> que;
    que.push(pair<int, vi>(0, q));
    while (!que.empty()) {
      vi w = que.top().second;
      int t = -que.top().first;
      que.pop();
      if (ma[w[0]][w[1]][w[2] * 100 + w[3] * 10 + w[4]] < t)
        continue;
      rep(i, 4) {
        a = w;
        a[0] += dx[i];
        a[1] += dy[i];
        if (a[0] < 0 || a[1] < 0 || a[0] >= n || a[1] >= m)
          continue;
        roll(i);
        int cost = t + a[7] * in[a[0]][a[1]];
        if (ma[a[0]][a[1]][a[2] * 100 + a[3] * 10 + a[4]] > cost) {
          ma[a[0]][a[1]][a[2] * 100 + a[3] * 10 + a[4]] = cost;
          que.push(pair<int, vi>(-cost, a));
          if (a[0] == gx && a[1] == gy)
            out = min(out, cost);
        }
      }
    }
    cout << out << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#define loop(i, a, b) for (int i = a; i < b; i++)
#define rep(i, a) loop(i, 0, a)
#define pb push_back
#define mp make_pair
#define all(in) in.begin(), in.end()
#define shosu(x) fixed << setprecision(x)
using namespace std;
// kaewasuretyuui
typedef long long ll;
typedef pair<int, int> pii;
typedef vector<ll> vi;
typedef vector<vi> vvi;
typedef vector<pii> vp;
typedef vector<vp> vvp;
typedef vector<string> vs;
typedef vector<double> vd;
typedef pair<int, pii> pip;
typedef vector<pip> vip;
const double PI = acos(-1);
const double EPS = 1e-8;
const int inf = 1e8;
int dx[] = {0, 1, 0, -1};
int dy[] = {1, 0, -1, 0};
int rol[4][4] = {{4, 5, 2, 0}, {1, 0, 3, 5}, {0, 2, 5, 4}, {5, 3, 0, 1}};
vi a;
void roll(int b) {
  int tmp = a[2 + rol[b][0]];
  rep(i, 3) a[2 + rol[b][i]] = a[2 + rol[b][i + 1]];
  a[2 + rol[b][3]] = tmp;
}
int main() {
  int n, m;
  while (cin >> n >> m, n + m) {
    vvi in(n, vi(m));
    rep(i, n) rep(j, m) cin >> in[i][j];
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    vi q(8);
    q[0] = sx;
    q[1] = sy;
    q[2] = 1;
    q[3] = 2;
    q[4] = 3;
    q[5] = 5;
    q[6] = 4;
    q[7] = 6;
    map<vi, int> ma;
    int out = inf;
    priority_queue<pair<int, vi>> que;
    que.push(pair<int, vi>(0, q));
    while (!que.empty()) {
      vi w = que.top().second;
      int t = -que.top().first;
      que.pop();
      if (ma[w] < t)
        continue;
      rep(i, 4) {
        a = w;
        a[0] += dx[i];
        a[1] += dy[i];
        if (a[0] < 0 || a[1] < 0 || a[0] >= n || a[1] >= m)
          continue;
        roll(i);
        int cost = t + a[7] * in[a[0]][a[1]];
        if (ma[a] == 0 || ma[a] > cost) {
          ma[a] = cost;
          que.push(pair<int, vi>(-cost, a));
          if (a[0] == gx && a[1] == gy)
            out = min(out, cost);
        }
      }
    }
    cout << out << endl;
  }
}