結果

問題 No.20 砂漠のオアシス
ユーザー 双六双六
提出日時 2020-07-26 02:13:21
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 305 ms / 5,000 ms
コード長 1,568 bytes
コンパイル時間 310 ms
コンパイル使用メモリ 87,004 KB
実行使用メモリ 94,892 KB
最終ジャッジ日時 2023-09-10 02:48:56
合計ジャッジ時間 5,639 ms
ジャッジサーバーID
(参考情報)
judge12 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 94 ms
71,300 KB
testcase_01 AC 98 ms
71,652 KB
testcase_02 AC 96 ms
71,340 KB
testcase_03 AC 173 ms
79,116 KB
testcase_04 AC 173 ms
79,272 KB
testcase_05 AC 280 ms
93,144 KB
testcase_06 AC 305 ms
93,816 KB
testcase_07 AC 298 ms
93,896 KB
testcase_08 AC 300 ms
94,200 KB
testcase_09 AC 302 ms
94,892 KB
testcase_10 AC 95 ms
71,468 KB
testcase_11 AC 97 ms
71,324 KB
testcase_12 AC 171 ms
79,048 KB
testcase_13 AC 168 ms
79,560 KB
testcase_14 AC 180 ms
79,900 KB
testcase_15 AC 180 ms
79,956 KB
testcase_16 AC 215 ms
81,676 KB
testcase_17 AC 205 ms
80,700 KB
testcase_18 AC 210 ms
80,924 KB
testcase_19 AC 206 ms
81,580 KB
testcase_20 AC 151 ms
78,328 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys; input = sys.stdin.buffer.readline
sys.setrecursionlimit(10**7)
from collections import defaultdict
from heapq import heappop, heappush
INF = float('inf')

def getlist():
	return list(map(int, input().split()))

class Graph(object):
	def __init__(self):
		self.graph = defaultdict(list)

	def __len__(self):
		return len(self.graph)

	def add_edge(self, a, b, w):
		self.graph[a].append((b, w))

class Dijkstra(object):
	def __init__(self, graph, s):
		self.g = graph.graph
		self.dist = defaultdict(lambda: INF); self.dist[s] = 0
		self.Q = []
		heappush(self.Q, (self.dist[s], s))
		while self.Q:
			dist_u, u = heappop(self.Q)
			if self.dist[u] < dist_u:
				continue
			for v, w in self.g[u]:
				alt = dist_u + w
				if self.dist[v] > alt:
					self.dist[v] = alt
					heappush(self.Q, (alt, v))

#処理内容
def main():
	N, V, Ox, Oy = getlist()
	L = []
	for i in range(N):
		l = getlist()
		L.append(l)

	G = Graph()
	for i in range(N - 1):
		for j in range(N):
			G.add_edge(i * N + j, (i + 1) * N + j, L[i + 1][j])
			G.add_edge((i + 1) * N + j, i * N + j, L[i][j])

	for i in range(N):
		for j in range(N - 1):
			G.add_edge(i * N + j, i * N + j + 1, L[i][j + 1])
			G.add_edge(i * N + j + 1, i * N + j, L[i][j])

	D = Dijkstra(G, 0)
	dist = D.dist
	if dist[N ** 2 - 1] < V:
		print("YES")
		return

	if not (Ox == 0 and Oy == 0):
		oasisu = (Oy - 1) * N + Ox - 1
		stop = dist[oasisu]
		D2 = Dijkstra(G, oasisu)
		if stop < V and 2 * (V - stop) > D2.dist[N ** 2 - 1]:
			print("YES")
			return

	print("NO")

if __name__ == '__main__':
	main()
0