結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-31 12:24:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 3,761 ms / 5,000 ms
コード長 1,641 bytes
コンパイル時間 153 ms
コンパイル使用メモリ 82,468 KB
実行使用メモリ 240,568 KB
最終ジャッジ日時 2024-04-16 23:15:16
合計ジャッジ時間 32,223 ms
ジャッジサーバーID
(参考情報)
judge2 / judge5
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 38 ms
53,248 KB
testcase_01 AC 38 ms
52,736 KB
testcase_02 AC 53 ms
65,152 KB
testcase_03 AC 197 ms
81,400 KB
testcase_04 AC 38 ms
53,120 KB
testcase_05 AC 39 ms
53,248 KB
testcase_06 AC 40 ms
54,528 KB
testcase_07 AC 81 ms
76,544 KB
testcase_08 AC 239 ms
78,636 KB
testcase_09 AC 299 ms
79,488 KB
testcase_10 AC 231 ms
78,600 KB
testcase_11 AC 209 ms
78,140 KB
testcase_12 AC 1,806 ms
104,156 KB
testcase_13 AC 693 ms
91,680 KB
testcase_14 AC 687 ms
90,536 KB
testcase_15 AC 1,031 ms
133,580 KB
testcase_16 AC 1,213 ms
92,736 KB
testcase_17 AC 3,329 ms
206,780 KB
testcase_18 AC 1,292 ms
179,088 KB
testcase_19 AC 3,546 ms
214,368 KB
testcase_20 AC 3,761 ms
232,960 KB
testcase_21 AC 3,199 ms
231,460 KB
testcase_22 AC 3,485 ms
240,568 KB
testcase_23 AC 278 ms
79,684 KB
testcase_24 AC 60 ms
69,888 KB
testcase_25 AC 198 ms
80,464 KB
testcase_26 AC 196 ms
78,188 KB
testcase_27 AC 1,151 ms
175,392 KB
testcase_28 AC 516 ms
87,080 KB
testcase_29 AC 40 ms
53,376 KB
testcase_30 AC 296 ms
81,016 KB
testcase_31 AC 2,136 ms
162,684 KB
testcase_32 AC 174 ms
78,308 KB
testcase_33 AC 448 ms
88,448 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

#ダイクストラ高速化弱め

import heapq
import sys
input = sys.stdin.readline
INF = 10 ** 18

N,M,K = map(int,input().split())
R = set([i-1 for i in map(int,input().split())])
lR = list(R)

route = [[] for _ in [0]*N]
edge = [0]*M
st = set([0,N-1])
for i in range(M):
	A,B,C = map(int,input().split())
	A -= 1;	B -= 1
	if(i in R):
		edge[i] = [A,B,C]
		st.add(A);	st.add(B)
	route[A].append((B,C))
	route[B].append((A,C))

def dijkstra(route,start,finish,n):
	path = [-1]*n
	q = [(0,start)]
	while(q):
		d,v = heapq.heappop(q)
		if(path[v] == -1):
			path[v] = d
			if(v == finish):
				break
			for nv,dist in route[v]:
				heapq.heappush(q,(d+dist,nv))
	return path[finish]

ans = INF
rsum = sum(edge[i][2] for i in R)
dist = {}

dp = [[[INF] * 2 for _ in [0] * K] for _ in [0] * (1 << K)]
for i in range(K):
	for j in range(2):
		p = edge[lR[i]][j]
		if((0,p) not in dist):
			dist[(0,p)] = dist[(p,0)] = dijkstra(route,0,p,N)
		dp[1 << i][i][j] = rsum + dist[(0,p)]
for bit in range(1, 1 << K):
	for s in range(K):
		if not(bit & (1 << s)):
			continue
		for i in range(2):
			for t in range(K):
				if not(bit & (1 << t)):
					b = bit | (1 << t)
					for j in range(2):
						p = edge[lR[s]][i];	q = edge[lR[t]][j]
						if((p,q) not in dist):
							dist[(p,q)] = dist[(q,p)] = dijkstra(route,p,q,N)
						d = dp[bit][s][i ^ 1] + dist[(p,q)]
						if(dp[b][t][j] > d):
							dp[b][t][j] = d
ans = INF
for i in range(K):
	for j in range(2):
		p = edge[lR[i]][j];	q = N - 1
		if((p,q) not in dist):
			dist[(p,q)] = dist[(q,p)] = dijkstra(route,p,q,N)
		d = dp[-1][i][j ^ 1] + dist[(p,q)]
		if(ans > d):
			ans = d
print(ans)
0