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

#include <bits/stdc++.h>

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

using namespace std;

const int IINF = INT_MAX;

struct Data {
  int x, y, oxygen, left, right, money;
  bool operator<(const Data &data) const {
    if (money != data.money)
      return money > data.money;
    return oxygen < data.oxygen;
  }
};

int H, W, f, m, o, c[20][20], dp[20][20][60][20][20];
int dx[] = {-1, +1};
int dy[] = {+0, +0};

bool isValid(int x, int y) { return 0 <= x && x < W && 0 <= y && y <= H; }

int main() {
  while (cin >> W >> H, W | H) {
    cin >> f >> m >> o;
    rep(i, W) c[0][i] = 0;
    rep(i, H) rep(j, W) cin >> c[i + 1][j];
    rep(i, H + 2) rep(j, W) rep(k, m + 1) rep(y, W) rep(x, W)
        dp[i][j][k][y][x] = IINF;
    priority_queue<Data> Q;
    rep(i, W) {
      dp[0][i][o][i][i] = 0;
      Q.push((Data){i, 0, o, i, i, 0});
    }

    int ans = IINF;
    while (!Q.empty()) {
      Data data = Q.top();
      Q.pop();

      if (data.y == H) {
        ans = data.money;
        break;
      }
      if (data.oxygen <= 1)
        continue;
      int oxygen = data.oxygen - 1;
      assert(oxygen >= 1);
      rep(i, 2) {
        int nx = data.x + dx[i], ny = data.y + dy[i], left = data.left,
            right = data.right;
        if (!isValid(nx, ny))
          continue;
        int nmoney = data.money;
        int noxygen = oxygen;
        if (left > nx || right < nx) {
          if (c[ny][nx] < 0)
            nmoney -= c[ny][nx];
          else
            noxygen += c[ny][nx];
        }
        noxygen = min(noxygen, m);
        if (dp[ny][nx][noxygen][min(left, nx)][max(right, nx)] > nmoney) {
          dp[ny][nx][noxygen][min(left, nx)][max(right, nx)] = nmoney;
          Q.push(
              (Data){nx, ny, noxygen, min(left, nx), max(right, nx), nmoney});
        }
      }

      int nx = data.x, ny = data.y + 1, nmoney = data.money, noxygen = oxygen;
      if (c[ny][nx] < 0)
        nmoney -= c[ny][nx];
      else
        noxygen += c[ny][nx];
      noxygen = min(noxygen, m);
      if (dp[ny][nx][noxygen][nx][nx] > nmoney) {
        dp[ny][nx][noxygen][nx][nx] = nmoney;
        Q.push((Data){nx, ny, noxygen, nx, nx, nmoney});
      }
    }
    if (ans == IINF || ans > f)
      puts("NA");
    else
      cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <deque>
#include <iostream>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
int dx[] = {0, -1, 1};
int dy[] = {1, 0, 0};

struct Pox {
  int x, y, f, o;
  Pox(int x, int y, int f, int o) : x(x), y(y), f(f), o(o) {}
};

int main() {
  int W, H;
  while (cin >> W >> H, W | H) {
    int f, m, o;
    cin >> f >> m >> o;
    vector<vector<int>> G;
    G.resize(H + 1);
    for (int i = 0; i < H; i++) {
      G[i].resize(W);
      rep(j, W) cin >> G[i][j];
    }

    int mincost[H + 1][W][51];
    rep(i, H + 1) rep(j, W) rep(k, 51) mincost[i][j][k] = (1 << 29);

    deque<Pox> deq;
    rep(i, W) if (G[0][i] <= 0 && -G[0][i] <= f && o - 1 > 0) {
      deq.push_back(Pox(i, 0, -G[0][i], o - 1)),
          mincost[0][i][o - 1] = -G[0][i];
    }
    else if (G[0][i] > 0 && o - 1 > 0)
        deq.push_back(Pox(i, 0, 0, min(o - 1 + G[0][i], m))),
        mincost[0][i][min(o - 1 + G[0][i], m)] = 0;

    int ans = (1 << 29);
    while (!deq.empty()) {
      Pox pox = deq.front();
      deq.pop_front();
      int x = pox.x;
      int y = pox.y;
      if (ans <= pox.f)
        continue;
      if (y >= H - 1) {
        ans = min(ans, pox.f);
        continue;
      }

      for (int i = 0; i < 3; i++) {
        int nx = x + dx[i];
        int ny = y + dy[i];
        if (!(0 <= nx && nx < W && ny < H))
          continue;

        if (G[ny][nx] <= 0) {
          if (pox.f - G[ny][nx] <= f && pox.o - 1 > 0 &&
              mincost[ny][nx][pox.o - 1] > pox.f - G[ny][nx]) {
            mincost[ny][nx][pox.o - 1] = pox.f - G[ny][nx];
            deq.push_back(Pox(nx, ny, pox.f - G[ny][nx], pox.o - 1));
          }
        } else {
          if (pox.o - 1 > 0 && mincost[ny][nx][pox.o - 1] > pox.f) {
            mincost[ny][nx][min(pox.o - 1 + G[ny][nx], m)] = pox.f;
            deq.push_back(Pox(nx, ny, pox.f, min(pox.o - 1 + G[ny][nx], m)));
          }
        }
      }
    }
    ans == (1 << 29) ? cout << "NA" << endl : cout << ans << endl;
  }
  return 0;
}