結果

問題 No.20 砂漠のオアシス
ユーザー rlangevinrlangevin
提出日時 2023-06-29 23:00:58
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 249 ms / 5,000 ms
コード長 1,550 bytes
コンパイル時間 500 ms
コンパイル使用メモリ 87,140 KB
実行使用メモリ 90,024 KB
最終ジャッジ日時 2023-09-20 18:02:29
合計ジャッジ時間 5,176 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,124 KB
testcase_01 AC 79 ms
71,364 KB
testcase_02 AC 80 ms
71,484 KB
testcase_03 AC 132 ms
78,652 KB
testcase_04 AC 133 ms
78,588 KB
testcase_05 AC 234 ms
88,236 KB
testcase_06 AC 246 ms
90,024 KB
testcase_07 AC 249 ms
89,992 KB
testcase_08 AC 245 ms
89,840 KB
testcase_09 AC 246 ms
89,748 KB
testcase_10 AC 77 ms
71,308 KB
testcase_11 AC 77 ms
71,372 KB
testcase_12 AC 151 ms
79,112 KB
testcase_13 AC 158 ms
79,512 KB
testcase_14 AC 167 ms
80,216 KB
testcase_15 AC 158 ms
79,300 KB
testcase_16 AC 183 ms
81,428 KB
testcase_17 AC 171 ms
79,820 KB
testcase_18 AC 170 ms
81,172 KB
testcase_19 AC 175 ms
81,320 KB
testcase_20 AC 129 ms
78,280 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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")
0