結果

問題 No.20 砂漠のオアシス
ユーザー Yuta123456Yuta123456
提出日時 2020-05-22 14:43:32
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 332 ms / 5,000 ms
コード長 1,596 bytes
コンパイル時間 215 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 35,456 KB
最終ジャッジ日時 2024-04-15 04:30:05
合計ジャッジ時間 3,126 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 30 ms
11,008 KB
testcase_01 AC 29 ms
10,880 KB
testcase_02 AC 31 ms
11,008 KB
testcase_03 AC 41 ms
12,288 KB
testcase_04 AC 43 ms
12,160 KB
testcase_05 AC 295 ms
30,208 KB
testcase_06 AC 329 ms
35,456 KB
testcase_07 AC 330 ms
35,328 KB
testcase_08 AC 332 ms
35,456 KB
testcase_09 AC 326 ms
35,328 KB
testcase_10 AC 28 ms
10,880 KB
testcase_11 AC 28 ms
10,880 KB
testcase_12 AC 38 ms
11,904 KB
testcase_13 AC 41 ms
12,160 KB
testcase_14 AC 51 ms
12,928 KB
testcase_15 AC 45 ms
12,416 KB
testcase_16 AC 77 ms
15,744 KB
testcase_17 AC 64 ms
14,208 KB
testcase_18 AC 68 ms
14,976 KB
testcase_19 AC 77 ms
15,744 KB
testcase_20 AC 34 ms
11,520 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
def dijkstra(start,graph):
    INF = 10 ** 15
    dist = [INF] * (n*n)
    dist[start] = 0
    q = [(0,start)]
    while q:
        d,v = heappop(q)
        if dist[v] < d:
            continue
        for w,a in graph[v]:
            d1 = d + a
            if dist[w] > d1:
                dist[w] = d1
                heappush(q, (d1,w))
    return dist
#このまま適当にはって使える感じではない?
#隣接リスト適当に渡せば動く。重みを追加するのをわすれずに
def check(s,t):
    if (0 <=  s <= n - 1) and (0 <= t <= n - 1):
        return True
    else:
        return False
def grid_2_graph(grid):
    adjacent_list = [[] for i in range(h*w)]
    for i in range(h):
        for j in range(w):
            for n_x,n_y in [(j+1,i),(j-1,i),(j,i+1),(j,i-1)]:
                if check(n_x,n_y):
                    #(i,j) -> (n_y, n_x)に辺をつなぐ
                    adjacent_list[i*w+j].append([(n_y)*w + (n_x),grid[n_y][n_x]])
    return adjacent_list




n,v,o_x,o_y = map(int,input().split())
h,w = n,n
graph = []
for i in range(n):
    graph.append(list(map(int, input().split())))
graph = grid_2_graph(graph)
dist_from_start = dijkstra(0,graph)
oasis_node = 0
if o_x != 0 and o_y != 0:
    oasis_node = (o_x - 1) + (o_y - 1) * n
    dist_from_oasis = dijkstra(oasis_node,graph)

ans = "NO"
if dist_from_start[n*n-1] < v:
    ans = "YES"
if o_x != 0 and o_y != 0:
    v -= dist_from_start[oasis_node]
    if v > 0:      
        v *= 2
        if dist_from_oasis[n*n-1] < v:
            ans = "YES"
print(ans)
0