結果

問題 No.1812 Uribo Road
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-14 23:02:40
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 931 ms / 5,000 ms
コード長 2,453 bytes
コンパイル時間 200 ms
コンパイル使用メモリ 82,852 KB
実行使用メモリ 98,812 KB
最終ジャッジ日時 2024-04-30 16:49:48
合計ジャッジ時間 10,975 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 40 ms
54,532 KB
testcase_01 AC 43 ms
54,420 KB
testcase_02 AC 53 ms
64,468 KB
testcase_03 AC 142 ms
82,096 KB
testcase_04 AC 40 ms
54,808 KB
testcase_05 AC 41 ms
54,768 KB
testcase_06 AC 41 ms
55,204 KB
testcase_07 AC 88 ms
77,128 KB
testcase_08 AC 161 ms
78,304 KB
testcase_09 AC 221 ms
79,164 KB
testcase_10 AC 147 ms
78,164 KB
testcase_11 AC 160 ms
77,808 KB
testcase_12 AC 474 ms
87,892 KB
testcase_13 AC 228 ms
87,324 KB
testcase_14 AC 241 ms
86,736 KB
testcase_15 AC 440 ms
84,684 KB
testcase_16 AC 360 ms
83,352 KB
testcase_17 AC 783 ms
92,828 KB
testcase_18 AC 284 ms
87,376 KB
testcase_19 AC 911 ms
98,392 KB
testcase_20 AC 931 ms
98,812 KB
testcase_21 AC 757 ms
95,068 KB
testcase_22 AC 550 ms
92,860 KB
testcase_23 AC 175 ms
79,244 KB
testcase_24 AC 50 ms
60,796 KB
testcase_25 AC 283 ms
81,832 KB
testcase_26 AC 143 ms
77,680 KB
testcase_27 AC 262 ms
84,396 KB
testcase_28 AC 451 ms
85,664 KB
testcase_29 AC 41 ms
55,428 KB
testcase_30 AC 169 ms
78,528 KB
testcase_31 AC 589 ms
91,160 KB
testcase_32 AC 123 ms
77,760 KB
testcase_33 AC 347 ms
88,088 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(N) ]
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)
ddic[N-1] = Dijkstra(lis,N-1)

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