結果

問題 No.20 砂漠のオアシス
ユーザー qibqib
提出日時 2023-01-19 01:14:36
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 340 ms / 5,000 ms
コード長 1,017 bytes
コンパイル時間 1,536 ms
コンパイル使用メモリ 86,768 KB
実行使用メモリ 82,744 KB
最終ジャッジ日時 2023-09-02 18:13:26
合計ジャッジ時間 6,170 ms
ジャッジサーバーID
(参考情報)
judge13 / judge14
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 82 ms
71,208 KB
testcase_01 AC 82 ms
70,820 KB
testcase_02 AC 80 ms
71,208 KB
testcase_03 AC 137 ms
77,404 KB
testcase_04 AC 154 ms
78,072 KB
testcase_05 AC 340 ms
82,744 KB
testcase_06 AC 273 ms
78,980 KB
testcase_07 AC 272 ms
79,440 KB
testcase_08 AC 185 ms
78,620 KB
testcase_09 AC 272 ms
79,272 KB
testcase_10 AC 80 ms
71,252 KB
testcase_11 AC 79 ms
70,908 KB
testcase_12 AC 147 ms
77,908 KB
testcase_13 AC 157 ms
78,804 KB
testcase_14 AC 165 ms
78,644 KB
testcase_15 AC 158 ms
78,704 KB
testcase_16 AC 179 ms
78,404 KB
testcase_17 AC 162 ms
78,940 KB
testcase_18 AC 164 ms
78,368 KB
testcase_19 AC 171 ms
78,480 KB
testcase_20 AC 137 ms
77,536 KB
権限があれば一括ダウンロードができます

ソースコード

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