結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTSMasKoaTS
提出日時 2021-10-05 17:11:12
言語 Python3
(3.12.2 + numpy 1.26.4 + scipy 1.12.0)
結果
AC  
実行時間 879 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 148 ms
コンパイル使用メモリ 12,928 KB
実行使用メモリ 21,668 KB
最終ジャッジ日時 2024-05-01 17:24:42
合計ジャッジ時間 9,498 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 32 ms
11,136 KB
testcase_01 AC 53 ms
11,008 KB
testcase_02 AC 29 ms
11,008 KB
testcase_03 AC 29 ms
11,008 KB
testcase_04 AC 28 ms
11,136 KB
testcase_05 AC 29 ms
11,008 KB
testcase_06 AC 31 ms
11,008 KB
testcase_07 AC 28 ms
11,008 KB
testcase_08 AC 26 ms
11,008 KB
testcase_09 AC 54 ms
11,264 KB
testcase_10 AC 32 ms
11,136 KB
testcase_11 AC 39 ms
11,648 KB
testcase_12 AC 76 ms
12,160 KB
testcase_13 AC 62 ms
11,904 KB
testcase_14 AC 142 ms
12,672 KB
testcase_15 AC 32 ms
11,136 KB
testcase_16 AC 54 ms
11,264 KB
testcase_17 AC 52 ms
11,264 KB
testcase_18 AC 56 ms
11,264 KB
testcase_19 AC 53 ms
11,264 KB
testcase_20 AC 154 ms
14,848 KB
testcase_21 AC 61 ms
11,904 KB
testcase_22 AC 299 ms
18,432 KB
testcase_23 AC 188 ms
15,104 KB
testcase_24 AC 369 ms
17,524 KB
testcase_25 AC 395 ms
18,168 KB
testcase_26 AC 605 ms
18,868 KB
testcase_27 AC 674 ms
19,712 KB
testcase_28 AC 582 ms
19,660 KB
testcase_29 AC 785 ms
19,716 KB
testcase_30 AC 879 ms
21,512 KB
testcase_31 AC 627 ms
21,668 KB
testcase_32 AC 121 ms
12,032 KB
testcase_33 AC 38 ms
11,264 KB
testcase_34 AC 36 ms
11,136 KB
testcase_35 AC 57 ms
12,672 KB
testcase_36 AC 72 ms
11,392 KB
testcase_37 AC 50 ms
12,288 KB
testcase_38 AC 205 ms
13,184 KB
testcase_39 AC 81 ms
11,520 KB
testcase_40 AC 90 ms
12,160 KB
testcase_41 AC 172 ms
18,788 KB
testcase_42 AC 136 ms
14,336 KB
testcase_43 AC 93 ms
11,904 KB
testcase_44 AC 173 ms
13,696 KB
testcase_45 AC 52 ms
11,520 KB
testcase_46 AC 234 ms
17,664 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