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

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define reep(i, s, n) for (int i = s; i < (int)n; i++)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
#define tyd typedef
#define vc vector
#define pb push_Back
#define eb emplace_back
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int IINF = 1 << 28;

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

int field[16][16];
int h, w;
int f, m, o;

struct Node {
  int x, y, cost, oxy, v;
  Node(int x, int y, int cost, int oxy, int v)
      : x(x), y(y), cost(cost), oxy(oxy), v(v) {}

  friend bool operator<(const Node &n1, const Node &n2) {
    return n1.cost > n2.cost;
  }
};

bool visited[15][15][55][2054];
int dijkstra() {
  priority_queue<Node> que;
  if (o == 1)
    return IINF;
  rep(i, w) {
    int cost = 0, oxy = o;
    if (field[0][i] < 0) {
      cost += -field[0][i];
    } else {
      oxy += field[0][i];
    }
    que.emplace(i, 0, cost, min(oxy - 1, m), 1 << i);
  }

  memset(visited, 0, sizeof(visited));

  int ret = IINF;
  while (!que.empty()) {
    Node n = que.top();
    que.pop();
    if (n.oxy <= 0)
      continue;
    if (n.cost > f) {
      break;
    }
    if (n.y == h - 1) {
      if (n.oxy > 0) {
        return n.cost;
      } else {
        continue;
      }
    }
    if (visited[n.y][n.x][n.oxy][n.v]) {
      continue;
    }
    visited[n.y][n.x][n.oxy][n.v] = true;
    if (n.oxy <= 1)
      continue;
    rep(i, 3) {
      int nx = n.x + dx[i];
      int ny = n.y + dy[i];
      int nv = n.v;
      if (nx < 0 || nx >= w) {
        continue;
      }
      if (ny != n.y)
        nv = 0;
      int cost = n.cost;
      int oxy = n.oxy - 1;

      if ((nv & (1 << nx)) == 0) {
        if (field[ny][nx] > 0) {
          oxy += field[ny][nx];
        } else {
          cost += -field[ny][nx];
        }
      }
      que.emplace(nx, ny, cost, min(oxy, m), nv | (1 << nx));
    }
  }

  return ret;
}

int main() {
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> f >> m >> o;
    rep(i, h) {
      rep(j, w) { cin >> field[i][j]; }
    }

    int ans = IINF;
    ans = min(ans, dijkstra());
    if (ans == IINF || ans > f) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <complex>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <tuple>
#include <vector>
using namespace std;

#define reep(i, s, n) for (int i = s; i < (int)n; i++)
#define rep(i, n) for (int i = 0; i < (int)n; i++)
#define REP(i, n) for (int i = 0; i < (int)n; i++)
#define all(v) v.begin(), v.end()
#define tyd typedef
#define vc vector
#define pb push_Back
#define eb emplace_back
#define mp make_pair

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef pair<int, int> pii;
const int IINF = 1 << 28;

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

int field[16][16];
int h, w;
int f, m, o;

struct Node {
  int x, y, cost, oxy, v;
  Node(int x, int y, int cost, int oxy, int v)
      : x(x), y(y), cost(cost), oxy(oxy), v(v) {}

  friend bool operator<(const Node &n1, const Node &n2) {
    return n1.cost > n2.cost;
  }
};

bool visited[20][20][55][3024];
int dijkstra() {
  priority_queue<Node> que;
  rep(i, w) {
    int cost = 0, oxy = o;
    if (field[0][i] < 0) {
      cost += -field[0][i];
    } else {
      oxy += field[0][i];
    }
    que.emplace(i, 0, cost, min(oxy - 1, m), 1 << i);
  }

  memset(visited, 0, sizeof(visited));

  int ret = IINF;
  while (!que.empty()) {
    Node n = que.top();
    que.pop();
    if (n.oxy <= 0)
      continue;
    if (n.cost > f) {
      break;
    }
    if (n.y == h - 1) {
      if (n.oxy > 0) {
        return n.cost;
      } else {
        continue;
      }
    }
    if (visited[n.y][n.x][n.oxy][n.v]) {
      continue;
    }
    visited[n.y][n.x][n.oxy][n.v] = true;
    if (n.oxy <= 1)
      continue;
    rep(i, 3) {
      int nx = n.x + dx[i];
      int ny = n.y + dy[i];
      int nv = n.v;
      if (nx < 0 || nx >= w) {
        continue;
      }
      if (ny != n.y)
        nv = 0;
      int cost = n.cost;
      int oxy = n.oxy - 1;

      if ((nv & (1 << nx)) == 0) {
        if (field[ny][nx] > 0) {
          oxy += field[ny][nx];
        } else {
          cost += -field[ny][nx];
        }
      }
      que.emplace(nx, ny, cost, min(oxy, m), nv | (1 << nx));
    }
  }

  return ret;
}

int main() {
  while (cin >> w >> h) {
    if (!w && !h)
      break;
    cin >> f >> m >> o;
    rep(i, h) {
      rep(j, w) { cin >> field[i][j]; }
    }

    int ans = IINF;
    ans = min(ans, dijkstra());
    if (ans == IINF || ans > f) {
      cout << "NA" << endl;
    } else {
      cout << ans << endl;
    }
  }
  return 0;
}