結果
問題 | No.20 砂漠のオアシス |
ユーザー | 双六 |
提出日時 | 2020-07-26 02:13:21 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 245 ms / 5,000 ms |
コード長 | 1,568 bytes |
コンパイル時間 | 290 ms |
コンパイル使用メモリ | 82,048 KB |
実行使用メモリ | 93,952 KB |
最終ジャッジ日時 | 2024-06-27 18:53:04 |
合計ジャッジ時間 | 4,160 ms |
ジャッジサーバーID (参考情報) |
judge2 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
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()