結果

問題 No.34 砂漠の行商人
ユーザー togari_takamototogari_takamoto
提出日時 2016-02-26 13:32:44
言語 C++11
(gcc 11.4.0)
結果
AC  
実行時間 1,731 ms / 5,000 ms
コード長 1,395 bytes
コンパイル時間 1,502 ms
コンパイル使用メモリ 161,112 KB
実行使用メモリ 8,460 KB
最終ジャッジ日時 2023-09-10 19:20:08
合計ジャッジ時間 9,100 ms
ジャッジサーバーID
(参考情報)
judge13 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,376 KB
testcase_02 AC 3 ms
4,380 KB
testcase_03 AC 2 ms
4,376 KB
testcase_04 AC 44 ms
4,376 KB
testcase_05 AC 41 ms
4,380 KB
testcase_06 AC 4 ms
4,376 KB
testcase_07 AC 74 ms
4,376 KB
testcase_08 AC 104 ms
4,376 KB
testcase_09 AC 541 ms
5,328 KB
testcase_10 AC 72 ms
4,380 KB
testcase_11 AC 219 ms
4,376 KB
testcase_12 AC 20 ms
4,380 KB
testcase_13 AC 1,731 ms
8,460 KB
testcase_14 AC 1,422 ms
8,220 KB
testcase_15 AC 4 ms
4,376 KB
testcase_16 AC 88 ms
4,380 KB
testcase_17 AC 3 ms
4,376 KB
testcase_18 AC 7 ms
4,376 KB
testcase_19 AC 485 ms
5,240 KB
testcase_20 AC 848 ms
7,952 KB
testcase_21 AC 4 ms
4,376 KB
testcase_22 AC 12 ms
4,376 KB
testcase_23 AC 3 ms
4,376 KB
testcase_24 AC 938 ms
5,192 KB
testcase_25 AC 64 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi  = vector<int>; using vb  = vector<bool>; using vd  = vector<double>; using vl  = vector<ll>;
using vvi = vector<vi>;  using vvb = vector<vb>;   using vvd = vector<vd>;     using vvl = vector<vl>;

#define REP(i,n) for(ll i=0; i<(n); ++i)

int dx[] = {1, 0, -1, 0};
int dy[] = {0, 1, 0, -1};

bool contain(ll n, ll x) { return 0 <= x && x < n; }

int main() {
	ll n, v, sx, sy, gx, gy;
	cin >> n >> v >> sx >> sy >> gx >> gy;
	sx--; sy--; gx--; gy--;
	vvi field(n, vi(n));
	REP(y, n) REP(x, n) cin >> field[y][x];

	vvl vital(n, vl(n, 0));
	using P = pair<ll, pair<ll, pair<ll,ll>>>;
	priority_queue<P, vector<P>, greater<P>> que;
	que.push({0, {v, {sx, sy}}});
	while (!que.empty()) {
		auto _ = que.top(); que.pop();
		ll times = _.first;
		ll cur_v = _.second.first;
		auto pos = _.second.second;
		if (pos.first == gx && pos.second == gy) {
			cout << times << endl; return 0;
		}
		
		if (vital[pos.second][pos.first] >= cur_v) continue;
		vital[pos.second][pos.first] = cur_v;

		REP(i, 4) {
			auto n_pos = make_pair(pos.first + dx[i], pos.second + dy[i]);
			if (!contain(n, n_pos.first) || !contain(n, n_pos.second)) continue;
			ll n_v = cur_v - field[n_pos.second][n_pos.first];
			if(n_v > vital[n_pos.second][n_pos.first]) que.push({ times + 1, { n_v, n_pos } });
		}
	}

	cout << -1 << endl;
	return 0;
}
0