Language: C++

//Solution for aoj:0212 Highway Express Bus
#include<iostream>
#include<algorithm>
#include<vector>
#include<queue>
#include<functional>
using namespace std;

struct S {
	int cost, x, y, o, xh;
	S(int c, int in_x, int in_y, int in_o, int in_xh) : cost(c), x(in_x), y(in_y), o(in_o), xh(in_xh){}
	bool operator<(const S &s) const {
		return cost < s.cost;
	}
};

const int INF = -1000000000, INF2=10000000;
int w, h, f, m, o, ans;
int stratum[11][11];
int c[11][11][51];
priority_queue<S> pq;

void dijkstra(){
	int move[3] = { 1, -1, 1 };//?´y,?´x,?´x
	while (!pq.empty()){
		S a = pq.top();
		pq.pop();
		if (!a.o) continue;
		for (int i = 0; i < 3; i++){
			if (i&&a.x+move[i]<=w&&0<a.x+move[i]){
				if (stratum[a.y][a.x+move[i]]< 0 && a.cost + stratum[a.y][a.x+move[i]] >= -f&&a.cost + stratum[a.y][a.x+move[i]]>c[a.y][a.x + move[i]][a.o - 1]){
					c[a.y][a.x + move[i]][a.o - 1] = a.cost + stratum[a.y][a.x+move[i]];

					pq.push({ c[a.y][a.x + move[i]][a.o - 1], a.x + move[i], a.y, a.o - 1 ,a.xh+move[i]});
				}
				else if (stratum[a.y][a.x+move[i]] > 0){
					if (stratum[a.y][a.x + move[i]] + a.x - 1 >= m)
						stratum[a.y][a.x + move[i]] = m - a.x + 2;
					c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1] = a.cost;
					pq.push({ c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1], a.x + move[i], a.y, a.o + stratum[a.y][a.x + move[i]] - 1, a.xh+move[i] });
					//if (a.xh)
					//	for (int j = 1; j <= a.xh; j++)
					//		if (stratum[a.y][a.x+move[i]]>j)
					//		pq.push({ c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1], a.x + +move[i] + j, a.y, a.o + stratum[a.y][a.x + move[i]] - 1 - j, a.xh + move[i] });
			
					//if (a.xh < 0)
					//	for (int j = -1; j <= a.xh; j--)
					//		if (stratum[a.y][a.x+move[i]]>-j)
					//		pq.push({ c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1], a.x + +move[i] + j, a.y, a.o + stratum[a.y][a.x + move[i]] - 1 + j, a.xh + move[i] });
				}
			}
			else if(!i&&a.y+move[i]<=h){
				if (stratum[a.y+move[i]][a.x] < 0 && a.cost + stratum[a.y+move[i]][a.x] >= -f&&a.cost+stratum[a.y+move[i]][a.x]>c[a.y+move[i]][a.x][a.o-1]){
					c[a.y + move[i]][a.x][a.o - 1] = a.cost + stratum[a.y+move[i]][a.x];
					pq.push({ c[a.y + move[i]][a.x][a.o - 1], a.x, a.y + move[i], a.o - 1, 0 });
				}
				else if (stratum[a.y+move[i]][a.x] > 0){
					if (stratum[a.y+move[i]][a.x] + a.x - 1 >= m)
						stratum[a.y+move[i]][a.x] = m - a.x + 2;
					c[a.y + move[i]][a.x][a.o + stratum[a.y + move[i]][a.x] - 1] = a.cost;
					pq.push({ c[a.y + move[i]][a.x][a.o + stratum[a.y + move[i]][a.x] - 1], a.x, a.y + move[i], a.o + stratum[a.y + move[i]][a.x] - 1, 0 });
					//for (int j = 1; j <= a.xh; j++)
					//	if (stratum[a.y][a.x + move[i]]>j)
					//	pq.push({ c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1], a.x + +move[i] + j, a.y, a.o + stratum[a.y][a.x + move[i]] - 1 - j, a.xh + move[i] });
					//if (a.xh<0)
					//	for (int j = -1; j <= a.xh; j--)
					//		if (stratum[a.y][a.x + move[i]]>-j)
					//		pq.push({ c[a.y][a.x + move[i]][a.o + stratum[a.y][a.x + move[i]] - 1], a.x + +move[i] + j, a.y, a.o + stratum[a.y][a.x + move[i]] - 1 + j, a.xh + move[i] });
				}
			}
		}
	}
}


int main(){
	while (cin >> w >> h&&w&&h){
		cin >> f >> m >> o;
		memset(stratum, sizeof(stratum), 0);
		ans=INF2;
		for (int i = 1; i <= 10; i++)
			for (int j = 1; j <= 10; j++)
				fill(c[i][j], c[i][j] + 51, INF);
		for (int i = 1; i <= h; i++)
			for (int j = 1; j <= w; j++)
				cin >> stratum[i][j];
		for (int i = 1; i <= w; i++){
			c[1][i][o - 1] = stratum[1][i];
			pq.push({ stratum[1][i], i, 1, o - 1 ,0});
		}
		dijkstra();
		for (int i = 1; i <= m; i++)
			for (int j = 1; j <= w; j++)
				if (c[h][j][i] != INF)
					ans = min(ans, -c[h][j][i]);
		if (ans!=INF2)
			cout << ans << endl;
		else
			cout << "NA" << endl;

	}
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:79:17: error: 'memset' was not declared in this scope
   79 |                 memset(stratum, sizeof(stratum), 0);
      |                 ^~~~~~
a.cc:7:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    6 | #include<functional>
  +++ |+#include <cstring>
    7 | using namespace std;