結果

問題 No.1814 Uribo Road (Easy)
ユーザー ShirotsumeShirotsume
提出日時 2021-12-30 00:04:33
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 287 ms / 2,000 ms
コード長 1,547 bytes
コンパイル時間 198 ms
コンパイル使用メモリ 82,308 KB
実行使用メモリ 79,396 KB
最終ジャッジ日時 2024-11-21 23:00:50
合計ジャッジ時間 7,261 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
import sys
input = lambda: sys.stdin.readline().rstrip()
n, m, k = map(int,input().split())
r = list(map(int,input().split()))
for i in range(k):
    r[i] -= 1
graph = [[] for _ in range(n)]
r_n = len(r)
r_edges = {}
r.sort()
for i in range(m):
    a, b, c = map(int,input().split())
    if b < a: a, b = b, a
    graph[a - 1].append((b - 1, c))
    graph[b - 1].append((a - 1, c))
    if i in r:
        r_edges[(a - 1, b - 1)] = r.index(i)

def dijkstra(n, graph):
    INF = 10 ** 15
    dist = [[INF] * (2 ** r_n) for _ in range(n)]
    dist[0][0] = 0
    hq = []
    heapq.heappush(hq, (0, 0, 0))
    visit = [[False] * (2 ** r_n) for _ in range(n)]
    visit[0][0] = True
    while hq:
            c, v, bit = heapq.heappop(hq)
            if c > dist[v][bit]:
                continue
            
            visit[v][bit] = True
            for to, cost in graph[v]:
                if (min(v, to), max(v, to)) in r_edges:
                    msk = 1 << r_edges[(min(v, to), max(v, to))]
                    if visit[to][bit | msk] == False and dist[v][bit] + cost < dist[to][bit | msk]:
                        dist[to][bit | msk] = dist[v][bit] + cost
                        heapq.heappush(hq, (dist[to][bit | msk], to, bit | msk))
                elif visit[to][bit] == False and dist[v][bit] + cost < dist[to][bit]:
                        dist[to][bit] = dist[v][bit] + cost
                        heapq.heappush(hq, (dist[to][bit], to, bit))

    return dist


ans = dijkstra(n, graph)
print(ans[n - 1][2 ** r_n - 1])
0