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

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int w, h;
struct P {
  int x, y;
  int cost;
  int i;
  bool move() {
    x += dx[i];
    y += dy[i];
    if (x <= 0 || y <= 0 || w < x || h < y)
      return false;
    return true;
  }
  void turn_right() {
    i += 1;
    i %= 4;
  }
  void turn_left() {
    i += 3;
    i %= 4;
  }
  void turn_back() {
    i += 2;
    i %= 4;
  }
  bool operator<(const P &right) const { return cost < right.cost; }
  bool operator>(const P &right) const { return cost > right.cost; }
};

int main() {
  while (true) {
    vector<vi> a(35, vi(35, 4));
    vi cost(4, 0);
    int flag[35][35][4];

    cin >> w >> h;
    if (!w)
      break;
    FORI(i, 1, h + 1) FORI(j, 1, w + 1) cin >> a[i][j];
    FOR(i, 4)
    cin >> cost[i];
    FOR(i, 35) FOR(j, 35) FOR(k, 4) flag[i][j][k] = INT_MAX;

    P start{1, 1, 0, 1};
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(start);
    while (!que.empty()) {
      P now = que.top();
      que.pop();
      if (now.x == w && now.y == h) {
        cout << now.cost << endl;
        break;
      }
      FOR(i, 4) {
        P next = now;
        if (a[now.y][now.x] == i)
          continue;
        next.cost += cost[i];
        switch (i) {
        case 0:
          break;
        case 1:
          next.turn_right();
          break;
        case 2:
          next.turn_back();
          break;
        case 3:
          next.turn_left();
          break;
        }
        if (next.move() && flag[next.y][next.x][next.i] > next.cost) {
          flag[next.y][next.x][next.i] = next.cost;
          que.push(next);
        }
      }
      bool ok = true;
      switch (a[now.y][now.x]) {
      case 1:
        now.turn_right();
        break;
      case 2:
        now.turn_back();
        break;
      case 3:
        now.turn_left();
        break;
      case 4:
        ok = false;
        break;
      }
      if (!ok)
        continue;
      if (now.move() && flag[now.y][now.x][now.i] > now.cost) {
        flag[now.y][now.x][now.i] = now.cost;
        que.push(now);
      }
    }
  }
}

Error: MLE

Fixed submission:

#define _CRT_SECURE_NO_WARNINGS 1
#define _USE_MATH_DEFINES
#include <algorithm>
#include <bitset>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <forward_list>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
#define INF ((int)(1 << 25))
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define FOR(i, n) REP(i, n)
#define FORI(i, k, n) for (int i = k; i < (int)n; i++)
#define TORAD 2.0 * M_PI / 360.0
#define INT(x)                                                                 \\
  int x;                                                                       \\
  cin >> x;
#define STR(x)                                                                 \\
  string x;                                                                    \\
  cin >> x;
#define ALL(x) begin(x), end(x)
#define RALL(x) (x).rbegin(), (x).rend()
#define DEBUG(x) cout << #x << ":" << x << endl;
#define EPS 1e-12
#ifdef _MSC_VER
#include <intrin.h>
#define __builtin_popcount __popcnt
#endif
typedef long long lint;
typedef vector<bool> vb;
typedef vector<char> vc;
typedef vector<int> vi;
typedef vector<lint> vl;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef pair<int, pii> piii;
const int dx[4] = {0, 1, 0, -1}, dy[4] = {-1, 0, 1, 0};
template <class T> void debug(T a) {
  for (auto i : a)
    cout << i << endl;
}

int w, h;
struct P {
  int x, y;
  int cost;
  int i;
  set<piii> a;
  bool move() {
    x += dx[i];
    y += dy[i];
    if (a.count(piii(i, pii(x, y))) || x <= 0 || y <= 0 || w < x || h < y)
      return false;
    a.insert(piii(i, pii(x, y)));
    return true;
  }
  void turn_right() {
    i += 1;
    i %= 4;
  }
  void turn_left() {
    i += 3;
    i %= 4;
  }
  void turn_back() {
    i += 2;
    i %= 4;
  }
  bool operator<(const P &right) const { return cost < right.cost; }
  bool operator>(const P &right) const { return cost > right.cost; }
};

int main() {
  while (true) {

    vector<vi> a(35, vi(35, 4));
    vi cost(4, 0);

    cin >> w >> h;
    if (!w)
      break;
    FORI(i, 1, h + 1) FORI(j, 1, w + 1) cin >> a[i][j];
    FOR(i, 4)
    cin >> cost[i];
    P start{1, 1, 0, 1, {}};
    priority_queue<P, vector<P>, greater<P>> que;
    que.push(start);
    while (!que.empty()) {
      P now = que.top();
      que.pop();
      if (now.x == w && now.y == h) {
        cout << now.cost << endl;
        break;
      }
      FOR(i, 4) {
        P next = now;
        if (a[now.y][now.x] == i)
          continue;
        next.cost += cost[i];
        switch (i) {
        case 0:
          break;
        case 1:
          next.turn_right();
          break;
        case 2:
          next.turn_back();
          break;
        case 3:
          next.turn_left();
          break;
        }
        if (next.move()) {
          que.push(next);
        }
      }
      bool ok = true;
      switch (a[now.y][now.x]) {
      case 1:
        now.turn_right();
        break;
      case 2:
        now.turn_back();
        break;
      case 3:
        now.turn_left();
        break;
      case 4:
        ok = false;
        break;
      }
      if (!ok)
        continue;
      if (now.move()) {
        que.push(now);
      }
    }
  }
}