結果

問題 No.1812 Uribo Road
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-14 22:57:04
言語 PyPy3
(7.3.15)
結果
RE  
実行時間 -
コード長 2,423 bytes
コンパイル時間 139 ms
コンパイル使用メモリ 82,636 KB
実行使用メモリ 99,632 KB
最終ジャッジ日時 2024-04-30 16:44:12
合計ジャッジ時間 9,369 ms
ジャッジサーバーID
(参考情報)
judge4 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
54,320 KB
testcase_01 AC 42 ms
54,224 KB
testcase_02 AC 56 ms
64,960 KB
testcase_03 AC 141 ms
82,108 KB
testcase_04 RE -
testcase_05 RE -
testcase_06 RE -
testcase_07 AC 83 ms
77,316 KB
testcase_08 AC 150 ms
78,588 KB
testcase_09 AC 181 ms
78,880 KB
testcase_10 AC 133 ms
78,156 KB
testcase_11 AC 153 ms
78,304 KB
testcase_12 RE -
testcase_13 RE -
testcase_14 AC 227 ms
87,268 KB
testcase_15 AC 415 ms
87,504 KB
testcase_16 RE -
testcase_17 AC 723 ms
94,120 KB
testcase_18 AC 278 ms
93,852 KB
testcase_19 AC 843 ms
99,460 KB
testcase_20 AC 839 ms
99,632 KB
testcase_21 AC 682 ms
97,180 KB
testcase_22 AC 507 ms
94,392 KB
testcase_23 AC 154 ms
78,928 KB
testcase_24 AC 47 ms
62,248 KB
testcase_25 AC 234 ms
82,452 KB
testcase_26 AC 129 ms
77,984 KB
testcase_27 AC 243 ms
87,292 KB
testcase_28 AC 386 ms
85,696 KB
testcase_29 AC 39 ms
54,180 KB
testcase_30 AC 160 ms
79,280 KB
testcase_31 AC 530 ms
91,668 KB
testcase_32 AC 115 ms
77,892 KB
testcase_33 AC 313 ms
88,376 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

"""

bitdpっぽくやるのね…
10^7...
厳しめ

dp[bit][最後][LorR] = mincost

"""

from sys import stdin

import heapq
def Dijkstra(lis,start):

    ret = [float("inf")] * len(lis)
    ret[start] = 0
    q = [(0,start)]

    while len(q) > 0:
        ncost,now = heapq.heappop(q)
        if ncost != ret[now]:
            continue

        for nex,ecost in lis[now]:
            if ret[nex] > ncost + ecost:
                ret[nex] = ncost + ecost
                heapq.heappush(q , (ret[nex] , nex))
    return ret

N,M,K = map(int,stdin.readline().split())

R = list(map(int,stdin.readline().split()))
for i in range(K):
    R[i] -= 1

lis = [ [] for i in range(M) ]
ABC = []

for i in range(M):

    A,B,C = map(int,stdin.readline().split())
    A -= 1
    B -= 1

    ABC.append( (A,B,C) )
    lis[A].append( (B,C) )
    lis[B].append( (A,C) )

ddic = {}
ddic[0] = Dijkstra(lis,0)

for nr in R:
    l,r,_ = ABC[nr]

    if l not in ddic:
        ddic[l] = Dijkstra(lis,l)
    if r not in ddic:
        ddic[r] = Dijkstra(lis,r)

#print (ddic)

inf = float("inf")

dp = [ [[inf,inf] for j in range(K)] for i in range(2**K) ]

# dp[bit][最後][LorR] = mincost

for i,nr in enumerate(R):

    l,r,c = ABC[nr]

    dp[2**i][i][0] = ddic[0][r] + c
    dp[2**i][i][1] = ddic[0][l] + c

def popcnt(x):
    ret = 0
    while x:
        ret += x%2
        x //= 2
    return ret

for bit in range(2**K):
    for lastNR in range(K):
        for lastSide in range(2):

            lastL,lastR,_ = ABC[R[lastNR]]
            lastV = [lastL,lastR][lastSide]
            
            if dp[bit][lastNR][lastSide] == inf:
                continue

            for newNR in range(K):
                newbit = 2**newNR | bit
                if bit == newbit:
                    continue

                newL,newR,newC = ABC[R[newNR]]

                
                dp[newbit][newNR][0] = min(dp[newbit][newNR][0] ,
                                           dp[bit][lastNR][lastSide] + ddic[lastV][newR] + newC )
                dp[newbit][newNR][1] = min(dp[newbit][newNR][1] ,
                                           dp[bit][lastNR][lastSide] + ddic[lastV][newL] + newC )

ans = inf
bit = 2**K-1
for lastNR in range(K):
    for lastSide in range(2):

        lastL,lastR,_ = ABC[R[lastNR]]
        lastV = [lastL,lastR][lastSide]

        ans = min(ans , dp[bit][lastNR][lastSide] + ddic[lastV][N-1])

print (ans)
0