結果

問題 No.1812 Uribo Road
ユーザー MasKoaTSMasKoaTS
提出日時 2022-01-12 22:06:17
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 1,125 ms / 5,000 ms
コード長 3,295 bytes
コンパイル時間 167 ms
コンパイル使用メモリ 82,788 KB
実行使用メモリ 119,516 KB
最終ジャッジ日時 2024-04-27 12:31:51
合計ジャッジ時間 14,017 ms
ジャッジサーバーID
(参考情報)
judge1 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 37 ms
53,492 KB
testcase_01 AC 37 ms
55,100 KB
testcase_02 AC 46 ms
63,708 KB
testcase_03 AC 116 ms
80,860 KB
testcase_04 AC 37 ms
54,552 KB
testcase_05 AC 36 ms
54,048 KB
testcase_06 AC 39 ms
55,148 KB
testcase_07 AC 72 ms
76,676 KB
testcase_08 AC 122 ms
76,952 KB
testcase_09 AC 151 ms
77,496 KB
testcase_10 AC 113 ms
76,796 KB
testcase_11 AC 125 ms
77,096 KB
testcase_12 AC 488 ms
88,620 KB
testcase_13 AC 300 ms
87,820 KB
testcase_14 AC 291 ms
87,812 KB
testcase_15 AC 679 ms
100,388 KB
testcase_16 AC 385 ms
83,684 KB
testcase_17 AC 923 ms
104,780 KB
testcase_18 AC 884 ms
119,516 KB
testcase_19 AC 1,069 ms
110,888 KB
testcase_20 AC 1,125 ms
111,880 KB
testcase_21 AC 991 ms
112,172 KB
testcase_22 AC 943 ms
113,408 KB
testcase_23 AC 146 ms
77,976 KB
testcase_24 AC 64 ms
70,028 KB
testcase_25 AC 275 ms
84,052 KB
testcase_26 AC 143 ms
77,764 KB
testcase_27 AC 688 ms
106,088 KB
testcase_28 AC 371 ms
86,636 KB
testcase_29 AC 39 ms
54,220 KB
testcase_30 AC 208 ms
78,596 KB
testcase_31 AC 670 ms
98,160 KB
testcase_32 AC 133 ms
76,840 KB
testcase_33 AC 334 ms
90,204 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

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

"""
Union-Find
from : https://github.com/customaddone/beginPython/blob/master/cgi-bin/library/unionfind/unionfind.py
ref : https://algo-logic.info/union-find-tree/
"""

class UnionFind():
	#Uni = UnionFind(n) のようにする
	def __init__(self, n):
		self.n = n
		self.parents = [-1] * n

	#xの親(親がいないときは自身の番号を返す)
	def find(self, x):
		if self.parents[x] < 0:
			return x
		else:
			self.parents[x] = self.find(self.parents[x])
			return self.parents[x]

	#xとyを関係付ける
	def union(self, x, y):
		x = self.find(x)
		y = self.find(y)

		if x == y:
			return

		if self.parents[x] > self.parents[y]:
			x, y = y, x
		# if x > y: # よりrootのインデックスが小さい方が親
			# x, y = y, x

		self.parents[x] += self.parents[y]
		self.parents[y] = x

	#xとyが同じ組に属するかどうか
	def same(self, x, y):
		return self.find(x) == self.find(y)

	#xが属する組の大きさ
	def size(self, x):
		return -self.parents[self.find(x)]

	#xが属する組の全要素をリストとして取得
	def members(self, x):
		root = self.find(x)
		return [i for i in range(self.n) if self.find(i) == root]

	#Union-Find木の根に当たる要素全てをリストとして取得
	def roots(self):
		return [i for i, x in enumerate(self.parents) if x < 0]

	#Union-Find木の根とそれに属する組の全要素
	def all_group_members(self):
		return {r: self.members(r) for r in self.roots()}

N,M,K = map(int, input().split())

if not(2 <= N <= 10 ** 4 and N - 1 <= M <= min(2 * 10 ** 4, N * (N - 1) // 2) and 1 <= K <= min(12, M)):
	exit(1)

R = [i - 1 for i in map(int, input().split())]
S = set(R)

if any(not(1 <= i + 1 <= M) for i in S):
	exit(1)
if not(len(S) == len(R) == K):
	exit(1)

edge = [[] for _ in [0] * M]
route = [[] for _ in [0] * N]
abst = set([])
uni = UnionFind(N)
for i in range(M):
	a,b,c = map(int, input().split())

	if((a,b) in abst):
		exit(1)
	abst.add((a,b));	abst.add((b,a))
	if not(1 <= a <= N and 1 <= b <= N and a != b and 1 <= c <= 10 ** 4):
		exit(1)

	a -= 1;	b -= 1

	uni.union(a,b)

	route[a].append((b,c))
	route[b].append((a,c))
	if(i in S):
		edge[i] = [a,b,c]

if not(uni.size(0) == N):
	exit(1)

def dijkstra(s,route):
	que = [(0,s)]
	dist = [INF] * N
	while(que):
		d,v = hq.heappop(que)
		if(dist[v] < INF):
			continue
		dist[v] = d
		for nv,nd in route[v]:
			hq.heappush(que,(d + nd, nv))
	return dist

dist = [[] for _ in [0] * N]
T = set([0,N - 1])
for i in S:
	T.add(edge[i][0])
	T.add(edge[i][1])
for i in T:
	dist[i] = dijkstra(i,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] = dist[0][edge[R[i]][j ^ 1]] + edge[R[i]][2]
for bit in range(1, 1 << K):
	for s in range(K):
		if not(bit & (1 << s)):
			continue
		for t in range(K):
			if(bit & (1 << t)):
				continue
			b = bit | (1 << t);	c = edge[R[t]][2]
			for x in range(2):
				for y in range(2):
					p = edge[R[s]][x];	q = edge[R[t]][y ^ 1]
					d = dp[bit][s][x] + dist[p][q] + c
					if(dp[b][t][y] > d):
						dp[b][t][y] = d

ans = INF
for i in range(K):
	for j in range(2):
		d = dp[-1][i][j] + dist[edge[R[i]][j]][N - 1]
		if(ans > d):
			ans = d
print(ans)
0