from typing import List, Tuple
from queue import PriorityQueue
from itertools import permutations
'''
先求6个点相互之间的距离,然后dfs枚举路线的所有可能排列
'''
n, m = map(int, input().split())
arr = list(map(int, input().split()))
from collections import deque
class SPFA:
# start_node 为起始点,edges是边的三元组(节点1, 节点2, 边权重) is_directed表示是否是有向图
# 包含节点为1, 2, 3, 4, ..... max_node_num
def __init__(self, start_node, edges, is_directed, max_node_num):
self.edges = edges[::]
self.start_node = start_node
self.is_directed = is_directed
self.max_node_num = max_node_num
# 获取最短路径长度的列表[(节点1,长度1), (节点2, 长度2) .....]
def getMinPathLen(self):
return self.getMinInfo()
def getMinInfo(self):
link = {}
dis = [0x7fffffff] * (self.max_node_num+1)
dis[self.start_node] = 0
for a, b, w in self.edges:
if not self.is_directed:
# 无向图检查是否有负边
if w < 0:
return None
if a not in link:
link[a] = []
if b not in link:
link[b] = []
link[a].append((b, w))
link[b].append((a, w))
else:
if a not in link:
link[a] = []
link[a].append((b, w))
updated_nodes = deque()
updated_nodes.append(self.start_node)
in_que = [0] * (self.max_node_num + 1)
in_que[self.start_node] = 1
# 进行V次迭代,第V次检查是否是无解情况
iter_times = 0
while iter_times < self.max_node_num:
update_flag = False
# 利用上一轮迭代距离变小的点进行松弛操作
node_num = len(updated_nodes)
for _ in range(node_num):
a = updated_nodes.popleft()
in_que[a] = 0
if a not in link:
continue
for b, w in link[a]:
new_dis = 0x7fffffff if dis[a] == 0x7fffffff else dis[a] + w
if new_dis < dis[b]:
dis[b] = new_dis
update_flag = True
if in_que[b] == 0:
in_que[b] = 1
updated_nodes.append(b)
iter_times += 1
if iter_times == self.max_node_num:
if update_flag:
return None
if not update_flag:
break
#return {node: dis[node] for node in range(1, self.max_node_num+1) if node == 1 or node in arr }
return dis
edges = []
for i in range(m):
a, b, w = map(int, input().split())
edges.append((a,b,w))
edges.append((b,a,w))
dist = {}
for start_node in [1, arr[0], arr[1], arr[2], arr[3], arr[4]]:
dist[(start_node, start_node)] = 0
algo = SPFA(start_node, edges, is_directed=False, max_node_num=n)
ans = algo.getMinPathLen()
for next in [1, arr[0], arr[1], arr[2], arr[3], arr[4]]:
if next != start_node:
dist[(start_node, next)] = ans[next]
min_total = 0x7fffffff
visit = {1}
def dfs(cur, sum):
if len(visit) == 6:
return sum
ans = 0x7fffffff
for next in arr:
if next not in visit:
visit.add(next)
ans = min(ans, dfs(next, sum + dist[(cur, next)]))
visit.remove(next)
return ans
print(dfs(1, 0))