結果
問題 | No.1812 Uribo Road |
ユーザー | 👑 SPD_9X2 |
提出日時 | 2022-01-14 23:02:40 |
言語 | PyPy3 (7.3.15) |
結果 |
AC
|
実行時間 | 804 ms / 5,000 ms |
コード長 | 2,453 bytes |
コンパイル時間 | 183 ms |
コンパイル使用メモリ | 82,332 KB |
実行使用メモリ | 98,936 KB |
最終ジャッジ日時 | 2024-11-20 13:40:37 |
合計ジャッジ時間 | 10,335 ms |
ジャッジサーバーID (参考情報) |
judge5 / judge3 |
(要ログイン)
テストケース
テストケース表示入力 | 結果 | 実行時間 実行使用メモリ |
---|---|---|
testcase_00 | AC | 39 ms
54,844 KB |
testcase_01 | AC | 40 ms
53,584 KB |
testcase_02 | AC | 49 ms
64,408 KB |
testcase_03 | AC | 130 ms
81,724 KB |
testcase_04 | AC | 38 ms
52,824 KB |
testcase_05 | AC | 37 ms
53,472 KB |
testcase_06 | AC | 40 ms
54,636 KB |
testcase_07 | AC | 82 ms
77,008 KB |
testcase_08 | AC | 153 ms
78,228 KB |
testcase_09 | AC | 201 ms
79,120 KB |
testcase_10 | AC | 124 ms
77,808 KB |
testcase_11 | AC | 148 ms
77,532 KB |
testcase_12 | AC | 426 ms
87,764 KB |
testcase_13 | AC | 202 ms
86,868 KB |
testcase_14 | AC | 221 ms
86,744 KB |
testcase_15 | AC | 404 ms
84,296 KB |
testcase_16 | AC | 346 ms
82,956 KB |
testcase_17 | AC | 732 ms
92,460 KB |
testcase_18 | AC | 281 ms
86,984 KB |
testcase_19 | AC | 771 ms
98,132 KB |
testcase_20 | AC | 804 ms
98,936 KB |
testcase_21 | AC | 712 ms
95,076 KB |
testcase_22 | AC | 530 ms
92,744 KB |
testcase_23 | AC | 157 ms
79,252 KB |
testcase_24 | AC | 45 ms
61,320 KB |
testcase_25 | AC | 240 ms
81,824 KB |
testcase_26 | AC | 135 ms
77,556 KB |
testcase_27 | AC | 255 ms
84,260 KB |
testcase_28 | AC | 444 ms
85,488 KB |
testcase_29 | AC | 40 ms
54,088 KB |
testcase_30 | AC | 169 ms
78,396 KB |
testcase_31 | AC | 572 ms
91,160 KB |
testcase_32 | AC | 121 ms
78,000 KB |
testcase_33 | AC | 311 ms
88,348 KB |
ソースコード
""" 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)