結果

問題 No.3111 Toll Optimization
ユーザー issaimaru
提出日時 2025-04-20 11:31:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 2,238 ms / 5,000 ms
コード長 968 bytes
コンパイル時間 426 ms
コンパイル使用メモリ 12,544 KB
実行使用メモリ 71,064 KB
最終ジャッジ日時 2025-04-20 11:32:26
合計ジャッジ時間 50,738 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

from heapq import heappush, heappop
import math

def solve():
    N, M, K = map(int, input().split())
    C = list(map(int, input().split()))
    
    graph = [[] for _ in range(N + 1)]
    for i in range(M):
        u, v = map(int, input().split())
        graph[u].append((v, C[i]))
        graph[v].append((u, C[i]))
    
    dist = [[math.inf] * (K + 1) for _ in range(N + 1)]
    dist[1][K] = 0  
    
    pq = [(0, 1, K)]
    while pq:
        cost, v, k = heappop(pq)
        
        if cost > dist[v][k]:
            continue
        
        for u, c in graph[v]:
            if dist[u][k] > cost + c:
                dist[u][k] = cost + c
                heappush(pq, (cost + c, u, k))
            
            if k > 0 and dist[u][k - 1] > cost:
                dist[u][k - 1] = cost
                heappush(pq, (cost, u, k - 1))
    
    min_cost = min(dist[N])
    if min_cost == math.inf:
        print(-1)
    else:
        print(min_cost)

solve()


0