Problem p00234 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF (1 << 30)
struct NODE {
  int cost, x, y, l, r, o;
  NODE(int C, int a, int b, int c, int d, int e) {
    cost = C, x = a, y = b, l = c, r = d, o = e;
  }
};
bool operator<(const NODE &a, const NODE &b) { return a.cost > b.cost; }

// «ÝpÏO,CÆA_fîñ,}XÌf[^,_f§À(=m)ðwè·éÆAY¦Ég¤_fÆÁZ·éRXgð»ê¼êãüµÄ­êéB
int get(int &O, int &C, int s, int d, int lim) {
  if (d < 0) {
    O = s;
    C = -d;
  } else {
    O = min(lim, s + d);
    C = 0;
  }
}

int solve(int W, int H, int m, int o, vector<vector<int>> data) {
  if (o < 2)
    return INF;
  int O, C;
  bool done[10][10][10][10][51] = {0}; // [x][y][l][r][o]
  priority_queue<NODE> Q;
  rep(i, W) {
    get(O, C, o - 1, data[0][i], m);
    Q.push(NODE(C, i, 0, i, i, O));
  }
  while (Q.size()) {
    NODE q = Q.top();
    Q.pop();
    if (q.o == 0)
      continue;

    if (q.y == H - 1)
      return q.cost;

    if (done[q.x][q.y][q.l][q.r][q.o])
      continue;
    else
      done[q.x][q.y][q.l][q.r][q.o] = true;

    // cè_fª1ÈçÎÚ®Å«È¢ÌÅcontinue
    if (q.o == 1)
      continue;

    // ¡Ú®
    static const int dx[] = {-1, 1};
    rep(i, 2) {
      int tx = q.x + dx[i];
      int l = min(q.l, tx), r = max(q.r, tx);
      if (tx < 0 || tx >= W)
        continue;

      if (l == q.l && r == q.r)
        C = 0, O = q.o - 1;
      else
        get(O, C, q.o - 1, data[q.y][tx], m);

      Q.push(NODE(q.cost + C, tx, q.y, l, r, O));
    }

    // cÚ®
    if (q.y < H - 1) {
      get(O, C, q.o - 1, data[q.y + 1][q.x], m);
      Q.push(NODE(q.cost + C, q.x, q.y + 1, q.x, q.x, O));
    }
  }
  return INF;
}
int main() {
  int W, H, f, m, o;
  while (cin >> W >> H, W) {
    cin >> f >> m >> o;
    vector<vector<int>> data(H, vector<int>(W));
    rep(i, H) rep(j, W) cin >> data[i][j];
    int ans = solve(W, H, m, o, data);
    if (ans <= f)
      cout << ans << endl;
    else
      cout << "NA" << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
#define rep(i, n) for (int i = 0; i < n; i++)

// Ypos/left/right/Xpos/sanso/
int dp[10][10][10][10][51];
int a, b;
int get(int s, int d, int lim) {
  if (d < 0) {
    a = s;
    b = -d;
  } else {
    a = min(lim, s + d);
    b = 0;
  }
}
int solve(int W, int H, int f, int m, int o, vector<vector<int>> data) {
  rep(i, 10) rep(j, 10) rep(k, 10) rep(l, 10) rep(m, 51) dp[i][j][k][l][m] =
      (1 << 22);
  rep(i, W) {
    get(o - 1, data[0][i], m);
    dp[0][i][i][i][a] = b;
  }
  for (int y = 0; y < H; y++) {
    rep(Q, 100) {
      for (int s = m; s >= 2; s--) {
        rep(l, W) for (int r = l; r < W; r++) {
          rep(x, W) {
            // left
            if (x > 0) {
              if (l == x) {
                get(s - 1, data[y][x - 1], m);
                dp[y][x - 1][r][x - 1][a] =
                    min(dp[y][x - 1][r][x - 1][a], dp[y][l][r][x][s] + b);
              } else {
                dp[y][l][r][x - 1][s - 1] =
                    min(dp[y][l][r][x - 1][s - 1], dp[y][l][r][x][s]);
              }
            }

            // right
            if (x < W - 1) {
              if (r == x) {
                get(s - 1, data[y][x + 1], m);
                dp[y][l][x + 1][x + 1][a] =
                    min(dp[y][l][x + 1][x + 1][a], dp[y][l][r][x][s] + b);
              } else {
                dp[y][l][r][x + 1][s - 1] =
                    min(dp[y][l][r][x + 1][s - 1], dp[y][l][r][x][s]);
              }
            }
          }
        }
      }
      for (int s = m; s >= 2; s--) {
        rep(l, W) for (int r = l; r < W; r++) {
          rep(x, W) {
            if (y < H - 1) {
              get(s - 1, data[y + 1][x], m);
              dp[y + 1][x][x][x][a] =
                  min(dp[y + 1][x][x][x][a], dp[y][l][r][x][s] + b);
            }
          }
        }
      }
    }
  }
  int ret = (1 << 22);
  for (int s = 1; s <= m; s++) {
    rep(l, W) for (int r = l; r < W; r++) {
      rep(x, W) { ret = min(dp[H - 1][l][r][x][s], ret); }
    }
  }
  return ret;
}
int main() {
  int W, H, f, m, o;
  while (cin >> W >> H, W) {
    cin >> f >> m >> o;
    vector<vector<int>> data(H, vector<int>(W));
    rep(i, H) rep(j, W) cin >> data[i][j];
    int ret = solve(W, H, f, m, o, data);
    if (ret <= f)
      cout << ret << endl;
    else
      cout << "NA" << endl;
  }
}