結果

問題 No.20 砂漠のオアシス
ユーザー rpy3cpprpy3cpp
提出日時 2015-08-08 10:49:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
WA  
(最新)
AC  
(最初)
実行時間 -
コード長 1,250 bytes
コンパイル時間 188 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 13,056 KB
最終ジャッジ日時 2024-10-13 05:24:31
合計ジャッジ時間 2,024 ms
ジャッジサーバーID
(参考情報)
judge4 / judge3
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
other AC * 20 WA * 1
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq


def read_data():
    N, V, Ox, Oy = map(int, input().split())
    data = [list(map(int, input().split())) for n in range(N)]
    return N, V, Ox - 1, Oy - 1, data


def solve(N, V, Ox, Oy, data):
    Sx, Sy = 0, 0
    Gx, Gy = N - 1, N - 1
    distS = dijkstra(Sx, Sy, data, N, V)
    if distS[Gx][Gy] < V:
        return True
    if Ox == -1 and Oy == -1:
        return False
    newV = (V - distS[Ox][Oy]) * 2
    if newV <= 0:
        return False
    distO = dijkstra(Ox, Oy, data, N, newV)
    if distO[Gx][Gy] < newV:
        return True
    return False


def dijkstra(x, y, data, N, V):
    pq = [(0, x, y)]
    dist = [[float('inf')] * N for n in range(N)]
    dist[0][0] = 0
    while pq:
        d, x, y = heapq.heappop(pq)
        for nx, ny in [(x-1, y), (x+1, y), (x, y-1), (x, y+1)]:
            if nx < 0 or nx >= N or ny < 0 or ny >= N:
                continue
            newd = d + data[ny][nx]
            if newd >= V or newd >= dist[nx][ny]:
                continue
            dist[nx][ny] = newd
            heapq.heappush(pq, (newd, nx, ny))
    return dist

if __name__ == '__main__':
    N, V, Ox, Oy, data = read_data()
    if solve(N, V, Ox, Oy, data):
        print('YES')
    else:
        print('NO')
0