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

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int w, h, f, m, o;
vector<vector<int>> cell;

class Data {
public:
  int y, x, left, right, o;
  Data(int y0, int x0, int left0, int right0, int o0) {
    y = y0;
    x = x0;
    left = left0;
    right = right0;
    o = o0;
  }
  int toInt() { return (((y * w + x) * w + left) * w + right) * (m + 1) + o; }
};

int solve() {
  vector<int> dp((h + 1) * w * w * w * (m + 1), INT_MAX);
  multimap<int, Data> mm;

  for (int i = 0; i < w; ++i) {
    Data d(0, i, 0, w - 1, o);
    dp[d.toInt()] = 0;
    mm.insert(make_pair(0, d));
  }

  while (!mm.empty()) {
    int cost = mm.begin()->first;
    Data d = mm.begin()->second;
    mm.erase(mm.begin());

    if (cost > dp[d.toInt()])
      continue;
    if (d.y == h)
      return cost;

    for (int i = -1; i <= 1; ++i) {
      int cost1 = cost;
      Data d1 = d;
      --d1.o;

      d1.x += i;
      if (d1.x < 0 || d1.x >= w || d1.o <= 0)
        continue;

      if (i == 0) {
        ++d1.y;
        d1.left = d1.x;
        d1.right = d1.x - 1;
      }

      if (d1.x < d1.left || d1.x > d1.right) {
        d1.left = min(d1.left, d1.x);
        d1.right = max(d1.right, d1.x);
        if (cell[d1.y][d1.x] < 0) {
          cost1 += -cell[d1.y][d1.x];
        } else {
          d1.o += cell[d1.y][d1.x];
          d1.o = min(d1.o, m);
        }
      }

      if (cost1 < dp[d1.toInt()]) {
        dp[d1.toInt()] = cost1;
        mm.insert(make_pair(cost1, d1));
      }
    }
  }

  return INT_MAX;
}

int main() {
  for (;;) {
    cin >> w >> h >> f >> m >> o;
    if (w == 0 && h == 0)
      return 0;

    cell.assign(h + 1, vector<int>(w, 0));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> cell[i + 1][j];
      }
    }

    int ret = solve();
    if (ret > f)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

int w, h, f, m, o;
vector<vector<int>> cell;

class Data {
public:
  int y, x, left, right, o;
  Data(int y0, int x0, int left0, int right0, int o0) {
    y = y0;
    x = x0;
    left = left0;
    right = right0;
    o = o0;
  }
  int toInt() { return (((y * w + x) * w + left) * w + right) * (m + 1) + o; }
  bool operator<(const Data &d) const { return y < d.y; }
};

int solve() {
  vector<int> dp((h + 1) * w * w * w * (m + 1), INT_MAX);
  multimap<int, Data> mm;

  for (int i = 0; i < w; ++i) {
    Data d(0, i, 0, w - 1, o);
    dp[d.toInt()] = 0;
    mm.insert(make_pair(0, d));
  }

  while (!mm.empty()) {
    int cost = mm.begin()->first;
    Data d = mm.begin()->second;
    mm.erase(mm.begin());

    if (cost > dp[d.toInt()])
      continue;
    if (d.y == h)
      return cost;

    for (int i = -1; i <= 1; ++i) {
      int cost1 = cost;
      Data d1 = d;
      --d1.o;

      d1.x += i;
      if (d1.x < 0 || d1.x >= w || d1.o == 0)
        continue;

      if (i == 0) {
        ++d1.y;
        d1.left = d1.x;
        d1.right = d1.x - 1;
      }

      if (d1.x < d1.left || d1.x > d1.right) {
        d1.left = min(d1.left, d1.x);
        d1.right = max(d1.right, d1.x);
        if (cell[d1.y][d1.x] < 0) {
          cost1 += -cell[d1.y][d1.x];
        } else {
          d1.o += cell[d1.y][d1.x];
          d1.o = min(d1.o, m);
        }
      }

      if (cost1 < dp[d1.toInt()]) {
        dp[d1.toInt()] = cost1;
        mm.insert(make_pair(cost1, d1));
      }
    }
  }

  return INT_MAX;
}

int main() {
  for (;;) {
    cin >> w >> h >> f >> m >> o;
    if (w == 0 && h == 0)
      return 0;

    cell.assign(h + 1, vector<int>(w, 0));
    for (int i = 0; i < h; ++i) {
      for (int j = 0; j < w; ++j) {
        cin >> cell[i + 1][j];
      }
    }

    int ret = solve();
    if (ret > f)
      cout << "NA" << endl;
    else
      cout << ret << endl;
  }
}