結果
| 問題 | No.20 砂漠のオアシス | 
| コンテスト | |
| ユーザー |  rlangevin | 
| 提出日時 | 2023-06-29 23:00:58 | 
| 言語 | PyPy3 (7.3.15) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 185 ms / 5,000 ms | 
| コード長 | 1,550 bytes | 
| コンパイル時間 | 284 ms | 
| コンパイル使用メモリ | 82,004 KB | 
| 実行使用メモリ | 89,512 KB | 
| 最終ジャッジ日時 | 2024-07-06 12:58:34 | 
| 合計ジャッジ時間 | 3,227 ms | 
| ジャッジサーバーID (参考情報) | judge4 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 21 | 
ソースコード
from heapq import heappush, heappop
inf = float('inf')
def dijkstra(s, g, N):
    # ゴールがない場合はg=-1とする。
    def cost(v, m):
        return v * N + m
    dist = [inf] * N
    mindist = [inf] * N
    seen = [False] * N
    Q = [cost(0, s)]
    while Q:
        c, m = divmod(heappop(Q), N)
        if seen[m]:
            continue
        seen[m] = True
        dist[m] = c
        if m == g:
            return dist
        #------heapをアップデートする。--------
        for u, C in G[m]:
            if seen[u]:
                continue
            newdist = dist[m] + C
            #------------------------------------
            if newdist >= mindist[u]:
                continue
            mindist[u] = newdist
            heappush(Q, cost(newdist, u))
            
    return dist
N, V, qx, qy = map(int, input().split())
L = []
for i in range(N):
    L.append(list(map(int, input().split())))
    
G = [[] for i in range(N * N)]
dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]
for i in range(N):
    for j in range(N):
        for k in range(4):
            x = i + dx[k]
            y = j + dy[k]
            if x < 0 or x > N - 1 or y < 0 or y > N - 1:
                continue
            G[i * N + j].append((x * N + y, L[x][y]))
            
D = dijkstra(0, -1, N * N)
if D[-1] < V:
    print("YES")
    exit()
if qx == 0 and qy == 0:
    print("NO")
    exit()
    
qy, qx = qx - 1, qy - 1    
V -= D[qx * N + qy]
V *= 2
D2 = dijkstra(qx * N + qy, -1, N * N)
print("YES") if D2[-1] < V else print("NO")
            
            
            
        