結果

問題 No.1812 Uribo Road
コンテスト
ユーザー LyricalMaestro
提出日時 2026-10-16 01:32:44
言語 PyPy3
(8.0.0 + ACL)
コンパイル:
pypy3 -mpy_compile _filename_
実行:
pypy3 _filename_
結果
AC  
実行時間 473 ms / 5,000 ms
+ 546µs
コード長 2,398 bytes
記録
記録タグの例:
初AC ショートコード 純ショートコード 純主流ショートコード 最速実行時間
コンパイル時間 88 ms
コンパイル使用メモリ 64,860 KB
実行使用メモリ 96,100 KB
最終ジャッジ日時 2026-10-16 01:32:53
合計ジャッジ時間 8,320 ms
ジャッジサーバーID
(参考情報)
judge4_0 / judge2_0
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 4
other AC * 30
権限があれば一括ダウンロードができます

ソースコード

diff #
raw source code

## https://yukicoder.me/problems/no/1812

import heapq

MAX_INT = 10 ** 18

def dijkstra(N, edges, start):
    next_nodes = [[] for _ in range(N)]
    for u, v, c in edges:
        next_nodes[u].append((v , c))
        next_nodes[v].append((u , c))


    fix = [MAX_INT] * N
    seen = [MAX_INT] * N
    seen[start] = 0
    queue = []
    heapq.heappush(queue, (0, start))

    while len(queue) > 0:
        cost, v = heapq.heappop(queue)
        if fix[v] < MAX_INT:
            continue

        fix[v] = cost
        for w, c in next_nodes[v]:
            if fix[w] < MAX_INT:
                continue

            new_cost = c + cost
            if seen[w] > new_cost:
                seen[w] = new_cost
                heapq.heappush(queue, (new_cost, w))
    return fix


def main():
    N, M, K = map(int, input().split())
    R = list(map(int, input().split()))
    edges = []
    for _ in range(M):
        A, B, C = map(int, input().split())
        edges.append((A - 1, B - 1, C))

    dists_map = {}
    dists_map[0] = dijkstra(N, edges, 0)
    for r in R:
        u, v, _ = edges[r - 1]
        dists_map[u] = dijkstra(N, edges, u)
        dists_map[v] = dijkstra(N, edges, v)

    dp = [{} for _ in range(2 ** K)]
    dp[0][0] = 0

    for bit in range(2 ** K):
        for start_key, cost in dp[bit].items():
            for r_index in range(K):
                if bit & (1 << r_index) > 0:
                    continue

                new_bit = bit | (1 << r_index)
                u, v, c = edges[R[r_index] - 1]

                new_start_key = v
                new_cost = dists_map[start_key][u] + cost + c
                if new_start_key not in dp[new_bit]:
                    dp[new_bit][new_start_key] = MAX_INT
                dp[new_bit][new_start_key] = min(dp[new_bit][new_start_key], new_cost)
            
                new_start_key = u
                new_cost = dists_map[start_key][v] + cost + c
                if new_start_key not in dp[new_bit]:
                    dp[new_bit][new_start_key] = MAX_INT
                dp[new_bit][new_start_key] = min(dp[new_bit][new_start_key], new_cost)

    answer = MAX_INT
    for start_key, cost in dp[-1].items():
        d = dists_map[start_key][N - 1]
        answer = min(answer, d + cost)
    if answer == MAX_INT:
        print(-1)
    else:
        print(answer)










if __name__ == "__main__":
    main()
0