結果

問題 No.1812 Uribo Road
ユーザー 👑 SPD_9X2SPD_9X2
提出日時 2022-01-14 23:02:40
言語 PyPy3
(7.3.13)
結果
AC  
実行時間 924 ms / 5,000 ms
コード長 2,453 bytes
コンパイル時間 589 ms
コンパイル使用メモリ 87,132 KB
実行使用メモリ 99,588 KB
最終ジャッジ日時 2023-08-12 21:47:00
合計ジャッジ時間 12,903 ms
ジャッジサーバーID
(参考情報)
judge11 / judge2
このコードへのチャレンジ(β)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 78 ms
71,244 KB
testcase_01 AC 76 ms
71,248 KB
testcase_02 AC 91 ms
76,084 KB
testcase_03 AC 178 ms
83,512 KB
testcase_04 AC 76 ms
71,172 KB
testcase_05 AC 76 ms
70,928 KB
testcase_06 AC 79 ms
71,172 KB
testcase_07 AC 116 ms
77,868 KB
testcase_08 AC 193 ms
79,940 KB
testcase_09 AC 237 ms
80,240 KB
testcase_10 AC 167 ms
78,624 KB
testcase_11 AC 197 ms
79,296 KB
testcase_12 AC 474 ms
89,196 KB
testcase_13 AC 265 ms
89,048 KB
testcase_14 AC 276 ms
88,504 KB
testcase_15 AC 470 ms
87,024 KB
testcase_16 AC 385 ms
84,864 KB
testcase_17 AC 802 ms
95,104 KB
testcase_18 AC 318 ms
89,548 KB
testcase_19 AC 904 ms
99,588 KB
testcase_20 AC 924 ms
99,328 KB
testcase_21 AC 760 ms
95,816 KB
testcase_22 AC 565 ms
91,952 KB
testcase_23 AC 206 ms
80,100 KB
testcase_24 AC 84 ms
75,408 KB
testcase_25 AC 298 ms
83,904 KB
testcase_26 AC 177 ms
79,540 KB
testcase_27 AC 294 ms
86,000 KB
testcase_28 AC 490 ms
87,352 KB
testcase_29 AC 76 ms
71,360 KB
testcase_30 AC 207 ms
80,672 KB
testcase_31 AC 606 ms
93,032 KB
testcase_32 AC 154 ms
79,236 KB
testcase_33 AC 379 ms
90,660 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