結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-30 22:31:28
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,733 ms / 5,000 ms
コード長 1,467 bytes
コンパイル時間 185 ms
コンパイル使用メモリ 81,792 KB
実行使用メモリ 174,696 KB
最終ジャッジ日時 2024-04-16 08:57:09
合計ジャッジ時間 18,195 ms
ジャッジサーバーID
(参考情報)
judge2 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 47 ms
52,608 KB
testcase_01 AC 47 ms
52,864 KB
testcase_02 AC 64 ms
63,872 KB
testcase_03 AC 194 ms
80,896 KB
testcase_04 AC 45 ms
52,992 KB
testcase_05 AC 47 ms
53,376 KB
testcase_06 AC 48 ms
53,888 KB
testcase_07 AC 90 ms
73,472 KB
testcase_08 AC 163 ms
77,488 KB
testcase_09 AC 218 ms
79,016 KB
testcase_10 AC 161 ms
77,376 KB
testcase_11 AC 188 ms
78,028 KB
testcase_12 AC 903 ms
148,136 KB
testcase_13 AC 559 ms
133,328 KB
testcase_14 AC 533 ms
133,088 KB
testcase_15 AC 679 ms
109,920 KB
testcase_16 AC 603 ms
121,780 KB
testcase_17 AC 1,151 ms
150,016 KB
testcase_18 AC 578 ms
114,944 KB
testcase_19 AC 1,733 ms
171,556 KB
testcase_20 AC 1,688 ms
173,092 KB
testcase_21 AC 1,540 ms
172,124 KB
testcase_22 AC 1,574 ms
174,696 KB
testcase_23 AC 187 ms
78,780 KB
testcase_24 AC 64 ms
63,616 KB
testcase_25 AC 246 ms
82,552 KB
testcase_26 AC 149 ms
77,184 KB
testcase_27 AC 519 ms
105,600 KB
testcase_28 AC 333 ms
88,044 KB
testcase_29 AC 48 ms
53,248 KB
testcase_30 AC 206 ms
79,360 KB
testcase_31 AC 919 ms
121,348 KB
testcase_32 AC 149 ms
77,272 KB
testcase_33 AC 528 ms
103,288 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)
	if not(1 <= A+1 <= N and 1 <= B+1 <= N and 1 <= C <= 10**4):
		print((i,A+1,B+1,C))
		exit(1)
	route[A].append((B,C))
	route[B].append((A,C))

def dijkstra(route):
	dist = {}
	for i in st:
		q = [(0,i)]
		st2 = set([])
		while(q and len(st2) < len(st)):
			d,v = heapq.heappop(q)
			if((i,v) not in dist):
				if(v in st):
					st2.add(v)
				dist[(i,v)] = d
				for nv,nd in route[v]:
					heapq.heappush(q,(d + nd,nv))
	return dist

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

dp = [[[INF] * 2 for _ in [0] * K] for _ in [0] * (1 << K)]
for i in range(K):
	for j in range(2):
		dp[1 << i][i][j] = rsum + dist[(0,edge[lR[i]][j])]
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):
						d = dp[bit][s][i ^ 1] + dist[(edge[lR[s]][i],edge[lR[t]][j])]
						if(dp[b][t][j] > d):
							dp[b][t][j] = d
ans = INF
for i in range(K):
	for j in range(2):
		d = dp[-1][i][j ^ 1] + dist[(edge[lR[i]][j],N - 1)]
		if(ans > d):
			ans = d
print(ans)
0