結果

問題 No.1812 Uribo Road
ユーザー ShirotsumeShirotsume
提出日時 2021-12-28 12:34:27
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
TLE  
実行時間 -
コード長 2,055 bytes
コンパイル時間 79 ms
コンパイル使用メモリ 13,056 KB
実行使用メモリ 20,128 KB
最終ジャッジ日時 2024-04-10 02:16:55
合計ジャッジ時間 7,182 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 31 ms
11,008 KB
testcase_01 AC 32 ms
11,136 KB
testcase_02 AC 105 ms
11,008 KB
testcase_03 TLE -
testcase_04 -- -
testcase_05 -- -
testcase_06 -- -
testcase_07 -- -
testcase_08 -- -
testcase_09 -- -
testcase_10 -- -
testcase_11 -- -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
testcase_19 -- -
testcase_20 -- -
testcase_21 -- -
testcase_22 -- -
testcase_23 -- -
testcase_24 -- -
testcase_25 -- -
testcase_26 -- -
testcase_27 -- -
testcase_28 -- -
testcase_29 -- -
testcase_30 -- -
testcase_31 -- -
testcase_32 -- -
testcase_33 -- -
権限があれば一括ダウンロードができます

ソースコード

diff #

import heapq
def dijkstra(s, n, graph):
    INF = 10 ** 18
    dist = [INF] * n
    dist[s] = 0
    bef = [0] * n
    bef[s] = s
    hq = [(0, s)] #距離、地点を記録したヒープを作る
    heapq.heapify(hq)
    visit = [False] * n #訪れたかの判定
    while(len(hq) > 0):
        c, v = heapq.heappop(hq) #ヒープから地点を1つ持ってくる
        visit[v] = True
        if c > dist[v]:
            continue
        for to, cost in graph[v]:
            if visit[to] == False and dist[v] + cost < dist[to]:
                dist[to] =  cost + dist[v]
                bef[to] = v
                heapq.heappush(hq, (dist[to], to))
    '''
    return bef
    '''
    return dist

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 + 1)]
r_cost = 0
r_edges = []
r.sort()
for i in range(m):
    a, b, c = map(int,input().split())
    graph[a - 1].append((b - 1, c))
    graph[b - 1].append((a - 1, c))
    if i in r:
        r_cost += c
        r_edges.append([a - 1, b - 1])
dist = [[] for _ in range(n)]
for a, b in r_edges:
    dist[a] = dijkstra(a, n, graph)
    dist[b] = dijkstra(b, n, graph)
dist[0] = dijkstra(0, n, graph)
dist[n - 1] = dijkstra(n - 1, n, graph)
INF = 10 ** 12
ans = INF
for i in range(2 ** k):
    dp = [[INF] * k for _ in range(2 ** k)]
    for j in range(k):
        p = r_edges[j][1 & (i >> j)]
        dp[2 ** j][j] = dist[0][p] + r_cost
    
    for i2 in range(0, 2 ** k):
        for x in range(k):
            fr = r_edges[x][(1 & (i >> x)) ^ 1]
            if not(i2 & (2 ** x)):
                continue
            for y in range(k):
                to = r_edges[y][1 & (i >> y)]
                mask = i2 | (2 ** y)
                d = dp[i2][x] + dist[fr][to]
                if (i2 & (2 ** y) == 0):
                    dp[mask][y] = min(dp[mask][y], d)
    c = INF
    for z in range(k):
        p = r_edges[z][(1 & (i >> z)) ^ 1]
        c = dp[-1][z] + dist[n - 1][p]
    ans = min(ans, c)



print(ans)



0