結果

問題 No.20 砂漠のオアシス
ユーザー nsd_fbnsd_fb
提出日時 2015-02-20 13:10:24
言語 Python2
(2.7.18)
結果
AC  
実行時間 216 ms / 5,000 ms
コード長 1,042 bytes
コンパイル時間 204 ms
コンパイル使用メモリ 7,040 KB
実行使用メモリ 9,088 KB
最終ジャッジ日時 2024-04-21 06:23:17
合計ジャッジ時間 2,204 ms
ジャッジサーバーID
(参考情報)
judge4 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 11 ms
6,656 KB
testcase_01 AC 10 ms
6,656 KB
testcase_02 AC 10 ms
6,656 KB
testcase_03 AC 17 ms
6,528 KB
testcase_04 AC 17 ms
6,656 KB
testcase_05 AC 216 ms
8,716 KB
testcase_06 AC 206 ms
9,088 KB
testcase_07 AC 204 ms
9,088 KB
testcase_08 AC 113 ms
8,064 KB
testcase_09 AC 207 ms
9,088 KB
testcase_10 AC 12 ms
6,656 KB
testcase_11 AC 12 ms
6,656 KB
testcase_12 AC 19 ms
6,528 KB
testcase_13 AC 21 ms
6,784 KB
testcase_14 AC 28 ms
6,656 KB
testcase_15 AC 23 ms
6,656 KB
testcase_16 AC 49 ms
7,040 KB
testcase_17 AC 37 ms
6,784 KB
testcase_18 AC 44 ms
6,912 KB
testcase_19 AC 50 ms
6,944 KB
testcase_20 AC 15 ms
6,528 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

direction = [(1, 0), (0, 1), (-1, 0), (0, -1)]

def dijkstra(sx, sy, L):
    N = len(L)
    dist = [[float("inf")] * N for _ in xrange(N)]
    dist[sy][sx] = 0
    queue = [(0, sx, sy)]

    while queue:
        d, x, y = heapq.heappop(queue)

        if dist[y][x] < d:
            continue

        for dx, dy in direction:
            nx = x + dx
            ny = y + dy

            if 0 <= nx < N and 0 <= ny < N and dist[ny][nx] > d + L[ny][nx]:
                dist[ny][nx] = d + L[ny][nx]
                heapq.heappush(queue, (dist[ny][nx], nx, ny))

    return dist

def solve():
    N, V, Ox, Oy = map(int, raw_input().split())
    L = [map(int, raw_input().split()) for _ in xrange(N)]

    Ox -= 1
    Oy -= 1

    dist = dijkstra(0, 0, L)
    
    if dist[N - 1][N - 1] < V:
        return True

    if Ox >= 0 and dist[Oy][Ox] < V:
        V = (V - dist[Oy][Ox]) * 2
        dist = dijkstra(Ox, Oy, L)
        if dist[N - 1][N - 1] < V:
            return True

    return False

print 'YES' if solve() else 'NO'
0