結果

問題 No.3111 Toll Optimization
ユーザー Mistletoe
提出日時 2025-04-19 00:17:23
言語 Python3
(3.13.1 + numpy 2.2.1 + scipy 1.14.1)
結果
AC  
実行時間 2,451 ms / 5,000 ms
コード長 1,528 bytes
コンパイル時間 171 ms
コンパイル使用メモリ 12,416 KB
実行使用メモリ 89,940 KB
最終ジャッジ日時 2025-04-19 00:18:13
合計ジャッジ時間 48,902 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 70
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq

def solve():
    import sys
    input = sys.stdin.read
    data = input().split()
    idx = 0
    N = int(data[idx])
    idx += 1
    M = int(data[idx])
    idx += 1
    K = int(data[idx])
    idx += 1
    
    C = list(map(int, data[idx:idx+M]))
    idx += M
    
    adj = [[] for _ in range(N+1)]
    for i in range(M):
        u = int(data[idx])
        idx += 1
        v = int(data[idx])
        idx += 1
        adj[u].append((v, i))
        adj[v].append((u, i))
    
    INF = 1 << 60
    dist = [[INF] * (K+1) for _ in range(N+1)]
    dist[1][0] = 0
    heap = []
    heapq.heappush(heap, (0, 1, 0))
    
    found = False
    answer = INF
    
    while heap:
        current_cost, u, used = heapq.heappop(heap)
        if u == N:
            answer = min(answer, current_cost)
            found = True
            continue
        if current_cost > dist[u][used]:
            continue
        for (v, idx_bridge) in adj[u]:
            cost = C[idx_bridge]
            # Option 1: do not use coupon
            if dist[v][used] > current_cost + cost:
                dist[v][used] = current_cost + cost
                heapq.heappush(heap, (dist[v][used], v, used))
            # Option 2: use coupon if possible
            if used < K:
                if dist[v][used + 1] > current_cost:
                    dist[v][used + 1] = current_cost
                    heapq.heappush(heap, (dist[v][used + 1], v, used + 1))
    
    if found:
        print(min(dist[N]))
    else:
        print(-1)

solve()
0