結果

問題 No.3111 Toll Optimization
ユーザー Daniel Agatan
提出日時 2025-04-19 22:09:29
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
TLE  
実行時間 -
コード長 1,331 bytes
コンパイル時間 478 ms
コンパイル使用メモリ 12,160 KB
実行使用メモリ 37,568 KB
最終ジャッジ日時 2025-04-19 22:09:44
合計ジャッジ時間 14,130 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample -- * 3
other TLE * 1 -- * 69
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys

def dijkstra(n, m, k, bridges, costs):
    INF = sys.maxsize
    min_cost = [[INF] * (k + 1) for _ in range(n + 1)]
    min_cost[1][0] = 0
    pq = [(0, 1, 0)]

    while pq:
        cost, city, coupons_used = heapq.heappop(pq)

        if coupons_used == k:
            continue
        
        if cost > min_cost[city][coupons_used]:
            continue

        for i in range(m):
            u, v = bridges[i]
            c = costs[i]
            if city == u:
                next_city = v
            elif city == v:
                next_city = u
            else:
                continue

            if cost + c < min_cost[next_city][coupons_used]:
                min_cost[next_city][coupons_used] = cost + c
                heapq.heappush(pq, (cost + c, next_city, coupons_used))

            if coupons_used < k and cost < min_cost[next_city][coupons_used + 1]:
                min_cost[next_city][coupons_used + 1] = cost
                heapq.heappush(pq, (cost, next_city, coupons_used + 1))

    result = min(min_cost[n][i] for i in range(k + 1))
    return result if result != INF else -1

n, m, k = map(int, input().split())
costs = list(map(int, input().split()))
bridges = [tuple(map(int, input().split())) for _ in range(m)]

result = dijkstra(n, m, k, bridges, costs)
print(result)
0