結果

問題 No.20 砂漠のオアシス
ユーザー terrafarmterrafarm
提出日時 2021-02-14 16:42:48
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 330 ms / 5,000 ms
コード長 1,354 bytes
コンパイル時間 159 ms
コンパイル使用メモリ 82,328 KB
実行使用メモリ 86,592 KB
最終ジャッジ日時 2024-07-22 02:08:03
合計ジャッジ時間 4,807 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 21
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import logging
import sys
from inspect import currentframe

sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline
logging.basicConfig(level=logging.DEBUG)


def dbg(*args):
    id2names = {id(v): k for k, v in currentframe().f_back.f_locals.items()}
    logging.debug(", ".join(id2names.get(id(arg), "???") + " = " + repr(arg) for arg in args))


def dijkstra_grid(sx, sy, grid):
    d = [[float('inf')] * n for _ in range(n)]
    d[sx][sy] = 0
    pq = [(0, sx, sy)]
    while pq:
        c, x, y = heapq.heappop(pq)
        if d[x][y] < c:
            continue
        for dx, dy in ((-1, 0), (1, 0), (0, 1), (0, -1)):
            nx, ny = x + dx, y + dy
            if 0 <= nx < n and 0 <= ny < n and c + grid[nx][ny] < d[nx][ny]:
                d[nx][ny] = c + grid[nx][ny]
                heapq.heappush(pq, (d[nx][ny], nx, ny))
    return d


n, v, oy, ox = map(int, input().split())
l = [list(map(int, input().split())) for _ in range(n)]
d1 = dijkstra_grid(0, 0, l)
if ox == 0 and oy == 0:
    if d1[n - 1][n - 1] < v:
        print("YES")
    else:
        print("NO")
else:
    ox -= 1
    oy -= 1
    d2 = dijkstra_grid(ox, oy, l)
    dbg(d1)
    dbg(d2)
    if d1[n - 1][n - 1] < v:
        print("YES")
    elif d1[ox][oy] < v and d2[n - 1][n - 1] < 2 * (v - d1[ox][oy]):
        print("YES")
    else:
        print("NO")
0