Problem p00234 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <queue>
#include <string>
using namespace std;
int W, H, m, f, MAP[15][15];
int DP[11][11][55][11][11];
struct state {
  int x, y, s, e, o, f;
  state(int xx, int yy, int oo, int ss, int ee, int ff) {
    x = xx;
    y = yy;
    s = ss;
    e = ee;
    o = oo;
    f = ff;
  }
  state() {}
};
bool operator>(state a, state b) { return a.f > b.f; }
int solve(int o) {
  memset(DP, 0x10, sizeof(DP));
  if (o <= 1)
    return -1;

  priority_queue<state, vector<state>, greater<state>> que;
  for (int i = 0; i < W; i++) {
    int no = o - 1;
    int now = 0;
    if (MAP[i][0] > 0) {
      no += MAP[i][0];
      if (no > m)
        no = m;
    } else {
      now -= MAP[i][0];
    }
    DP[i][0][no][i][i] = now;
    que.push(state(i, 0, no, i, i, now));
  }
  state temp;
  while (!que.empty()) {
    temp = que.top();
    que.pop();
    temp.o--;
    state nstate = temp;
    if (nstate.y == H - 1) {
      return nstate.f;
    }
    if (nstate.o == 0)
      continue;

    nstate.y += 1;
    nstate.s = nstate.x;
    nstate.e = nstate.x;
    if (MAP[nstate.x][nstate.y] > 0) {
      nstate.o += MAP[nstate.x][nstate.y];
      if (nstate.o > m)
        nstate.o = m;
    } else {
      nstate.f -= MAP[nstate.x][nstate.y];
    }
    if (DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] > nstate.f) {
      DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] = nstate.f;
      que.push(nstate);
    }

    nstate = temp;
    nstate.x += 1;
    if (nstate.x < W) {
      if (nstate.e < nstate.x) {
        nstate.e = nstate.x;
        if (MAP[nstate.x][nstate.y] > 0) {
          nstate.o += MAP[nstate.x][nstate.y];
          if (nstate.o > m)
            nstate.o = m;
        } else {
          nstate.f -= MAP[nstate.x][nstate.y];
        }
      }
      if (DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] > nstate.f) {
        DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] = nstate.f;
        que.push(nstate);
      }
    }
    nstate = temp;
    nstate.x -= 1;
    if (nstate.x >= 0) {
      if (nstate.s > nstate.x) {
        nstate.s = nstate.x;
        if (MAP[nstate.x][nstate.y] > 0) {
          nstate.o += MAP[nstate.x][nstate.y];
          if (nstate.o > m)
            nstate.o = m;
        } else {
          nstate.f -= MAP[nstate.x][nstate.y];
        }
      }
      if (DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] > nstate.f) {
        DP[nstate.x][nstate.y][nstate.o][nstate.s][nstate.e] = nstate.f;
        que.push(nstate);
      }
    }
  }
  return -1;
}

int main() {
  for (; scanf("%d%d", &W, &H), W;) {
    int o = 0;
    scanf("%d%d%d", &f, &m, &o);

    for (int y = 0; y < H; y++)
      for (int x = 0; x < W; x++)
        scanf("%d", &MAP[x][y]);
    int res = solve(o);
    if (res == -1 || res > f) {
      puts("NA");
    } else {
      printf("%d\\n", res);
    }
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <queue>

using namespace std;

#define inf (1 << 27)

struct State {
  int x, y, l, r, o, c;
  State(int xx, int yy, int ll, int rr, int oo, int cc) {
    x = xx, y = yy, l = ll, r = rr, o = oo, c = cc;
  }
};

bool operator>(const State &left, const State &right) {
  return left.c > right.c;
}

int map[10][10];
int cost[10][10][10][10][51];

int main() {
  int W, H, f, m, o;
  while (scanf("%d %d", &W, &H), W) {
    fill(cost[0][0][0][0], cost[0][0][0][0] + 10 * 10 * 10 * 10 * 51, inf);
    scanf("%d %d %d", &f, &m, &o);
    for (int j = 0; j < H; j++)
      for (int i = 0; i < W; i++) {
        scanf("%d", &map[i][j]);
      }
    if (o == 1) {
      printf("NA\\n");
      continue;
    }
    priority_queue<State, vector<State>, greater<State>> que;
    for (int i = 0; i < W; i++) {
      int oo = o - 1, cc = 0;
      if (map[i][0] < 0) {
        cc -= map[i][0];
      } else {
        oo = map[i][0];
        if (oo > m)
          oo = m;
      }
      que.push(State(i, 0, i, i, oo, cc));
    }
    int res = inf;
    while (!que.empty()) {
      State s = que.top();
      que.pop();
      s.o--;
      if (s.y == (H - 1)) {
        res = s.c;
        break;
      }
      if (s.o == 0) {
        continue;
      }
      if (cost[s.x][s.y][s.l][s.r][s.o] < s.c) {
        continue;
      }
      State ns = s;
      ns.y++;
      ns.l = ns.r = ns.x;
      if (ns.y < H) {
        if (map[ns.x][ns.y] > 0) {
          ns.o += map[ns.x][ns.y];
          if (ns.o > m)
            ns.o = m;
        } else {
          ns.c -= map[ns.x][ns.y];
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
      ns = s;
      ns.x++;
      if (ns.x < W) {
        if (ns.x > ns.r) {
          ns.r = ns.x;
          if (map[ns.x][ns.y] > 0) {
            ns.o += map[ns.x][ns.y];
            if (ns.o > m)
              ns.o = m;
          } else {
            ns.c -= map[ns.x][ns.y];
          }
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
      ns = s;
      ns.x--;
      if (ns.x >= 0) {
        if (ns.l > ns.x) {
          ns.l = ns.x;
          if (map[ns.x][ns.y] > 0) {
            ns.o += map[ns.x][ns.y];
            if (ns.o > m)
              ns.o = m;
          } else {
            ns.c -= map[ns.x][ns.y];
          }
        }
        if (cost[ns.x][ns.y][ns.l][ns.r][ns.o] > ns.c) {
          cost[ns.x][ns.y][ns.l][ns.r][ns.o] = ns.c;
          que.push(ns);
        }
      }
    }
    if (res <= f)
      printf("%d\\n", res);
    else
      printf("NA\\n");
  }
}