結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2021-12-30 22:36:58
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 3,396 ms / 5,000 ms
コード長 1,467 bytes
コンパイル時間 100 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 56,912 KB
最終ジャッジ日時 2024-04-16 08:58:39
合計ジャッジ時間 28,201 ms
ジャッジサーバーID
(参考情報)
judge2 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 33 ms
10,880 KB
testcase_01 AC 32 ms
11,008 KB
testcase_02 AC 34 ms
11,008 KB
testcase_03 AC 639 ms
17,024 KB
testcase_04 AC 32 ms
10,880 KB
testcase_05 AC 32 ms
10,880 KB
testcase_06 AC 33 ms
11,008 KB
testcase_07 AC 76 ms
11,520 KB
testcase_08 AC 69 ms
11,904 KB
testcase_09 AC 83 ms
12,288 KB
testcase_10 AC 66 ms
12,028 KB
testcase_11 AC 57 ms
11,776 KB
testcase_12 AC 1,622 ms
54,636 KB
testcase_13 AC 1,215 ms
53,136 KB
testcase_14 AC 1,172 ms
51,476 KB
testcase_15 AC 878 ms
23,992 KB
testcase_16 AC 756 ms
35,724 KB
testcase_17 AC 1,874 ms
44,768 KB
testcase_18 AC 1,263 ms
21,156 KB
testcase_19 AC 3,396 ms
51,880 KB
testcase_20 AC 3,122 ms
51,428 KB
testcase_21 AC 2,863 ms
56,052 KB
testcase_22 AC 3,192 ms
56,912 KB
testcase_23 AC 162 ms
13,180 KB
testcase_24 AC 34 ms
10,880 KB
testcase_25 AC 199 ms
17,428 KB
testcase_26 AC 51 ms
11,392 KB
testcase_27 AC 788 ms
17,740 KB
testcase_28 AC 335 ms
20,840 KB
testcase_29 AC 33 ms
10,880 KB
testcase_30 AC 106 ms
12,288 KB
testcase_31 AC 1,629 ms
31,572 KB
testcase_32 AC 81 ms
11,648 KB
testcase_33 AC 1,091 ms
27,220 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