結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-17 11:41:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 738 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 510 ms
コンパイル使用メモリ 82,432 KB
実行使用メモリ 124,308 KB
最終ジャッジ日時 2024-05-01 17:26:18
合計ジャッジ時間 12,331 ms
ジャッジサーバーID
(参考情報)
judge2 / judge1
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 48 ms
52,608 KB
testcase_01 AC 75 ms
68,608 KB
testcase_02 AC 46 ms
53,376 KB
testcase_03 AC 45 ms
52,480 KB
testcase_04 AC 45 ms
52,480 KB
testcase_05 AC 46 ms
52,736 KB
testcase_06 AC 57 ms
61,824 KB
testcase_07 AC 45 ms
53,248 KB
testcase_08 AC 46 ms
52,992 KB
testcase_09 AC 144 ms
77,312 KB
testcase_10 AC 107 ms
76,928 KB
testcase_11 AC 123 ms
77,696 KB
testcase_12 AC 190 ms
78,592 KB
testcase_13 AC 176 ms
77,952 KB
testcase_14 AC 270 ms
80,256 KB
testcase_15 AC 136 ms
77,312 KB
testcase_16 AC 173 ms
77,588 KB
testcase_17 AC 207 ms
78,404 KB
testcase_18 AC 166 ms
77,548 KB
testcase_19 AC 120 ms
77,312 KB
testcase_20 AC 253 ms
82,536 KB
testcase_21 AC 217 ms
79,016 KB
testcase_22 AC 364 ms
93,160 KB
testcase_23 AC 282 ms
83,928 KB
testcase_24 AC 420 ms
97,280 KB
testcase_25 AC 418 ms
97,408 KB
testcase_26 AC 553 ms
106,448 KB
testcase_27 AC 595 ms
112,584 KB
testcase_28 AC 480 ms
102,956 KB
testcase_29 AC 738 ms
124,308 KB
testcase_30 AC 599 ms
121,908 KB
testcase_31 AC 468 ms
103,084 KB
testcase_32 AC 254 ms
79,652 KB
testcase_33 AC 114 ms
76,416 KB
testcase_34 AC 112 ms
76,288 KB
testcase_35 AC 140 ms
78,720 KB
testcase_36 AC 173 ms
77,440 KB
testcase_37 AC 135 ms
78,208 KB
testcase_38 AC 293 ms
81,656 KB
testcase_39 AC 196 ms
78,024 KB
testcase_40 AC 220 ms
78,720 KB
testcase_41 AC 221 ms
84,608 KB
testcase_42 AC 247 ms
81,912 KB
testcase_43 AC 206 ms
78,336 KB
testcase_44 AC 245 ms
80,780 KB
testcase_45 AC 180 ms
77,952 KB
testcase_46 AC 307 ms
89,216 KB
権限があれば一括ダウンロードができます

ソースコード

diff #

import itertools
import heapq

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

route = [[] for _ in [0]*N]
edge = [0]*M
for i in range(M):
	A,B,C = map(int,input().split())
	A -= 1;	B -= 1
	edge[i] = [A,B,C]
	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 = float("inf")
rsum = sum(edge[i][2] for i in R)
dist = [[0]*N for _ in [0]*N]
for rlist in itertools.permutations(R):
	for bit in range(1 << K):
		s = rsum
		path = [0]
		for i in range(K):
			if(bit & (1 << i)):
				path.append(edge[rlist[i]][0])
				path.append(edge[rlist[i]][1])
			else:
				path.append(edge[rlist[i]][1])
				path.append(edge[rlist[i]][0])
		path.append(N-1)
		for i in range(0,len(path)-1,2):
			if(dist[path[i]][path[i+1]] == 0):
				dist[path[i]][path[i+1]] = dist[path[i+1]][path[i]] = dijkstra(route,path[i],path[i+1],N)
			s += dist[path[i]][path[i+1]]
		if(ans > s):
			ans = s

print(ans)
0