結果
問題 | No.20 砂漠のオアシス |
ユーザー | mkawa2 |
提出日時 | 2020-01-03 22:45:17 |
言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) |
結果 |
AC
|
実行時間 | 269 ms / 5,000 ms |
コード長 | 1,595 bytes |
コンパイル時間 | 122 ms |
コンパイル使用メモリ | 12,928 KB |
実行使用メモリ | 14,848 KB |
最終ジャッジ日時 | 2024-11-22 19:40:41 |
合計ジャッジ時間 | 2,519 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
other | AC * 21 |
ソースコード
from heapq import * import sys sys.setrecursionlimit(10 ** 6) int1 = lambda x: int(x) - 1 p2D = lambda x: print(*x, sep="\n") def II(): return int(sys.stdin.readline()) def MI(): return map(int, sys.stdin.readline().split()) def LI(): return list(map(int, sys.stdin.readline().split())) def LLI(rows_number): return [LI() for _ in range(rows_number)] def main(): n, v, oj, oi = MI() oi, oj = oi - 1, oj - 1 ll = [LI() for _ in range(n)] dp0 = [[0] * n for _ in range(n)] hp=[] heappush(hp,[-v,0,0]) while hp: p,i,j=heappop(hp) p=-p if p<=dp0[i][j]:continue dp0[i][j]=p if (i,j)==(oi,oj):continue for di,dj in [(1,0),(0,1),(-1,0),(0,-1)]: ni,nj=i+di,j+dj if ni<0 or ni>=n or nj<0 or nj>=n:continue np=p-ll[ni][nj] if np<=dp0[ni][nj]:continue if (ni,nj)==(n-1,n-1): print("YES") exit() heappush(hp,[-np,ni,nj]) if dp0[oi][oj]==0: print("NO") exit() dp1 = [[0] * n for _ in range(n)] hp=[] heappush(hp,[-dp0[oi][oj]*2,oi,oj]) while hp: p,i,j=heappop(hp) p=-p if p<=dp1[i][j]:continue dp1[i][j]=p for di,dj in [(1,0),(0,1),(-1,0),(0,-1)]: ni,nj=i+di,j+dj if ni<0 or ni>=n or nj<0 or nj>=n:continue np=p-ll[ni][nj] if np<=dp1[ni][nj]:continue if (ni,nj)==(n-1,n-1): print("YES") exit() heappush(hp,[-np,ni,nj]) print("NO") main()