結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-31 12:24:43
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 4,551 ms / 5,000 ms
コード長 1,641 bytes
コンパイル時間 364 ms
コンパイル使用メモリ 81,920 KB
実行使用メモリ 240,820 KB
最終ジャッジ日時 2024-10-08 05:47:27
合計ジャッジ時間 18,326 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 42 ms
52,992 KB
testcase_01 AC 41 ms
52,736 KB
testcase_02 AC 59 ms
64,512 KB
testcase_03 AC 210 ms
81,152 KB
testcase_04 AC 42 ms
52,480 KB
testcase_05 AC 42 ms
53,376 KB
testcase_06 AC 45 ms
53,888 KB
testcase_07 AC 91 ms
76,416 KB
testcase_08 AC 245 ms
78,124 KB
testcase_09 AC 315 ms
79,268 KB
testcase_10 AC 235 ms
78,232 KB
testcase_11 AC 219 ms
78,136 KB
testcase_12 AC 1,731 ms
103,904 KB
testcase_13 AC 668 ms
91,424 KB
testcase_14 AC 666 ms
90,544 KB
testcase_15 AC 1,132 ms
133,064 KB
testcase_16 AC 1,207 ms
92,464 KB
testcase_17 AC 3,110 ms
206,280 KB
testcase_18 AC 1,207 ms
179,460 KB
testcase_19 AC 3,182 ms
214,016 KB
testcase_20 AC 3,873 ms
232,448 KB
testcase_21 AC 3,276 ms
231,712 KB
testcase_22 AC 4,551 ms
240,820 KB
testcase_23 AC 297 ms
79,804 KB
testcase_24 AC 67 ms
69,120 KB
testcase_25 AC 195 ms
79,948 KB
testcase_26 AC 191 ms
78,592 KB
testcase_27 AC 1,129 ms
174,880 KB
testcase_28 AC 509 ms
87,360 KB
testcase_29 AC 41 ms
52,736 KB
testcase_30 AC 299 ms
81,152 KB
testcase_31 AC 2,121 ms
162,808 KB
testcase_32 AC 174 ms
78,180 KB
testcase_33 AC 442 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