結果

問題 No.3111 Toll Optimization
ユーザー Nzt3
提出日時 2025-04-15 11:28:24
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,350 ms / 5,000 ms
コード長 969 bytes
コンパイル時間 551 ms
コンパイル使用メモリ 82,372 KB
実行使用メモリ 155,000 KB
最終ジャッジ日時 2025-04-15 11:29:42
合計ジャッジ時間 66,612 ms
ジャッジサーバーID
(参考情報)
judge1 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

n,m,k = map(int, input().split())
c = list(map(int, input().split()))
graph = [[] for _ in range(n)]

for i in range(m):
    u,v = map(int, input().split())
    graph[u-1].append((v-1, c[i]))
    graph[v-1].append((u-1, c[i]))

start = 0
end = n-1

dist = [[float('inf')] * (k + 1) for _ in range(n)]

dist[start][0] = 0

queue = [(0, start, 0)]

while queue:
    now_cost, now_pos, coupon = heapq.heappop(queue)

    if dist[now_pos][coupon] < now_cost:
        continue

    for to, edge_cost in graph[now_pos]:
        if coupon < k:
            if dist[to][coupon + 1] > now_cost:
                dist[to][coupon + 1] = now_cost
                heapq.heappush(queue, (now_cost, to, coupon + 1))

        if dist[to][coupon] > now_cost + edge_cost:
            dist[to][coupon] = now_cost + edge_cost
            heapq.heappush(queue, (now_cost + edge_cost, to, coupon))

ans = min(dist[end])

if ans == float('inf'):
    print(-1)
else:
    print(ans)
0