結果

問題 No.20 砂漠のオアシス
ユーザー takakintakakin
提出日時 2020-05-25 21:36:53
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 380 ms / 5,000 ms
コード長 1,097 bytes
コンパイル時間 103 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 23,104 KB
最終ジャッジ日時 2024-04-21 03:53:38
合計ジャッジ時間 3,592 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
10,880 KB
testcase_01 AC 32 ms
10,880 KB
testcase_02 AC 33 ms
10,880 KB
testcase_03 AC 51 ms
11,392 KB
testcase_04 AC 52 ms
11,520 KB
testcase_05 AC 318 ms
20,344 KB
testcase_06 AC 380 ms
23,096 KB
testcase_07 AC 380 ms
23,104 KB
testcase_08 AC 374 ms
23,096 KB
testcase_09 AC 373 ms
23,104 KB
testcase_10 AC 32 ms
11,008 KB
testcase_11 AC 32 ms
10,880 KB
testcase_12 AC 45 ms
11,264 KB
testcase_13 AC 48 ms
11,392 KB
testcase_14 AC 63 ms
11,904 KB
testcase_15 AC 54 ms
11,520 KB
testcase_16 AC 99 ms
13,184 KB
testcase_17 AC 78 ms
12,416 KB
testcase_18 AC 88 ms
12,800 KB
testcase_19 AC 99 ms
13,056 KB
testcase_20 AC 39 ms
11,136 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
input=lambda: sys.stdin.readline().rstrip()
n,v,ox,oy=map(int,input().split())
L=[[int(i) for i in input().split()] for _ in range(n)]
nn=n**2
D=((1,0),(0,1),(-1,0),(0,-1))
edge=[[] for i in range(nn)]
for i in range(nn):
  ix,iy=i//n,i%n
  for dx,dy in D:
    if 0<=ix+dx<n and 0<=iy+dy<n:
      edge[i].append(L[ix+dx][iy+dy]*10**6+(ix+dx)*n+iy+dy)

import heapq
def dijkstra_heap(s):
  d=[float("inf")]*nn
  used=[True]*nn
  d[s]=0
  used[s]=False
  edgelist=[]
  for e in edge[s]:
    heapq.heappush(edgelist,e)
  while edgelist:
    minedge=heapq.heappop(edgelist)
    minedge_cost=minedge//(10**6)
    minedge_v=minedge%(10**6)
    if not used[minedge_v]:
      continue
    d[minedge_v] = minedge_cost
    used[minedge_v] = False
    for e in edge[minedge_v]:
      e_cost=e//(10**6)
      e_v=e%(10**6)
      if used[e_v]:
        heapq.heappush(edgelist,e+minedge_cost*10**6)
  return d

D1=dijkstra_heap(0)
D2=dijkstra_heap((oy-1)*n+ox-1)

if D1[-1]<v:
  print("YES")
elif ox*oy!=0 and D1[(oy-1)*n+ox-1]<v and D2[-1]<2*(v-D1[(oy-1)*n+ox-1]):
  print("YES")
else:
  print("NO")
0