結果

問題 No.20 砂漠のオアシス
ユーザー kohei2019kohei2019
提出日時 2021-02-10 10:38:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 393 ms / 5,000 ms
コード長 1,809 bytes
コンパイル時間 293 ms
コンパイル使用メモリ 87,260 KB
実行使用メモリ 84,688 KB
最終ジャッジ日時 2023-09-22 03:51:43
合計ジャッジ時間 5,263 ms
ジャッジサーバーID
(参考情報)
judge15 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 88 ms
71,428 KB
testcase_01 AC 88 ms
71,296 KB
testcase_02 AC 88 ms
71,320 KB
testcase_03 AC 151 ms
78,076 KB
testcase_04 AC 148 ms
78,088 KB
testcase_05 AC 393 ms
84,688 KB
testcase_06 AC 208 ms
82,012 KB
testcase_07 AC 313 ms
82,308 KB
testcase_08 AC 183 ms
80,512 KB
testcase_09 AC 300 ms
81,156 KB
testcase_10 AC 86 ms
71,296 KB
testcase_11 AC 85 ms
71,104 KB
testcase_12 AC 178 ms
79,420 KB
testcase_13 AC 161 ms
78,744 KB
testcase_14 AC 199 ms
80,008 KB
testcase_15 AC 183 ms
79,360 KB
testcase_16 AC 210 ms
79,916 KB
testcase_17 AC 201 ms
79,736 KB
testcase_18 AC 210 ms
79,800 KB
testcase_19 AC 213 ms
79,468 KB
testcase_20 AC 140 ms
77,736 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
#ダイクストラ法
N,V,ox,oy = map(int,input().split())
ox,oy = oy-1,ox-1
lsL = [list(map(int,input().split())) for i in range(N)]
dxy =[(1,0),(0,1),(-1,0),(0,-1)]
#オアシスを通らない場合
lsHP = [[-float('INF')]*(N) for i in range(N)]
lsHP[0][0] = V#-lsL[0][0]
used = [[False]*(N) for i in range(N)]
h = [(-lsHP[0][0],0,0)]
heapq.heapify(h)
while h:
    cost,x,y = heapq.heappop(h)
    if used[x][y]:
        continue
    used[x][y] = True
    for dx,dy in dxy:
        if 0<=x+dx<N and 0<=y+dy<N:
            if used[x+dx][y+dy]:
                continue
            if (lsHP[x][y] - lsL[x+dx][y+dy]) < 0:
                continue
            if lsHP[x+dx][y+dy] < (lsHP[x][y] - lsL[x+dx][y+dy]):
                lsHP[x+dx][y+dy] = (lsHP[x][y] - lsL[x+dx][y+dy])
                heapq.heappush(h,(-lsHP[x+dx][y+dy],x+dx,y+dy))
if lsHP[-1][-1] > 0:
    print('YES')
    sys.exit()
if ox != -1 and oy != -1:
    #オアシスを通る場合
    lsHP2 = [[-float('INF')]*(N) for i in range(N)]
    lsHP2[ox][oy] = lsHP[ox][oy]*2
    used = [[False]*(N) for i in range(N)]
    h = [(-lsHP[ox][oy],ox,oy)]
    heapq.heapify(h)
    while h:
        cost,x,y = heapq.heappop(h)
        if used[x][y]:
            continue
        used[x][y] = True
        for dx,dy in dxy:
            if 0<=x+dx<N and 0<=y+dy<N:
                if used[x+dx][y+dy]:
                    continue
                if (lsHP2[x][y] - lsL[x+dx][y+dy]) < 0:
                    continue
                if lsHP2[x+dx][y+dy] < (lsHP2[x][y] - lsL[x+dx][y+dy]):
                    lsHP2[x+dx][y+dy] = (lsHP2[x][y] - lsL[x+dx][y+dy])
                    heapq.heappush(h,(-lsHP2[x+dx][y+dy],x+dx,y+dy))
    if lsHP2[-1][-1] > 0:
        print('YES')
    else:
        print('NO')
else:
    print('NO')
0