結果
| 問題 | No.20 砂漠のオアシス | 
| コンテスト | |
| ユーザー |  | 
| 提出日時 | 2020-05-22 14:43:32 | 
| 言語 | Python3 (3.13.1 + numpy 2.2.1 + scipy 1.14.1) | 
| 結果 | 
                                AC
                                 
                             | 
| 実行時間 | 295 ms / 5,000 ms | 
| コード長 | 1,596 bytes | 
| コンパイル時間 | 151 ms | 
| コンパイル使用メモリ | 12,800 KB | 
| 実行使用メモリ | 35,456 KB | 
| 最終ジャッジ日時 | 2024-10-04 15:48:43 | 
| 合計ジャッジ時間 | 3,058 ms | 
| ジャッジサーバーID (参考情報) | judge2 / judge5 | 
(要ログイン)
| ファイルパターン | 結果 | 
|---|---|
| other | AC * 21 | 
ソースコード
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)
            
            
            
        