結果

問題 No.34 砂漠の行商人
ユーザー Yang33Yang33
提出日時 2018-04-10 22:20:31
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
TLE  
実行時間 -
コード長 3,497 bytes
コンパイル時間 1,756 ms
コンパイル使用メモリ 182,260 KB
実行使用メモリ 236,536 KB
最終ジャッジ日時 2023-09-09 03:53:58
合計ジャッジ時間 8,627 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
4,380 KB
testcase_01 AC 1 ms
4,376 KB
testcase_02 AC 4 ms
4,380 KB
testcase_03 AC 7 ms
4,376 KB
testcase_04 AC 41 ms
4,948 KB
testcase_05 AC 40 ms
5,100 KB
testcase_06 AC 4 ms
4,380 KB
testcase_07 AC 64 ms
6,180 KB
testcase_08 AC 91 ms
8,132 KB
testcase_09 TLE -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;

using VS = vector<string>;    using LL = long long;
using VI = vector<int>;       using VVI = vector<VI>;
using PII = pair<int, int>;   using PLL = pair<LL, LL>;
using VL = vector<LL>;        using VVL = vector<VL>;

#define ALL(a)  begin((a)),end((a))
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define EB emplace_back
#define MP make_pair
#define SZ(a) int((a).size())
#define SORT(c) sort(ALL((c)))
#define RSORT(c) sort(RALL((c)))
#define UNIQ(c) (c).erase(unique(ALL((c))), end((c)))
#define FOR(i, s, e) for (int(i) = (s); (i) < (e); (i)++)
#define FORR(i, s, e) for (int(i) = (s); (i) > (e); (i)--)
#define debug(x) cerr << #x << ": " << x << endl
const int INF = 1e9;                          const LL LINF = 1e16;
const LL MOD = 1000000007;                    const double PI = acos(-1.0);
int DX[8] = { 0, 0, 1, -1, 1, 1, -1, -1 };    int DY[8] = { 1, -1, 0, 0, 1, -1, 1, -1 };

/* -----  2018/04/10  Problem: yukicoder 034  / Link: http://yukicoder.me/problems/no/034  ----- */
/* ------問題------

太郎君は砂漠を歩く行商人です。
太郎君はこれから次の街へ行こうとしています。

砂漠には移動しやすい場所とそうでない場所があり、
太郎君は長年の経験から、
その場所に行くとどれくらいの体力を消耗するかを知っています。

砂漠は際限なく続いていますが、太郎君が知っているのは N×N マスの範囲だけで、
その外側に行くと命の危険があるため絶対に行きません。

いま太郎君は体力 V で (SX,SY) の場所に立っており、次の街は (GX,GY) の場所にあります。
太郎君は、辺を共有する前後左右の隣接マスへのみ移動することができ、
今居るマスから隣のマスへ移動するときに1回の移動とみなし、
さらに、移動した先の砂漠レベル(LXY)分の体力が減ります。
移動先の砂漠レベルが0の場合、体力値は減りませんが、
太郎君の体力が 0以下 になった時点で太郎君が死んでしまいます。
街に着いた瞬間に死んでしまってもいけません。

太郎君は、商品をできるだけ早く捌きたいので、
「太郎君が死なずに」「最も早く次の街へ着く」には、
どれくらい時間がかかるか計算してください。

-----問題ここまで----- */
/* -----解説等-----



----解説ここまで---- */


int main() {
	cin.tie(0);
	ios_base::sync_with_stdio(false);

	LL N, V; cin >> N >> V;
	int sy, sx, ty, tx; cin >> sx >> sy >> tx >> ty;
	sy--, sx--, tx--, ty--;
	VVI cost(N, VI(N));
	FOR(i, 0, N) {
		FOR(j, 0, N) {
			cin >> cost[i][j];
		}
	}
	using tp = tuple<int, int, int, int>;
	priority_queue<tp, vector<tp>, greater<tp>>pq;
	vector<VVI>dist(N, VVI(N, VI(V, INF)));
	dist[sy][sx][V - 1] = 0;
	pq.push(tp(0, V - 1, sy, sx));

	while (!pq.empty()) {
		int d, v, y, x;
		tie(d, v, y, x) = pq.top(); pq.pop();
		if (dist[y][x][v] < d)continue;
		FOR(k, 0, 4) {
			int ny = y + DY[k], nx = x + DX[k];
			if (0 <= ny&&ny < N && 0 <= nx&&nx < N) {
				int nv = v - cost[ny][nx];
				if (nv >= 0) {
					if (dist[ny][nx][nv] > dist[y][x][v] + 1) {
						dist[ny][nx][nv] = dist[y][x][v] + 1;
						pq.push(tp(dist[ny][nx][nv], nv, ny, nx));
					}
				}
			}
		}

	}
	int ans = INF;
	FOR(i, 0, V) {
		ans = min(ans, dist[ty][tx][i]);
	}
	cout << (ans == INF ? -1 : ans) << "\n";

	return 0;
}
0