結果

問題 No.20 砂漠のオアシス
ユーザー rpy3cpprpy3cpp
提出日時 2017-08-26 02:58:03
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 153 ms / 5,000 ms
コード長 1,250 bytes
コンパイル時間 236 ms
コンパイル使用メモリ 12,800 KB
実行使用メモリ 13,312 KB
最終ジャッジ日時 2024-04-23 16:23:22
合計ジャッジ時間 2,176 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 29 ms
10,880 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 28 ms
10,880 KB
testcase_03 AC 32 ms
10,880 KB
testcase_04 AC 32 ms
11,008 KB
testcase_05 AC 138 ms
12,928 KB
testcase_06 AC 47 ms
11,904 KB
testcase_07 AC 153 ms
13,312 KB
testcase_08 AC 66 ms
11,904 KB
testcase_09 AC 131 ms
12,928 KB
testcase_10 AC 27 ms
10,880 KB
testcase_11 AC 27 ms
10,880 KB
testcase_12 AC 31 ms
11,136 KB
testcase_13 AC 32 ms
11,008 KB
testcase_14 AC 39 ms
11,008 KB
testcase_15 AC 36 ms
11,008 KB
testcase_16 AC 53 ms
11,264 KB
testcase_17 AC 45 ms
11,008 KB
testcase_18 AC 49 ms
11,136 KB
testcase_19 AC 53 ms
11,264 KB
testcase_20 AC 31 ms
10,880 KB
権限があれば一括ダウンロードができます

ソースコード

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[x][y] = 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