結果

問題 No.20 砂漠のオアシス
ユーザー ant2357ant2357
提出日時 2019-03-28 01:40:19
言語 C++14
(gcc 12.3.0 + boost 1.83.0)
結果
RE  
実行時間 -
コード長 1,837 bytes
コンパイル時間 1,768 ms
コンパイル使用メモリ 179,068 KB
実行使用メモリ 4,384 KB
最終ジャッジ日時 2023-08-01 14:37:16
合計ジャッジ時間 3,185 ms
ジャッジサーバーID
(参考情報)
judge11 / judge15
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 1 ms
4,380 KB
testcase_01 AC 2 ms
4,380 KB
testcase_02 AC 2 ms
4,376 KB
testcase_03 RE -
testcase_04 AC 2 ms
4,380 KB
testcase_05 AC 15 ms
4,380 KB
testcase_06 AC 17 ms
4,376 KB
testcase_07 AC 16 ms
4,380 KB
testcase_08 AC 16 ms
4,376 KB
testcase_09 AC 16 ms
4,380 KB
testcase_10 AC 2 ms
4,376 KB
testcase_11 WA -
testcase_12 AC 2 ms
4,376 KB
testcase_13 AC 2 ms
4,380 KB
testcase_14 AC 3 ms
4,376 KB
testcase_15 AC 2 ms
4,376 KB
testcase_16 AC 5 ms
4,376 KB
testcase_17 AC 4 ms
4,380 KB
testcase_18 AC 4 ms
4,380 KB
testcase_19 AC 4 ms
4,380 KB
testcase_20 AC 2 ms
4,380 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#include "bits/stdc++.h"

using namespace std;

using ll = long long;
using ld = long double;

const double PI = 3.1415926535897932384626433832795;

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

int gcd(int x, int y) { return y ? gcd(y, x % y) : abs(x); }
ll gcd(ll x, ll y) { return y ? gcd(y, x % y) : abs(x); }
int lcm(int x, int y) { return x / gcd(x, y) * y; }
ll lcm(ll x, ll y) { return x / gcd(x, y) * y; }

int n, v, ox, oy;
vector<vector<int>> graph;

vector<vector<int>> dijkstra(int sy, int sx) {
	vector<vector<int>> dist(n, vector<int>(n, INT_MAX));
	dist[sy][sx] = 0;

	priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>, greater<pair<int, pair<int, int>>>> pq;
	pq.push({ dist[sy][sx], {sy, sx} });

	while (!pq.empty()) {
		int nowCost = pq.top().first;
		int y = pq.top().second.first;
		int x = pq.top().second.second;
		pq.pop();

		if (nowCost > dist[y][x]) {
			continue;
		}

		for (int i = 0; i < 4; i++) {
			int ny = y + dy[i];
			int nx = x + dx[i];

			if (ny < 0 || ny >= n || nx < 0 || nx >= n) {
				continue;
			}

			if (dist[ny][nx] > nowCost + graph[ny][nx]) {
				dist[ny][nx] = nowCost + graph[ny][nx];
				pq.push({ dist[ny][nx] , {ny, nx} });
			}
		}
	}

	return dist;
}

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

	cin >> n >> v >> ox >> oy;
	ox--; oy--;

	graph.resize(n, vector<int>(n));

	for (int y = 0; y < n; y++) {
		for (int x = 0; x < n; x++) {
			cin >> graph[y][x];
		}
	}

	vector<vector<int>> dist = dijkstra(0, 0);

	if (dist[n - 1][n - 1] < v) {
		cout << "YES" << endl;
		return 0;
	}

	if (oy == 0 || ox == 0) {
		cout << "NO" << endl;
		return 0;
	}

	int healHp = (v - dist[oy][ox]) * 2;
	vector<vector<int>> dist2 = dijkstra(oy, ox);

	cout << ((dist2[n - 1][n - 1] < healHp) ? "YES" : "NO") << endl;
	return 0;
}
0