Problem p00234 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int g[12][12][60][12][12]; // y, x, air, l, r
int event[12][12];
int w, h, f, m, o;
int dx[] = {1, 0, -1}, dy[] = {0, 1, 0};

typedef struct state {
  int cost, air, x, y, l, r;
  bool operator<(const state &a) const { return cost < a.cost; }
  bool operator>(const state &a) const { return cost > a.cost; }
} state;

bool range(int y, int x) { return (0 <= x and x < w and 0 <= y and y < h); }

void solve() {
  priority_queue<state, vector<state>, greater<state>> que;
  for (int i = 0; i < w; i++) {
    if (event[0][i] < 0 and m >= 1) {
      g[0][i][m - 1][i][i] = -event[0][i];
      state tmp = (state){-event[0][i], m - 1, i, 0, i, i};
      que.push(tmp);
    } else {
      int air = min(o, m + event[0][i] - 1);
      g[0][i][air][i][i] = 0;
      state tmp = (state){0, air, i, 0, i, i};
      que.push(tmp);
    }
  }
  while (not que.empty()) {
    state tmp = que.top();
    que.pop();
    int l = tmp.l, r = tmp.r;
    int cost = tmp.cost;
    int air = tmp.air;
    int x = tmp.x, y = tmp.y;
    if (air <= 1)
      continue;
    for (int i = 0; i < 3; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (not range(ny, nx))
        continue;
      int nl = min(l, nx), nr = max(r, nx);
      if (y != ny) {
        nl = nx;
        nr = nx;
      }
      if (event[ny][nx] < 0) {
        int add = -event[ny][nx];
        if (l <= nx and nx <= r and y == ny)
          add = 0;
        if (g[ny][nx][air - 1][nl][nr] > cost + add) {
          g[ny][nx][air - 1][nl][nr] = cost + add;
          int c = cost + add;
          tmp = (state){c, air - 1, nx, ny, nl, nr};
          que.push(tmp);
        }
      } else {
        int nair = min(air - 1 + event[ny][nx], o);
        if (l <= nx and nx <= r and y == ny)
          nair = air - 1;
        if (g[ny][nx][nair][nl][nr] > cost) {
          g[ny][nx][nair][nl][nr] = cost;
          tmp = (state){cost, nair, nx, ny, nl, nr};
          que.push(tmp);
        }
      }
    }
  }
}

void init() {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      for (int k = 0; k <= o; k++)
        for (int l = 0; l < w; l++)
          for (int m = 0; m < w; m++)
            g[i][j][k][l][m] = 1e9;
}

int main() {
  while (std::cin >> w >> h, w) {
    std::cin >> f >> o >> m;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        std::cin >> event[i][j];
      }
    }
    init();
    solve();
    int ans = 1e9;
    for (int i = 0; i < w; i++) {
      for (int j = 1; j <= o; j++) {
        for (int k = 0; k < w; k++) {
          for (int l = 0; l < w; l++) {
            ans = min(ans, g[h - 1][i][j][k][l]);
          }
        }
      }
    }
    if (ans > f)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int g[12][12][60][12][12]; // y, x, air, l, r
int event[12][12];
int w, h, f, m, o;
int dx[] = {1, 0, -1}, dy[] = {0, 1, 0};

typedef struct state {
  int cost, air, x, y, l, r;
  bool operator<(const state &a) const { return cost < a.cost; }
  bool operator>(const state &a) const { return cost > a.cost; }
} state;

bool range(int y, int x) { return (0 <= x and x < w and 0 <= y and y < h); }

void solve() {
  priority_queue<state, vector<state>, greater<state>> que;
  for (int i = 0; i < w; i++) {
    if (m <= 1)
      continue;
    if (event[0][i] < 0) {
      g[0][i][m - 1][i][i] = -event[0][i];
      state tmp = (state){-event[0][i], m - 1, i, 0, i, i};
      que.push(tmp);
    } else {
      int air = min(o, m + event[0][i] - 1);
      g[0][i][air][i][i] = 0;
      state tmp = (state){0, air, i, 0, i, i};
      que.push(tmp);
    }
  }
  while (not que.empty()) {
    state tmp = que.top();
    que.pop();
    int l = tmp.l, r = tmp.r;
    int cost = tmp.cost;
    int air = tmp.air;
    int x = tmp.x, y = tmp.y;
    if (air <= 1)
      continue;
    for (int i = 0; i < 3; i++) {
      int nx = x + dx[i], ny = y + dy[i];
      if (not range(ny, nx))
        continue;
      int nl = min(l, nx), nr = max(r, nx);
      if (y != ny) {
        nl = nx;
        nr = nx;
      }
      if (event[ny][nx] < 0) {
        int add = -event[ny][nx];
        if (l <= nx and nx <= r and y == ny)
          add = 0;
        if (g[ny][nx][air - 1][nl][nr] > cost + add) {
          g[ny][nx][air - 1][nl][nr] = cost + add;
          int c = cost + add;
          tmp = (state){c, air - 1, nx, ny, nl, nr};
          que.push(tmp);
        }
      } else {
        int nair = min(air - 1 + event[ny][nx], o);
        if (l <= nx and nx <= r and y == ny)
          nair = air - 1;
        if (g[ny][nx][nair][nl][nr] > cost) {
          g[ny][nx][nair][nl][nr] = cost;
          tmp = (state){cost, nair, nx, ny, nl, nr};
          que.push(tmp);
        }
      }
    }
  }
}

void init() {
  for (int i = 0; i < h; i++)
    for (int j = 0; j < w; j++)
      for (int k = 0; k <= o; k++)
        for (int l = 0; l < w; l++)
          for (int m = 0; m < w; m++)
            g[i][j][k][l][m] = 1e9;
}

int main() {
  while (std::cin >> w >> h, w) {
    std::cin >> f >> o >> m;
    for (int i = 0; i < h; i++) {
      for (int j = 0; j < w; j++) {
        std::cin >> event[i][j];
      }
    }
    init();
    solve();
    int ans = 1e9;
    for (int i = 0; i < w; i++) {
      for (int j = 1; j <= o; j++) {
        for (int k = 0; k < w; k++) {
          for (int l = 0; l < w; l++) {
            ans = min(ans, g[h - 1][i][j][k][l]);
          }
        }
      }
    }
    if (ans > f)
      std::cout << "NA" << std::endl;
    else
      std::cout << ans << std::endl;
  }
  return 0;
}