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

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

struct node {
  int cost, x, y, o, hori;
  node(int cost, int x, int y, int o, int hori)
      : cost(cost), x(x), y(y), o(o), hori(hori) {}
};
bool operator<(const node &a, const node &b) { return a.cost > b.cost; }
int main() {
  int W, H, f, m, o, c[10][10];
  while (scanf("%d%d", &W, &H), W | H) {
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        scanf("%d", &c[i][j]);

    static int visit[10][10][1 << 10][51];
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        for (int k = 0; k < 1 << W; ++k)
          for (int l = 0; l <= m; ++l)
            visit[i][j][k][l] = false;

    int res = -1;
    priority_queue<node> q;
    for (int i = 0; i < W; ++i)
      q.push(node(0, i, 0, o, 0));
    while (!q.empty()) {
      node e = q.top();
      q.pop();
      if (e.cost > f)
        break;
      else if (e.x < 0 || e.x >= W ||
               (e.y != H && (e.o <= 1 || visit[e.y][e.x][e.hori][e.o])))
        continue;
      else if (e.y == H && e.o >= 1) {
        res = e.cost;
        break;
      }

      visit[e.y][e.x][e.hori][e.o] = true;
      --e.o;
      if (!(e.hori & (1 << e.x))) {
        e.hori |= 1 << e.x;
        if (c[e.y][e.x] < 0)
          e.cost -= c[e.y][e.x];
        else
          e.o = min(m, e.o + c[e.y][e.x]);
      }

      q.push(node(e.cost, e.x, e.y + 1, e.o, 0));
      q.push(node(e.cost, e.x - 1, e.y, e.o, e.hori));
      q.push(node(e.cost, e.x + 1, e.y, e.o, e.hori));
    }

    if (res != -1)
      printf("%d\\n", res);
    else
      puts("NA");
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <functional>
#include <iostream>
#include <map>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#ifdef _MSC_VER
#define __typeof__ decltype

template <class T> int __builtin_popcount(T n) {
  return n ? 1 + __builtin_popcount(n & (n - 1)) : 0;
}
#endif

#define foreach(it, c)                                                         \\
  for (__typeof__((c).begin()) it = (c).begin(); it != (c).end(); ++it)
#define all(c) (c).begin(), (c).end()
#define rall(c) (c).rbegin(), (c).rend()
#define popcount __builtin_popcount

const double EPS = 1e-10;
typedef long long ll;
typedef pair<int, int> pint;

struct node {
  int cost, x, y, o, hori;
  node(int cost, int x, int y, int o, int hori)
      : cost(cost), x(x), y(y), o(o), hori(hori) {}
};
bool operator<(const node &a, const node &b) { return a.cost > b.cost; }
int main() {
  int W, H, f, m, o, c[10][10];
  while (scanf("%d%d", &W, &H), W | H) {
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        scanf("%d", &c[i][j]);

    static int visit[10][10][1 << 10][51];
    for (int i = 0; i < H; ++i)
      for (int j = 0; j < W; ++j)
        for (int k = 0; k < 1 << W; ++k)
          for (int l = 0; l <= m; ++l)
            visit[i][j][k][l] = false;

    int res = -1;
    priority_queue<node> q;
    for (int i = 0; i < W; ++i)
      q.push(node(0, i, 0, o, 0));
    while (!q.empty()) {
      node e = q.top();
      q.pop();
      if (e.cost > f)
        break;
      else if (e.x < 0 || e.x >= W || e.o <= 0 ||
               (e.y != H && visit[e.y][e.x][e.hori][e.o]))
        continue;
      else if (e.y == H) {
        res = e.cost;
        break;
      }

      --e.o;
      visit[e.y][e.x][e.hori][e.o] = true;
      if (!(e.hori & (1 << e.x))) {
        e.hori |= 1 << e.x;
        if (c[e.y][e.x] < 0)
          e.cost -= c[e.y][e.x];
        else
          e.o = min(m, e.o + c[e.y][e.x]);
      }

      q.push(node(e.cost, e.x, e.y + 1, e.o, 0));
      q.push(node(e.cost, e.x - 1, e.y, e.o, e.hori));
      q.push(node(e.cost, e.x + 1, e.y, e.o, e.hori));
    }

    if (res != -1)
      printf("%d\\n", res);
    else
      puts("NA");
  }

  return 0;
}