結果

問題 No.20 砂漠のオアシス
ユーザー qibqib
提出日時 2023-01-19 01:14:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 319 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 206 ms
コンパイル使用メモリ 82,304 KB
実行使用メモリ 81,152 KB
最終ジャッジ日時 2024-06-12 00:41:22
合計ジャッジ時間 4,040 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

INF = 1 << 60

n, v, ox, oy = map(int, input().split())
ox -= 1
oy -= 1
l = [list(map(int, input().split())) for _ in range(n)]

dx = [1, 0, -1, 0]
dy = [0, 1, 0, -1]

def cost(sx, sy, gx, gy):
  dist = [[INF for _ in range(n)] for _ in range(n)]
  dist[sx][sy] = 0
  hp = [(0, sx, sy)]
  while len(hp) > 0:
    cd, cx, cy = heapq.heappop(hp)
    if cd > dist[cx][cy]:
      continue

    for i in range(4):
      nx = cx + dx[i]
      ny = cy + dy[i]
      if nx < 0 or nx >= n or ny < 0 or ny >= n:
        continue
      if dist[nx][ny] > dist[cx][cy] + l[nx][ny]:
        dist[nx][ny] = dist[cx][cy] + l[nx][ny]
        heapq.heappush(hp, (dist[nx][ny], nx, ny))

  return dist[gx][gy]

if ox == -1 and oy == -1:
  rest = v - cost(0, 0, n - 1, n - 1)
  if rest <= 0:
    print("NO")
  else:
    print("YES")
else:
  rest = v - cost(0, 0, oy, ox)
  if rest <= 0:
    print("NO")
  else:
    rest = 2 * rest - cost(oy, ox, n - 1, n - 1)
    if rest <= 0:
      print("NO")
    else:
      print("YES")
0