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

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

bool visit[11][11][51][11][11];

struct state {
  int x, y, l, r;
  int cost, oxy;
  state(int x, int y, int l, int r, int c, int o)
      : x(x), y(y), l(l), r(r), cost(c), oxy(o) {}
  bool operator<(const state &r) const { return cost > r.cost; }
};

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

int main() {
  int w, h;
  while (cin >> w >> h, w | h) {
    int f, m, o;
    cin >> f >> m >> o;
    vector<vector<int>> c(h, vector<int>(w));
    rep(i, h) rep(j, w) cin >> c[i][j];

    memset(visit, 0, sizeof(visit));

    priority_queue<state> q;
    rep(i, w) {
      state s(i, 0, i, i, 0, o - 1);
      if (s.oxy <= 0)
        continue;
      s.cost = max(-c[0][i], 0);
      s.oxy = max(c[0][i] + s.oxy, s.oxy);
      if (s.oxy > m)
        s.oxy = m;
      q.push(s);
    }
    int ans = -1;
    while (!q.empty()) {
      state s = q.top();
      q.pop();

      if (s.cost > f)
        continue;
      if (s.oxy <= 0)
        continue;

      if (visit[s.x][s.y][s.oxy][s.l][s.r])
        continue;
      visit[s.x][s.y][s.oxy][s.l][s.r] = 1;

      if (s.y == h - 1) {
        ans = s.cost;
        break;
      }
      if (s.oxy <= 1)
        continue;

      rep(i, 3) {
        int x = s.x + dx[i];
        int y = s.y + dy[i];

        if (x < 0 || x >= w || y < 0 || y >= h)
          continue;

        state next = s;
        next.x = x;
        next.y = y;
        next.oxy--;
        if (s.y != next.y) {
          next.r = -1;
          next.l = 11;
        }
        if (x < next.l || x > next.r) {
          next.r = max(next.r, x);
          next.l = min(next.l, x);
          if (c[y][x] > 0) {
            next.oxy = min(next.oxy + c[y][x], m);
          } else {
            next.cost -= c[y][x];
          }
        }
        q.push(next);
      }
    }
    if (ans != -1)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }

  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define rep(i, n) for (int i = 0; i < (n); i++)
#define FOR(i, a, b) for (int i = (a); i < (b); i++)
#define all(v) (v).begin(), (v).end()
#define rev(s) (s).rbegin(), (s).rend()
#define MP make_pair
#define X first
#define Y second

using namespace std;

typedef long long ll;
typedef pair<int, int> P;
typedef vector<int> vi;

struct state {
  int x;
  int y;
  int oxy;
  int cost;
  int row;
  state(int x, int y, int o, int c, int r)
      : x(x), y(y), oxy(o), cost(c), row(r) {}
  void pp() {
    printf("x=%d, y=%d, oxy=%d, cost=%d, row=%d\\n", x, y, oxy, cost, row);
  }
  bool operator<(const state &r) const { return cost > r.cost; }
};

int visit[11][11][51][1 << 12];

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

int main() {
  int W, H;
  while (cin >> W >> H, W | H) {
    int f, m, o;
    cin >> f >> m >> o;
    vector<vector<int>> c(H, vector<int>(W));
    rep(i, H) rep(j, W) cin >> c[i][j];

    priority_queue<state> q;
    rep(i, W) {
      int cost = -c[0][i];
      int oxy = o - 1;
      if (c[0][i] > 0) {
        cost = 0;
        oxy = min(m, oxy + c[0][i]);
      }
      q.push(state(i, 0, oxy, cost, 1 << i));
    }

    memset(visit, 0, sizeof(visit));

    int ans = -1;
    while (!q.empty()) {
      state s = q.top();
      q.pop();

      if (visit[s.x][s.y][s.oxy][s.row])
        continue;
      visit[s.x][s.y][s.oxy][s.row] = 1;

      if (s.y == H - 1) {
        ans = s.cost;
        break;
      }
      if (s.oxy <= 1)
        continue;

      rep(i, 3) {
        int nx = s.x + dx[i];
        int ny = s.y + dy[i];
        if (nx < 0 || nx >= W || ny < 0 || ny >= H)
          continue;
        state next = s;
        next.x = nx;
        next.y = ny;
        if (s.y != ny)
          next.row = 0;
        next.oxy--;
        if (!((next.row >> next.x) & 1)) {
          if (c[ny][nx] < 0)
            next.cost -= c[ny][nx];
          else
            next.oxy = min(m, next.oxy + c[ny][nx]);
        }
        next.row |= (1 << next.x);
        if (next.oxy && next.cost <= f)
          q.push(next);
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }
}