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

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};

struct State {
  int d, o, y, x, l, r;
  bool operator<(const State &right) const {
    return d == right.d ? o < right.o : d > right.d;
  }
};

const int dx[] = {-1, 1, 0};
const int dy[] = {0, 0, 1};
int field[10][10];
int d[51][10][10][10][10];

int main() {
  while (1) {
    int W, H;
    scanf("%d%d", &W, &H);
    if (!W)
      break;

    int f, m, o;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &field[i][j]);
      }
    }

    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          for (int l = 0; l < 10; l++) {
            fill(d[i][j][k][l], d[i][j][k][l] + 10, INF);
          }
        }
      }
    }

    priority_queue<State> que;
    for (int i = 0; i < W; i++) {
      int no = o - 1, nd = 0;
      if (no <= 0)
        continue;
      if (field[0][i] < 0)
        nd -= field[0][i];
      else
        no = min(m, no + field[0][i]);
      d[no][0][i][i][i] = nd;
      que.push((State){nd, no, 0, i, i, i});
    }

    int ans = -1;
    while (que.size()) {
      State p = que.top();
      que.pop();
      if (d[p.o][p.y][p.x][p.l][p.r] < p.d)
        continue;
      if (p.d > f)
        break;
      if (p.y == H - 1) {
        ans = p.d;
        break;
      }

      for (int i = 0; i < 3; i++) {
        int nx = p.x + dx[i], ny = p.y + dy[i];
        if (nx < 0 || W <= nx || ny < 0 || H <= ny)
          continue;
        int nl = min(p.l, nx), nr = max(p.r, nx);
        int no = p.o - 1;
        if (no <= 0)
          continue;
        int nd = p.d;
        if (i == 2) {
          nl = nx;
          nr = nx;
        }
        if (i == 2 || nx < p.l || p.r < nx) {
          if (field[ny][nx] < 0)
            nd -= field[ny][nx];
          else
            no = min(m, no + field[ny][nx]);
        }
        if (d[no][ny][nx][nl][nr] > nd) {
          d[no][ny][nx][nl][nr] = nd;
          que.push((State){nd, no, ny, nx, nl, nr});
        }
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <complex>
#include <iostream>
#include <map>
#include <math.h>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <stdio.h>
#include <string.h>
#include <string>
#include <utility>
#include <vector>
using namespace std;

const int INF = 1 << 29;
const double EPS = 1e-8;
typedef vector<int> vec;
typedef pair<int, int> P;
struct edge {
  int to, cost;
};

struct State {
  int d, o, y, x, b;
  bool operator<(const State &right) const {
    return d == right.d ? o < right.o : d > right.d;
  }
};

const int dx[] = {-1, 1, 0};
const int dy[] = {0, 0, 1};
int field[10][10];
int d[51][10][10][(1 << 10)];

int main() {
  while (1) {
    int W, H;
    scanf("%d%d", &W, &H);
    if (!W)
      break;

    int f, m, o;
    scanf("%d%d%d", &f, &m, &o);
    for (int i = 0; i < H; i++) {
      for (int j = 0; j < W; j++) {
        scanf("%d", &field[i][j]);
      }
    }

    for (int i = 0; i < 51; i++) {
      for (int j = 0; j < 10; j++) {
        for (int k = 0; k < 10; k++) {
          for (int l = 0; l < (1 << 10); l++) {
            d[i][j][k][l] = INF;
          }
        }
      }
    }

    priority_queue<State> que;
    for (int i = 0; i < W; i++) {
      if (field[0][i] < 0) {
        d[o][0][i][(1 << i)] = -field[0][i];
        que.push((State){-field[0][i], o - 1, 0, i, (1 << i)});
      } else {
        if (o - 1 + field[0][i] >= m) {
          d[m][0][i][(1 << i)] = 0;
          que.push((State){0, m, 0, i, (1 << i)});
        } else {
          d[o - 1 + field[0][i]][0][i][(1 << i)] = 0;
          que.push((State){0, o - 1 + field[0][i], 0, i, (1 << i)});
        }
      }
    }

    int ans = -1;
    while (que.size()) {
      State p = que.top();
      que.pop();
      if (d[p.o][p.y][p.x][p.b] < p.d)
        continue;
      if (p.d >= f)
        break;
      if (p.y == H - 1) {
        ans = p.d;
        break;
      }

      for (int i = 0; i < 3; i++) {
        int nx = p.x + dx[i], ny = p.y + dy[i];
        if (nx < 0 || W <= nx || ny < 0 || H <= ny)
          continue;
        int nb = i == 2 ? 0 : p.b;
        if ((nb >> nx) & 1) {
          if (p.o <= 1)
            continue;
          bool useless = false;
          for (int i = p.o; i <= m; i++) {
            if (d[i][ny][nx][nb] <= p.d) {
              useless = true;
              break;
            }
          }
          if (useless)
            continue;
          if (d[p.o - 1][ny][nx][nb] > p.d) {
            d[p.o - 1][ny][nx][nb] = p.d;
            que.push((State){p.d, p.o - 1, ny, nx, nb});
          }
        } else {
          nb |= (1 << nx);
          if (field[ny][nx] < 0) {
            if (p.o <= 1)
              continue;
            int nd = p.d - field[ny][nx];
            if (d[p.o - 1][ny][nx][nb] > nd) {
              d[p.o - 1][ny][nx][nb] = nd;
              // printf("from p.d=%d p.y=%d p.x=%d p.o=%d",p.d,p.y,p.x,p.o);
              // printf(" to p.d=%d p.y=%d p.x=%d p.o=%d\\n",nd,ny,nx,p.o-1);
              que.push((State){nd, p.o - 1, ny, nx, nb});
            }
          } else {
            int no = min(m, p.o - 1 + field[ny][nx]);
            if (d[no][ny][nx][nb] > p.d) {
              d[no][ny][nx][nb] = p.d;
              que.push((State){p.d, no, ny, nx, nb});
              // printf("from p.d=%d p.y=%d p.x=%d p.o=%d",p.d,p.y,p.x,p.o);
              // printf(" to p.d=%d p.y=%d p.x=%d p.o=%d\\n",p.d,ny,nx,no);
            }
          }
        }
      }
    }
    if (ans == -1)
      cout << "NA" << endl;
    else
      cout << ans << endl;
  }

  return 0;
}