結果

問題 No.1814 Uribo Road (Easy)
ユーザー MasKoaTS
提出日時 2021-10-17 11:41:30
言語 PyPy3
(7.3.15)
結果
AC  
実行時間 605 ms / 2,000 ms
コード長 1,165 bytes
コンパイル時間 190 ms
コンパイル使用メモリ 82,408 KB
実行使用メモリ 124,080 KB
最終ジャッジ日時 2024-11-21 22:50:56
合計ジャッジ時間 10,881 ms
ジャッジサーバーID
(参考情報)
judge5 / judge1
このコードへのチャレンジ
(要ログイン)
ファイルパターン 結果
sample AC * 3
other AC * 44
権限があれば一括ダウンロードができます

ソースコード

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