結果
問題 | No.1812 Uribo Road |
ユーザー |
👑 ![]() |
提出日時 | 2022-01-14 22:57:04 |
言語 | PyPy3 (7.3.15) |
結果 |
RE
|
実行時間 | - |
コード長 | 2,423 bytes |
コンパイル時間 | 194 ms |
コンパイル使用メモリ | 82,436 KB |
実行使用メモリ | 99,336 KB |
最終ジャッジ日時 | 2024-11-20 13:33:59 |
合計ジャッジ時間 | 9,511 ms |
ジャッジサーバーID (参考情報) |
judge3 / judge4 |
(要ログイン)
ファイルパターン | 結果 |
---|---|
sample | AC * 4 |
other | AC * 24 RE * 6 |
ソースコード
"""bitdpっぽくやるのね…10^7...厳しめdp[bit][最後][LorR] = mincost"""from sys import stdinimport heapqdef Dijkstra(lis,start):ret = [float("inf")] * len(lis)ret[start] = 0q = [(0,start)]while len(q) > 0:ncost,now = heapq.heappop(q)if ncost != ret[now]:continuefor nex,ecost in lis[now]:if ret[nex] > ncost + ecost:ret[nex] = ncost + ecostheapq.heappush(q , (ret[nex] , nex))return retN,M,K = map(int,stdin.readline().split())R = list(map(int,stdin.readline().split()))for i in range(K):R[i] -= 1lis = [ [] for i in range(M) ]ABC = []for i in range(M):A,B,C = map(int,stdin.readline().split())A -= 1B -= 1ABC.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] = mincostfor i,nr in enumerate(R):l,r,c = ABC[nr]dp[2**i][i][0] = ddic[0][r] + cdp[2**i][i][1] = ddic[0][l] + cdef popcnt(x):ret = 0while x:ret += x%2x //= 2return retfor 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:continuefor newNR in range(K):newbit = 2**newNR | bitif bit == newbit:continuenewL,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 = infbit = 2**K-1for 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)