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

#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 29;

int bed[10][10], maxair;

struct Info {
  int y, x, air, l, r, cost;
  Info(int Y, int X, int A, int L, int R, int C)
      : y(Y), x(X), air(A), l(L), r(R), cost(C) {}
  bool operator>(const Info &in) const { return cost > in.cost; }
};

int refillAir(int air, int x, int y) {
  if (air == 0)
    return 0;
  return min(air + bed[y][x], maxair);
}

int main() {
  for (int w, h; scanf("%d%d", &w, &h), w;) {
    int budget, iniair;
    scanf("%d%d%d", &budget, &maxair, &iniair);
    rep(i, h) rep(j, w) scanf("%d", bed[i] + j);

    int ans = -1;
    static int cmin[10][10][51][10][10];
    rep(y, h) rep(x, w) rep(a, maxair + 1) rep(r, w) rep(l, r + 1)
        cmin[y][x][a][l][r] = INF;
    priority_queue<Info, vector<Info>, greater<Info>> pq;
    rep(x, w) {
      int nextair, nextcost;
      if (bed[0][x] < 0) {
        nextair = iniair - 1;
        nextcost = -bed[0][x];
      } else {
        nextair = refillAir(iniair - 1, x, 0);
        nextcost = 0;
      }
      cmin[0][x][nextair][x][x] = nextcost;
      pq.push(Info(0, x, nextair, x, x, nextcost));
    }
    while (!pq.empty()) {
      Info a = pq.top();
      pq.pop();
      int y = a.y, x = a.x, air = a.air, l = a.l, r = a.r, cost = a.cost;
      if (air <= 0 || cost > budget)
        continue;
      if (y == h - 1) {
        ans = cost;
        break;
      }

      if (cmin[y][x][air][l][r] < cost)
        continue;

      int nextair, nextcost;
      if (bed[y + 1][x] < 0) {
        nextair = air - 1;
        nextcost = cost - bed[y + 1][x];
      } else {
        nextair = refillAir(air - 1, x, y + 1);
        nextcost = cost;
      }
      if (nextcost < cmin[y + 1][x][nextair][x][x]) {
        cmin[y + 1][x][nextair][x][x] = nextcost;
        pq.push(Info(y + 1, x, nextair, x, x, nextcost));
      }

      if (x > 0) {
        int nextl;
        if (x == l) {
          nextl = x - 1;
          if (bed[y][x - 1] < 0) {
            nextair = air - 1;
            nextcost = cost - bed[y][x - 1];
          } else {
            nextair = refillAir(air - 1, x - 1, y);
            nextcost = cost;
          }
        } else {
          nextl = l;
          nextair = air - 1;
          nextcost = cost;
        }
        if (nextcost < cmin[y][x - 1][nextair][nextl][r]) {
          cmin[y][x - 1][nextair][nextl][r] = nextcost;
          pq.push(Info(y, x - 1, nextair, nextl, r, nextcost));
        }
      }

      if (x < w - 1) {
        int nextr;
        if (x == r) {
          nextr = x + 1;
          if (bed[y][x + 1] < 0) {
            nextair = air - 1;
            nextcost = cost - bed[y][x + 1];
          } else {
            nextair = refillAir(air - 1, x + 1, y);
            nextcost = cost;
          }
        } else {
          nextr = r;
          nextair = air - 1;
          nextcost = cost;
        }
        if (nextcost < cmin[y][x + 1][nextair][l][nextr]) {
          cmin[y][x + 1][nextair][l][nextr] = nextcost;
          pq.push(Info(y, x + 1, nextair, l, nextr, nextcost));
        }
      }
    }

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

  return 0;
}

Error: MLE

Fixed submission:

#include <cstdio>
#include <queue>

#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;

const int INF = 1 << 29;

struct Info {
  int y, x, air, l, r, cost;
  Info(int Y, int X, int A, int L, int R, int C)
      : y(Y), x(X), air(A), l(L), r(R), cost(C) {}
  bool operator>(const Info &in) const { return cost > in.cost; }
};

int main() {
  for (int w, h; scanf("%d%d", &w, &h), w;) {
    int budget, maxair, iniair;
    scanf("%d%d%d", &budget, &maxair, &iniair);
    int bed[10][10];
    rep(i, h) rep(j, w) scanf("%d", bed[i] + j);

    int ans = -1;
    static int cmin[10][10][51][10][10];
    rep(y, h) rep(x, w) rep(a, maxair + 1) rep(r, w) rep(l, r + 1)
        cmin[y][x][a][l][r] = INF;
    priority_queue<Info, vector<Info>, greater<Info>> pq;
    rep(x, w) {
      if (bed[0][x] < 0)
        pq.push(Info(0, x, iniair - 1, x, x, -bed[0][x]));
      else
        pq.push(Info(0, x, min(iniair - 1 + bed[0][x], maxair), x, x, 0));
    }
    while (!pq.empty()) {
      Info a = pq.top();
      pq.pop();
      int y = a.y, x = a.x, air = a.air, l = a.l, r = a.r, cost = a.cost;
      if (air <= 0 || cost > budget)
        continue;
      if (y == h - 1) {
        ans = cost;
        break;
      }

      if (cmin[y][x][air][l][r] < cost)
        continue;
      cmin[y][x][air][l][r] = cost;

      int nextair, nextcost;
      if (bed[y + 1][x] < 0) {
        nextair = air - 1;
        nextcost = cost - bed[y + 1][x];
      } else {
        nextair = min(air - 1 + bed[y + 1][x], maxair);
        nextcost = cost;
      }
      pq.push(Info(y + 1, x, nextair, x, x, nextcost));

      if (x > 0) {
        if (x == l) {
          if (bed[y][x - 1] < 0) {
            nextair = air - 1;
            nextcost = cost - bed[y][x - 1];
          } else {
            nextair = min(air - 1 + bed[y][x - 1], maxair);
            nextcost = cost;
          }
        } else {
          nextair = air - 1;
          nextcost = cost;
        }
        pq.push(Info(y, x - 1, nextair, min(l, x - 1), r, nextcost));
      }

      if (x < w - 1) {
        if (x == r) {
          if (bed[y][x + 1] < 0) {
            nextair = air - 1;
            nextcost = cost - bed[y][x + 1];
          } else {
            nextair = min(air - 1 + bed[y][x + 1], maxair);
            nextcost = cost;
          }
        } else {
          nextair = air - 1;
          nextcost = cost;
        }
        pq.push(Info(y, x + 1, nextair, l, max(r, x + 1), nextcost));
      }
    }

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

  return 0;
}