結果

問題 No.1814 Uribo Road (Easy)
ユーザー brthyyjpbrthyyjp
提出日時 2022-03-13 08:28:49
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 143 ms / 2,000 ms
コード長 2,081 bytes
コンパイル時間 393 ms
コンパイル使用メモリ 81,716 KB
実行使用メモリ 78,072 KB
最終ジャッジ日時 2023-10-17 20:46:47
合計ジャッジ時間 7,252 ms
ジャッジサーバーID
(参考情報)
judge14 / judge12
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 41 ms
53,444 KB
testcase_01 AC 42 ms
53,444 KB
testcase_02 AC 40 ms
53,444 KB
testcase_03 AC 41 ms
53,444 KB
testcase_04 AC 39 ms
53,444 KB
testcase_05 AC 40 ms
53,444 KB
testcase_06 AC 41 ms
53,444 KB
testcase_07 AC 40 ms
53,444 KB
testcase_08 AC 41 ms
53,444 KB
testcase_09 AC 74 ms
72,348 KB
testcase_10 AC 61 ms
66,192 KB
testcase_11 AC 71 ms
72,564 KB
testcase_12 AC 101 ms
76,096 KB
testcase_13 AC 102 ms
76,108 KB
testcase_14 AC 118 ms
76,300 KB
testcase_15 AC 77 ms
73,020 KB
testcase_16 AC 91 ms
75,704 KB
testcase_17 AC 96 ms
75,704 KB
testcase_18 AC 59 ms
66,104 KB
testcase_19 AC 56 ms
63,796 KB
testcase_20 AC 128 ms
77,372 KB
testcase_21 AC 119 ms
76,196 KB
testcase_22 AC 125 ms
77,740 KB
testcase_23 AC 124 ms
77,424 KB
testcase_24 AC 136 ms
77,704 KB
testcase_25 AC 131 ms
77,700 KB
testcase_26 AC 142 ms
78,028 KB
testcase_27 AC 135 ms
78,072 KB
testcase_28 AC 142 ms
77,804 KB
testcase_29 AC 143 ms
78,072 KB
testcase_30 AC 101 ms
77,624 KB
testcase_31 AC 98 ms
77,580 KB
testcase_32 AC 112 ms
76,112 KB
testcase_33 AC 64 ms
68,152 KB
testcase_34 AC 60 ms
66,076 KB
testcase_35 AC 88 ms
76,096 KB
testcase_36 AC 93 ms
76,088 KB
testcase_37 AC 87 ms
76,096 KB
testcase_38 AC 119 ms
76,360 KB
testcase_39 AC 102 ms
75,968 KB
testcase_40 AC 108 ms
76,112 KB
testcase_41 AC 113 ms
77,780 KB
testcase_42 AC 110 ms
76,540 KB
testcase_43 AC 111 ms
76,164 KB
testcase_44 AC 124 ms
76,576 KB
testcase_45 AC 93 ms
75,828 KB
testcase_46 AC 126 ms
77,904 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import sys
import io, os
input = io.BytesIO(os.read(0,os.fstat(0).st_size)).readline

import heapq
INF = 10**18
def dijkstra(s, edge):
    n = len(edge)
    dist = [INF]*n
    dist[s] = 0
    edgelist = []
    heapq.heappush(edgelist,(dist[s], s))
    while edgelist:
        minedge = heapq.heappop(edgelist)
        if dist[minedge[1]] < minedge[0]:
            continue
        v = minedge[1]
        for e in edge[v]:
            if dist[e[1]] > dist[v]+e[0]:
                dist[e[1]] = dist[v]+e[0]
                heapq.heappush(edgelist,(dist[e[1]], e[1]))
    return dist

def main():

    n, m, k = map(int, input().split())
    R = list(map(int, input().split()))
    R = [r-1 for r in R]
    toid = {}
    for i, r in enumerate(R):
        toid[r] = i

    g = [[] for i in range(n)]
    ABC = []
    for i in range(m):
        a, b, c = map(int, input().split())
        a, b = a-1, b-1
        g[a].append((c, b))
        g[b].append((c, a))
        if i in toid:
            ABC.append((a, b, c))

    dists = dijkstra(0, g)
    distt = dijkstra(n-1, g)
    D = []
    for i in range(k):
        a, b = ABC[i][0], ABC[i][1]
        dista = dijkstra(a, g)
        distb = dijkstra(b, g)
        D.append([dista,distb])


    dp = [[[INF]*2 for i in range(k)] for i in range(1<<k)]
    for i, (a, b, c) in enumerate(ABC):
        dp[1<<i][i][0] = dists[b]+c
        dp[1<<i][i][1] = dists[a]+c

    for s in range(1<<k):
        for i in range(k):
            if (s>>i)&1:
                continue
            ns = s|(1<<i)
            c = ABC[i][2]
            for j in range(k):
                if not ((s>>j)&1):
                    continue
                if i == j:
                    continue
                for x in range(2):
                    for y in range(2):
                        dp[ns][i][x] = min(dp[ns][i][x], dp[s][j][y]+D[i][1-x][ABC[j][y]]+c)
    ans = INF
    for i in range(k):
        for x in range(2):
            v = ABC[i][x]
            ans = min(ans, dp[(1<<k)-1][i][x]+distt[v])
    print(ans)

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