There are N towns in the State of Atcoder, connected by M bidirectional roads.
The i-th road connects Town A_i and B_i and has a length of C_i.
Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).
She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.
If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?

-----Constraints-----
 - 2≤N≤200
 - 1≤M≤N×(N-1)/2
 - 2≤R≤min(8,N) (min(8,N) is the smaller of 8 and N.)
 - r_i≠r_j (i≠j)
 - 1≤A_i,B_i≤N, A_i≠B_i
 - (A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)
 - 1≤C_i≤100000
 - Every town can be reached from every town by road.
 - All input values are integers.

-----Input-----
Input is given from Standard Input in the following format:
N M R
r_1 ... r_R
A_1 B_1 C_1
:
A_M B_M C_M

-----Output-----
Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.

-----Sample Input-----
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4

-----Sample Output-----
2

For example, if she visits the towns in the order of 1, 2, 3, the distance traveled will be 2, which is the minimum possible.

Difficulty: MEDIUM

Solutions:
(n, m, R) = list(map(int, input().split()))
r = list(map(int, input().split()))
dp = [[float('INF')] * n for i in range(n)]
for i in range(m):
	(a, b, c) = list(map(int, input().split()))
	dp[a - 1][b - 1] = c
	dp[b - 1][a - 1] = c
for i in range(n):
	dp[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
from itertools import permutations
per = list(permutations(r, R))
ans = float('INF')
for tup in per:
	ans_kouho = 0
	for i in range(R - 1):
		ans_kouho = ans_kouho + dp[tup[i] - 1][tup[i + 1] - 1]
	ans = min(ans, ans_kouho)
print(ans)


from collections import deque
import itertools
INF = 10 ** 12 + 7
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[INF] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	d[A][B] = C
	d[B][A] = C
for i in range(1, N + 1):
	d[i][i] = 0
for k in range(N + 1):
	for i in range(N + 1):
		for j in range(N + 1):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for i in itertools.permutations(r, len(r)):
	val = 0
	for j in range(1, len(r)):
		val += d[i[j - 1]][i[j]]
	ans = min(ans, val)
print(ans)


import itertools
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[10 ** 10 for j in range(n)] for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	d[a][b] = c
	d[b][a] = c
for i in range(n):
	d[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][k] + d[k][j], d[i][j])
ans = 10 ** 10
for i in itertools.permutations(r):
	x = i
	k = 0
	for j in range(R - 1):
		(a, b) = (x[j + 1], x[j])
		a -= 1
		b -= 1
		k += d[b][a]
	ans = min(ans, k)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = [int(s) - 1 for s in input().split()]
(a, b, c) = ([0] * M, [0] * M, [0] * M)
for i in range(M):
	(a[i], b[i], c[i]) = map(int, input().split())
	a[i] -= 1
	b[i] -= 1
wf = [[float('inf')] * N for _ in range(N)]
for i in range(M):
	wf[a[i]][b[i]] = c[i]
	wf[b[i]][a[i]] = c[i]
for (k, i, j) in itertools.product(range(N), repeat=3):
	wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])
x = float('inf')
for route in itertools.permutations(r):
	y = sum((wf[route[i]][route[i + 1]] for i in range(R - 1)))
	x = min(x, y)
print(x)


import itertools
(N, M, R) = map(int, input().split())
r = map(int, input().split())
A = [[1 << 64] * N for _ in range(N)]
for i in range(N):
	A[i][i] = 0
for _ in range(M):
	(a, b, c) = map(int, input().split())
	A[a - 1][b - 1] = A[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			A[i][j] = min(A[i][j], A[i][k] + A[k][j])
res = 1 << 64
for perm in itertools.permutations([x - 1 for x in r]):
	cost = 0
	for i in range(R - 1):
		cost += A[perm[i]][perm[i + 1]]
	if cost < res:
		res = cost
print(res)


(n, m, r) = map(int, input().split())
road = list(map(int, input().split()))
cost = [[float('inf')] * n for i in range(n)]
for i in range(n):
	cost[i][i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	cost[a][b] = c
	cost[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
import itertools
ans = float('inf')
for i in itertools.permutations(road):
	tmp = 0
	for j in range(len(i) - 1):
		tmp += cost[i[j] - 1][i[j + 1] - 1]
	if tmp < ans:
		ans = tmp
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
G = [[float('inf')] * N for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	(a, b) = (a - 1, b - 1)
	G[a][b] = c
	G[b][a] = c
for k in range(N):
	G[k][k] = 0
	for i in range(N):
		for j in range(N):
			G[i][j] = min(G[i][j], G[i][k] + G[k][j])
ans = float('inf')
for perm in permutations(r):
	tmp = 0
	for i in range(R - 1):
		tmp += G[perm[i]][perm[i + 1]]
	ans = min(ans, tmp)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
INF = 2 ** 63 - 1
d = [[INF for i in range(N + 1)] for j in range(N + 1)]
r = list(map(int, input().split()))
r = list(itertools.permutations(r))
for i in range(M):
	(a, b, c) = map(int, input().split())
	d[a][b] = c
	d[b][a] = c
for i in range(N + 1):
	d[i][i] = 0
for k in range(N + 1):
	for i in range(N + 1):
		for j in range(N + 1):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for path in r:
	anstemp = 0
	for i in range(R - 1):
		anstemp += d[path[i]][path[i + 1]]
	ans = min(anstemp, ans)
print(ans)


from itertools import permutations
import sys
input = sys.stdin.readline
(N, M, R) = map(int, input().split())
dest = [x - 1 for x in map(int, input().split())]
inf = float('inf')
mat = [[inf] * N for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	mat[a - 1][b - 1] = mat[b - 1][a - 1] = c
for i in range(N):
	for j in range(N):
		for k in range(N):
			mat[j][k] = min(mat[j][k], mat[j][i] + mat[i][k])
ans = inf
for l in permutations(dest):
	tmp = 0
	for (u, v) in zip(l, l[1:]):
		tmp += mat[u][v]
	ans = min(ans, tmp)
print(ans)


from itertools import permutations
(n, m, rl) = map(int, input().split())
r = list(map(int, input().split()))
d = [[float('inf') for _ in range(n)] for _ in range(n)]
for i in range(m):
	(x, y, z) = map(int, input().split())
	(d[x - 1][y - 1], d[y - 1][x - 1]) = (z, z)
for i in range(n):
	d[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
a = float('inf')
for i in permutations(r, rl):
	aa = 0
	for j in range(rl - 1):
		aa += d[i[j] - 1][i[j + 1] - 1]
	a = min(a, aa)
print(a)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
m = [[10 ** 10] * N for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	m[a - 1][b - 1] = c
	m[b - 1][a - 1] = c
for i in range(N):
	m[i][i] = 0
for i in range(N):
	for j in range(N):
		for k in range(N):
			m[j][k] = min(m[j][k], m[j][i] + m[i][k])
ans = 10 ** 10
for l in itertools.permutations(r):
	cost = 0
	for i in range(1, R):
		cost += m[l[i - 1] - 1][l[i] - 1]
	ans = min(ans, cost)
print(ans)


import sys
from itertools import permutations as per
input = sys.stdin.readline
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[float('inf')] * (N + 1) for _ in range(N + 1)]
for i in range(N + 1):
	d[i][i] = 0
for _ in range(M):
	(a, b, c) = map(int, input().split())
	d[a][b] = c
	d[b][a] = c
for k in range(1, N + 1):
	for i in range(1, N + 1):
		for j in range(1, N + 1):
			d[i][j] = min(d[i][k] + d[k][j], d[i][j])
res = float('inf')
for p in per(r, R):
	t = 0
	for i in range(R - 1):
		t += d[p[i]][p[i + 1]]
	res = min(t, res)
print(res)


import itertools
(n, m, r) = map(int, input().split())
wa = [[float('INF')] * n for i in range(n)]
l = [i - 1 for i in map(int, input().split())]
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	wa[a][b] = c
	wa[b][a] = c
for i in range(n):
	wa[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			wa[i][j] = min(wa[i][j], wa[i][k] + wa[k][j])
ans = float('INF')
for p in list(itertools.permutations(l)):
	count = 0
	for i in range(r - 1):
		count += wa[p[i]][p[i + 1]]
	ans = min(ans, count)
print(ans)


import sys, copy
from itertools import permutations

def wf(d):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
(N, M, R) = map(int, input().split())
Rs = list(map(int, input().split()))
d = [[float('inf') for i in range(N)] for i in range(N)]
for _ in range(M):
	(a, b, c) = map(int, sys.stdin.readline().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(N):
	d[i][i] = 0
wf(d)
ans = float('inf')
for prm in permutations(Rs, R):
	ans = min(ans, sum([d[prm[i - 1] - 1][prm[i] - 1] for i in range(1, R)]))
print(ans)


from itertools import permutations
INF = 10 ** 18
(n, m, r) = map(int, input().split())
dist = [[INF for _ in range(n + 1)] for _ in range(n + 1)]
dp = [[-1 for _ in range(r + 1)] for _ in range((1 << r) + 1)]
r_t = list(map(int, input().split()))
for i in range(n + 1):
	dist[i][i] = 0
for _ in range(m):
	(a, b, c) = map(int, input().split())
	dist[a][b] = c
	dist[b][a] = c
for k in range(n + 1):
	for i in range(n + 1):
		for j in range(n + 1):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
r_list = list(permutations(r_t))
fans = INF
for i in r_list:
	ans = 0
	for j in range(r - 1):
		ans += dist[i[j + 1]][i[j]]
	fans = min(fans, ans)
print(fans)


from itertools import permutations
inf = 10 ** 9
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
rr = permutations(r, R)
dis = [[inf] * n for _ in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	dis[a - 1][b - 1] = c
	dis[b - 1][a - 1] = c
for i in range(n):
	for j in range(n):
		for k in range(n):
			dis[j][k] = min(dis[j][i] + dis[i][k], dis[j][k])
ans = 10 ** 12
for x in rr:
	e = 0
	for i in range(R - 1):
		e += dis[x[i + 1] - 1][x[i] - 1]
	ans = min(ans, e)
print(ans)


(n, m, R) = [int(x) for x in input().split()]
r = [int(x) - 1 for x in input().split()]
d = [[float('inf') for _ in [0] * n] for _ in [0] * n]
for i in range(n):
	d[i][i] = 0
for i in [0] * m:
	(a, b, c) = [int(x) for x in input().split()]
	d[a - 1][b - 1] = d[b - 1][a - 1] = c
from itertools import product, permutations
for (k, i, j) in product(range(n), range(n), range(n)):
	if d[i][j] > d[i][k] + d[k][j]:
		d[i][j] = d[i][k] + d[k][j]

def Long(args):
	ans = 0
	for i in range(len(args) - 1):
		ans += d[args[i + 1]][args[i]]
	return ans
print(min((Long(i) for i in permutations(r))))


import itertools
(n, m, r_) = map(int, input().split())
r = list(map(int, input().split()))
for i in range(r_):
	r[i] -= 1
d = [[10 ** 10 for _ in range(n)] for i in range(n)]
for i in range(m):
	(ai, bi, ci) = map(int, input().split())
	ai -= 1
	bi -= 1
	d[ai][bi] = d[bi][ai] = ci
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][k] + d[k][j], d[i][j])
ans = 10 ** 10
for l in itertools.permutations(r, r_):
	tmp = 0
	for i in range(r_ - 1):
		tmp += d[l[i]][l[i + 1]]
	ans = min(ans, tmp)
print(ans)


import itertools
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
INF = 10 ** 10
d = [[INF for i in range(n)] for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
for i in range(R):
	r[i] -= 1
res = INF
for rp in itertools.permutations(r):
	tres = 0
	for i in range(R - 1):
		tres += d[rp[i]][rp[i + 1]]
	res = min(res, tres)
print(res)


from itertools import permutations
(n, m, r) = map(int, input().split())
R = list(map(int, input().split()))
inf = 10 ** 9
edges = [[inf for i in range(n + 1)] for i in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	edges[a][b] = c
	edges[b][a] = c
for k in range(1, n + 1):
	for i in range(1, n):
		for j in range(i, n + 1):
			if edges[i][j] > edges[i][k] + edges[k][j]:
				edges[i][j] = edges[i][k] + edges[k][j]
				edges[j][i] = edges[i][j]
ans = inf
for i in permutations(R):
	tmp = 0
	for j in range(r - 1):
		tmp += edges[i[j]][i[j + 1]]
	ans = min(tmp, ans)
print(ans)


(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
dp = [[10 ** 9] * N for _ in range(N)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	A -= 1
	B -= 1
	dp[A][B] = C
	dp[B][A] = C
for z in range(N):
	for x in range(N):
		for y in range(N):
			dp[x][y] = min(dp[x][y], dp[x][z] + dp[z][y])
import itertools
ans = 10 ** 9
for p in itertools.permutations(range(R)):
	t = 0
	rp = [r[i] for i in p]
	for i in range(R - 1):
		t += dp[rp[i] - 1][rp[i + 1] - 1]
	ans = min(ans, t)
print(ans)


import itertools
(n, m, r) = map(int, input().split())
t = list(map(int, input().split()))
t.sort()
d = [[10 ** 18] * n for j in range(n)]
for i in range(n):
	d[i][i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	d[a][b] = c
	d[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = 10 ** 18
for p in itertools.permutations(t):
	now = 0
	for i in range(r - 1):
		now += d[p[i] - 1][p[i + 1] - 1]
	ans = min(ans, now)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
Ans = 10 ** 10
A = [[10 ** 10 for _ in range(N)] for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	A[a - 1][b - 1] = c
	A[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			A[i][j] = min(A[i][j], A[i][k] + A[k][j])
P = list(permutations(r))
for p in P:
	d = 0
	for i in range(R - 1):
		d += A[p[i] - 1][p[i + 1] - 1]
	Ans = min(Ans, d)
print(Ans)


from itertools import permutations
(n, m, R) = map(int, input().split())
r = [int(i) - 1 for i in input().split()]
INF = float('INF')
d = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	d[a][b] = c
	d[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for p in permutations(r):
	res = 0
	for i in range(R - 1):
		res += d[p[i]][p[i + 1]]
	ans = min(ans, res)
print(ans)


import sys
from itertools import permutations
read = sys.stdin.read
readline = sys.stdin.readline
readlines = sys.stdin.readlines
sys.setrecursionlimit(10 ** 9)
INF = 1 << 60
MOD = 1000000007

def main():
	(N, M, R) = list(map(int, readline().split()))
	city = [int(s) - 1 for s in readline().split()]
	ABC = list(map(int, read().split()))
	G = [[INF] * N for _ in range(N)]
	for (a, b, c) in zip(*[iter(ABC)] * 3):
		G[a - 1][b - 1] = G[b - 1][a - 1] = c
	for i in range(N):
		G[i][i] = 0
	for k in range(N):
		for i in range(N):
			for j in range(N):
				if G[i][j] > G[i][k] + G[k][j]:
					G[i][j] = G[i][k] + G[k][j]
	ans = INF
	for plan in permutations(city):
		res = 0
		for i in range(R - 1):
			res += G[plan[i]][plan[i + 1]]
		if ans > res:
			ans = res
	print(ans)
	return

def __starting_point():
	main()
__starting_point()


from itertools import permutations
inf = float('inf')
(n, m, r) = map(int, input().split())
t = tuple((int(x) - 1 for x in input().split()))
g = [[inf] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = inf
for p in permutations(t):
	ans = min(ans, sum((g[a][b] for (a, b) in zip(p, p[1:]))))
print(ans)


import sys
sys.setrecursionlimit(10 ** 6)
input = sys.stdin.readline

def warshallFloyd():
	for k in range(N):
		for i in range(N - 1):
			for j in range(i, N):
				tmp = min(E[i][j], E[i][k] + E[k][j])
				E[i][j] = E[j][i] = tmp

def dfs(c, p, d):
	if c == R:
		ans[0] = min(ans[0], d)
		return
	for i in range(R):
		if used[i]:
			continue
		used[i] = True
		if p == -1:
			dfs(c + 1, i, 0)
		else:
			dfs(c + 1, i, d + E[S[p] - 1][S[i] - 1])
		used[i] = False
(N, M, R) = map(int, input().split())
S = list(map(int, input().split()))
E = [[float('inf')] * N for _ in range(N)]
for i in range(N):
	E[i][i] = 0
for _ in range(M):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	E[a][b] = E[b][a] = c
warshallFloyd()
ans = [float('inf')]
used = [False] * R
dfs(0, -1, 0)
print(*ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
INF = 10 ** 9
d = [[INF] * N for i in range(N)]
r = list(map(int, input().split()))
for i in range(M):
	(A, B, C) = map(int, input().split())
	d[A - 1][B - 1] = C
	d[B - 1][A - 1] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = 10 ** 9
for L in permutations(r):
	temp = 0
	for j in range(R - 1):
		temp += d[L[j] - 1][L[j + 1] - 1]
	ans = min(temp, ans)
print(ans)


(N, M, K) = (int(x) for x in input().split())
R = [int(x) - 1 for x in input().split()]
dist = [[1000000000.0 for _ in range(N)] for _ in range(N)]
for i in range(N):
	dist[i][i] = 0
for _ in range(M):
	(a, b, c) = (int(x) for x in input().split())
	dist[a - 1][b - 1] = dist[b - 1][a - 1] = c
del a, b, c, K
for k in range(N):
	for i in range(N):
		for j in range(i, N):
			dist[i][j] = dist[j][i] = min(dist[i][j], dist[i][k] + dist[k][j])

def mindist(x, X):
	nonlocal dist
	Y = X.copy()
	Y.remove(x)
	if len(Y) == 0:
		return 0
	return min([dist[x][y] + mindist(y, Y) for y in Y])
ans = min([mindist(r, R) for r in R])
print(ans)


from itertools import permutations
(n, m, r) = map(int, input().split())
R = list(map(lambda x: int(x) - 1, input().split()))
INF = 10 ** 18
dist = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	dist[a - 1][b - 1] = c
	dist[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j])
ans = INF
for subset in permutations(R):
	d = 0
	for (v, v2) in zip(subset, subset[1:]):
		d += dist[v][v2]
	if d < ans:
		ans = d
print(ans)


from itertools import permutations
(n, m, r) = list(map(int, input().split()))
rlst = list(map(int, input().split()))
inf = 10 ** 20
d = [[inf] * (n + 1) for _ in range(n + 1)]
for _ in range(m):
	(a, b, c) = list(map(int, input().split()))
	d[a][b] = c
	d[b][a] = c
for k in range(1, n + 1):
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = inf
for comb in permutations(rlst, r):
	i = 0
	tans = 0
	while i + 1 < r:
		tans += d[comb[i]][comb[i + 1]]
		i += 1
	ans = min(ans, tans)
print(ans)


from itertools import permutations
(n, m, r) = map(int, input().split())
rl = list(map(int, input().split()))
d = [[10 ** 8] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
m = 10 ** 9
for i in permutations(rl):
	c = 0
	for j in range(1, r):
		c += d[i[j - 1] - 1][i[j] - 1]
	m = min(m, c)
print(m)


from itertools import permutations
(n, m, r) = map(int, input().split())
A = list(map(int, input().split()))
d = [[1e+18] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = 1e+18
for x in permutations(A):
	l = 0
	for i in range(r - 1):
		l += d[x[i] - 1][x[i + 1] - 1]
	ans = min(ans, l)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
D = [[float('inf') for _ in range(N)] for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	D[a][b] = c
	D[b][a] = c
for i in range(N):
	D[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
ans = float('inf')
for p in list(itertools.permutations(r)):
	d = 0
	for i in range(R - 1):
		d += D[p[i] - 1][p[i + 1] - 1]
	ans = min(d, ans)
print(ans)


from itertools import permutations

def warshall_floyd(n, d):
	for i in range(n):
		for j in range(n):
			for k in range(n):
				d[j][k] = min(d[j][k], d[j][i] + d[i][k])
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[1000000000] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	d[A][B] = C
	d[B][A] = C
warshall_floyd(N + 1, d)
result = 1000000000
for p in permutations(r):
	t = 0
	for i in range(R - 1):
		t += d[p[i]][p[i + 1]]
	result = min(result, t)
print(result)


from itertools import permutations
(n, m, r) = map(int, input().split())
r_l = [int(i) - 1 for i in input().split()]
town = [[float('INF')] * n for _ in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	town[a - 1][b - 1] = c
	town[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			town[i][j] = min(town[i][j], town[i][k] + town[k][j])
res = float('INF')
for root in list(permutations(r_l)):
	tmp = 0
	for i in range(1, r):
		tmp += town[root[i]][root[i - 1]]
	res = min(tmp, res)
print(res)


import itertools
import sys

def main():
	input = sys.stdin.readline
	(n, m, r) = map(int, input().split())
	R = [int(x) for x in input().split()]
	inf = pow(10, 9) + 7
	roads = [[inf] * n for _ in range(n)]
	for i in range(m):
		(a, b, c) = map(int, input().split())
		roads[a - 1][b - 1] = c
		roads[b - 1][a - 1] = c
	for k in range(n):
		for i in range(n):
			for j in range(n):
				if roads[i][j] > roads[i][k] + roads[k][j]:
					roads[i][j] = roads[i][k] + roads[k][j]
	ans = inf
	for value in itertools.permutations(R):
		sub = 0
		for k in range(r - 1):
			sub += roads[value[k] - 1][value[k + 1] - 1]
		if ans > sub:
			ans = sub
	print(ans)

def __starting_point():
	main()
__starting_point()


import itertools
(N, M, R) = map(int, input().split())
r = [int(x) - 1 for x in input().split()]
G = [[10 ** 7 for j in range(N)] for i in range(N)]
for i in range(N):
	G[i][i] = 0
for i in range(M):
	(a, b, c) = map(int, input().split())
	G[a - 1][b - 1] = c
	G[b - 1][a - 1] = c

def warshall_floyd(G):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				G[i][j] = min(G[i][k] + G[k][j], G[i][j])
	return G
G = warshall_floyd(G)
D = 10 ** 8
for L in itertools.permutations(r):
	d = 0
	pre = L[0]
	for l in L[1:]:
		d += G[pre][l]
		pre = l
	D = min(d, D)
print(D)


INF = 10 ** 10
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
dp = [[INF] * (N + 1) for i in range(N + 1)]
for i in range(N + 1):
	dp[i][i] = 0
for i in range(M):
	(a, b, c) = map(int, input().split())
	dp[a][b] = c
	dp[b][a] = c
for k in range(1, N + 1):
	for i in range(1, N + 1):
		for j in range(1, N + 1):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
ans = INF
import itertools
for it in itertools.permutations(r):
	tmpans = 0
	for i in range(1, R):
		tmpans += dp[it[i - 1]][it[i]]
	ans = min(ans, tmpans)
print(ans)


(n, m, r) = map(int, input().split())
s = [[10 ** 9] * n for i in range(n)]
t = list(map(int, input().split()))
for i in range(m):
	(a, b, c) = map(int, input().split())
	s[a - 1][b - 1] = c
	s[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			s[i][j] = min(s[i][j], s[i][k] + s[k][j])
ans = float('inf')
from itertools import permutations
for i in permutations(t):
	an = 0
	for j in range(r - 1):
		an += s[i[j] - 1][i[j + 1] - 1]
	ans = min(ans, an)
print(ans)


import itertools
INF = 10 ** 10
(N, M, R) = map(int, input().split())
(*r,) = map(int, input().split())
r = [v - 1 for v in r]
g = [[INF] * N for _ in range(N)]
for i in range(N):
	g[i][i] = 0
for m in range(M):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = INF
for rs in itertools.permutations(r):
	cost = 0
	for i in range(len(rs) - 1):
		cost += g[rs[i]][rs[i + 1]]
	if cost < ans:
		ans = cost
print(ans)


import itertools
(n, m, r) = map(int, input().split())
D = [[10 ** 10] * n for _ in range(n)]
for i in range(n):
	D[i][i] = 0
R = list(map(int, input().split()))
for i in range(m):
	(a, b, c) = map(int, input().split())
	D[a - 1][b - 1] = c
	D[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
A = []
for v in itertools.permutations(R, r):
	ans = 0
	for i in range(r - 1):
		ans += D[v[i] - 1][v[i + 1] - 1]
	A.append(ans)
print(min(A))


import itertools

def main():
	(n, m, r) = list(map(int, input().split()))
	rx = [int(i) for i in input().split()]
	inf = 100000000
	wf = [[inf] * n for i in range(n)]
	for i in range(n):
		wf[i][i] = 0
	for i in range(m):
		(a, b, c) = list(map(int, input().split()))
		wf[a - 1][b - 1] = c
		wf[b - 1][a - 1] = c
	for k in range(n):
		for i in range(n):
			for j in range(n):
				if wf[i][j] > wf[i][k] + wf[k][j]:
					wf[i][j] = wf[i][k] + wf[k][j]
	cnt = 0
	l = list(itertools.permutations([i for i in range(r)]))
	cnt = inf
	for i in l:
		cnt_sub = 0
		for j in range(r - 1):
			cnt_sub += wf[rx[i[j]] - 1][rx[i[j + 1]] - 1]
		cnt = min(cnt, cnt_sub)
	print(cnt)
main()


(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[float('INF')] * n for i in range(n)]
for i in range(n):
	d[i][i] = 0
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
from itertools import permutations
per = list(permutations(r, R))
ans = float('INF')
for l in per:
	cnt = 0
	for j in range(R - 1):
		cnt += d[l[j] - 1][l[j + 1] - 1]
	ans = min(cnt, ans)
print(ans)


from itertools import permutations
(n, m, r) = map(int, input().split())
visit = list(map(int, input().split()))
dist = [[10 ** 9] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	(dist[a - 1][b - 1], dist[b - 1][a - 1]) = (c, c)
for i in range(n):
	dist[i - 1][i - 1] = 0
for i in range(n):
	for j in range(n):
		for k in range(n):
			dist[j][k] = min(dist[j][k], dist[j][i] + dist[i][k])
ans = 10 ** 9
check = permutations(visit)
for i in check:
	c = 0
	for j in range(1, r):
		c += dist[i[j - 1] - 1][i[j] - 1]
	if c < ans:
		ans = c
print(ans)


from itertools import product, permutations
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
INF = 10 ** 10
d = [[INF for i in range(n)] for j in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for (k, i, j) in product(range(n), range(n), range(n)):
	d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for v in permutations(r, R):
	dis = 0
	for i in range(R - 1):
		dis += d[v[i] - 1][v[i + 1] - 1]
	ans = min(ans, dis)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
rs = list(map(int, input().split()))
INF = float('inf')
es = [[INF for _ in range(N)] for _ in range(N)]
for i in range(N):
	es[i][i] = 0
for _ in range(M):
	(a, b, c) = map(int, input().split())
	es[a - 1][b - 1] = c
	es[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			es[i][j] = min(es[i][j], es[i][k] + es[k][j])
ans = INF
for route in itertools.permutations(rs):
	tmp = 0
	for (a, b) in zip(route, route[1:]):
		tmp += es[a - 1][b - 1]
	ans = min(ans, tmp)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
inf = 10 ** 9
d = [[inf] * N for i in range(N)]
for i in range(M):
	(A, B, C) = map(int, input().split())
	d[A - 1][B - 1] = C
	d[B - 1][A - 1] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = inf
for l in permutations(r):
	start = 0
	for j in range(R - 1):
		start += d[l[j] - 1][l[j + 1] - 1]
	ans = min(start, ans)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
for i in range(R):
	r[i] -= 1
d = [[10000000000] * N for _ in range(N)]
for i in range(N):
	d[i][i] = 0
for i in range(M):
	(A, B, C) = map(int, input().split())
	d[A - 1][B - 1] = C
	d[B - 1][A - 1] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
perms = permutations(r, R)
ans = 10000000000
for perm in perms:
	now = 0
	for i in range(R - 1):
		now += d[perm[i]][perm[i + 1]]
	ans = min(ans, now)
print(ans)


(n, m, r) = map(int, input().split())
must = list(map(lambda x: int(x) - 1, input().split()))
inf = 10 ** 18
wf = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
	wf[i][i] = 0
for _ in range(m):
	(a, b, l) = map(int, input().split())
	a -= 1
	b -= 1
	wf[a][b] = l
	wf[b][a] = l
for k in range(n):
	for i in range(n):
		for j in range(n):
			wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])
can = []
import itertools
for perm in itertools.permutations(range(r)):
	ans = 0
	for i in range(r - 1):
		ans += wf[must[perm[i]]][must[perm[i + 1]]]
	can.append(ans)
print(min(can))


import itertools
(N, M, R) = map(int, input().split())
r = map(int, input().split())
l = [[1000000000.0 for i in range(N + 1)] for i in range(N + 1)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	l[a][b] = l[b][a] = c
for k in range(1, N + 1):
	for i in range(1, N + 1):
		for j in range(1, N + 1):
			t = l[i][k] + l[k][j]
			if l[i][j] > t:
				l[i][j] = t
ans = 1000000000.0
for a in itertools.permutations(r):
	s = 0
	for (i, j) in zip(a[:-1], a[1:]):
		s += l[i][j]
	if ans > s:
		ans = s
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
r = list(map(lambda x: x - 1, r))
d = [[10 ** 10] * N for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(N):
	d[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = 10 ** 10
for i in itertools.permutations(r):
	dist = 0
	for j in range(R - 1):
		dist += d[i[j]][i[j + 1]]
	ans = min(ans, dist)
print(ans)


import itertools
inf = 10 ** 12
(n, m, r) = map(int, input().split())
n += 1
rl = [int(i) for i in input().split()]
d = [[inf for _ in [0] * n] for _ in [0] * n]
for _ in [0] * m:
	(f, t, c) = map(int, input().split())
	d[f][t] = c
	d[t][f] = c
for i in range(n):
	for j in range(n):
		for k in range(n):
			d[j][k] = min(d[j][k], d[j][i] + d[i][k])
ans = inf
for arr in itertools.permutations(rl):
	t = 0
	for i in range(0, r - 1):
		t += d[arr[i]][arr[i + 1]]
	ans = min(ans, t)
print(ans)


import itertools
INF = 10 ** 30
(N, M, R) = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
dist = [[INF] * N for i in range(N)]
for i in range(M):
	(A, B, C) = map(int, input().split())
	(A, B) = (A - 1, B - 1)
	dist[A][B] = C
	dist[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
ans = INF
for x in itertools.permutations(r):
	res = 0
	for i in range(len(x) - 1):
		res += dist[x[i]][x[i + 1]]
	ans = min(ans, res)
print(ans)


from itertools import permutations
INF = 10 ** 8
(N, M, R) = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
MAP = [[INF for i in range(N)] for j in range(N)]
for i in range(M):
	(A, B, C) = map(int, input().split())
	MAP[A - 1][B - 1] = C
	MAP[B - 1][A - 1] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			MAP[i][j] = min(MAP[i][j], MAP[i][k] + MAP[k][j])
ans = INF
for L in permutations(r):
	temp = 0
	for i in range(R - 1):
		temp += MAP[L[i]][L[i + 1]]
	ans = min(ans, temp)
print(ans)


from itertools import *
(n, m, R) = map(int, input().split())
r = map(int, input().split())
d = [[10 ** 18] * n for _ in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(n):
	d[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
m = 10 ** 18
for e in permutations(r):
	t = 0
	for (i, j) in zip(e, e[1:]):
		t += d[i - 1][j - 1]
	m = min(m, t)
print(m)


import itertools
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
abc = [list(map(int, input().split())) for i in range(m)]
inf = float('inf')
data = [[inf] * (n + 1) for i in range(n + 1)]
for i in range(n + 1):
	data[i][i] = 0
for u in abc:
	(a, b, c) = u
	data[a][b] = c
	data[b][a] = c
for k in range(1, n + 1):
	for i in range(1, n + 1):
		for j in range(1, n + 1):
			data[i][j] = min(data[i][j], data[i][k] + data[k][j])
ans = inf
for h in itertools.permutations(r):
	l = 0
	for j in range(1, R):
		l += data[h[j - 1]][h[j]]
	ans = min(ans, l)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
rlist = [int(r) for r in input().split()]
INF = 10 ** 9
E = [[INF] * N for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	E[a - 1][b - 1] = c
	E[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			E[i][j] = min(E[i][j], E[i][k] + E[k][j])
ans = 10 ** 9
for i in range(R):
	rlist[i] -= 1
for p in itertools.permutations(rlist):
	d = 0
	for i in range(1, R):
		d += E[p[i - 1]][p[i]]
	ans = min(ans, d)
print(ans)


(n, m, R) = list(map(int, input().split()))
dist = [[float('INF') if i != j else 0 for i in range(n)] for j in range(n)]
r = list(map(int, input().split()))
for i in range(m):
	(a, b, c) = list(map(int, input().split()))
	(a, b) = (a - 1, b - 1)
	dist[a][b] = c
	dist[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
from itertools import permutations
ans = float('INF')
for e in permutations(r):
	cur = 0
	for i in range(R - 1):
		cur += dist[e[i] - 1][e[i + 1] - 1]
	ans = min(ans, cur)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
INF = float('inf')
d = [[INF for i in range(N)] for i in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(N):
	d[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for l in list(itertools.permutations(r)):
	tmp = 0
	for i in range(R - 1):
		tmp += d[l[i] - 1][l[i + 1] - 1]
	ans = min(tmp, ans)
print(ans)


from itertools import permutations
import sys
input = sys.stdin.readline

def main():
	(N, M, RR) = map(int, input().split())
	R = list(map(int, input().split()))
	INF = float('inf')
	T = [[INF] * N for _ in range(N)]
	for _ in range(M):
		(a, b, c) = tuple(map(int, input().split()))
		T[a - 1][b - 1] = c
		T[b - 1][a - 1] = c
	for k in range(N):
		for i in range(N):
			for j in range(N):
				if T[i][j] > T[i][k] + T[k][j]:
					T[i][j] = T[i][k] + T[k][j]
	print(min((sum((T[rs[i] - 1][rs[i + 1] - 1] for i in range(RR - 1))) for rs in permutations(R))))

def __starting_point():
	main()
__starting_point()


from itertools import permutations
import numpy as np
HUGE = 10 ** 18

def main():
	(n, m, r) = list(map(int, input().split()))
	togo = list(map(int, input().split()))
	adj_mat = [[HUGE for j in range(n)] for i in range(n)]
	for i in range(n):
		adj_mat[i][i] = HUGE
	for i in range(m):
		(a, b, c) = list(map(int, input().split()))
		a0 = a - 1
		b0 = b - 1
		adj_mat[a0][b0] = c
		adj_mat[b0][a0] = c
	adj_mat = np.array(adj_mat)
	wf(adj_mat, n)
	res = HUGE
	for way in permutations(togo):
		assert len(way) > 1
		x = 0
		for i in range(len(way) - 1):
			x += adj_mat[way[i] - 1][way[i + 1] - 1]
		res = min(res, x)
	print(res)

def wf(adj_mat, n):
	for k in range(n):
		for i in range(n):
			adj_mat[i, :] = np.minimum(adj_mat[i, :], adj_mat[i][k] + adj_mat[k, :])

def __starting_point():
	main()
__starting_point()


(n, m, r) = map(int, input().split())
v = list(map(int, input().split()))
inf = 10 ** 18
d = [[inf for _ in range(n)] for _ in range(n)]
for i in range(n):
	d[i][i] = 0
for _ in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
import itertools
ans = 10 ** 18
for a in itertools.permutations(v):
	dis = 0
	for i in range(r - 1):
		dis += d[a[i + 1] - 1][a[i] - 1]
	ans = min(ans, dis)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
(*r,) = map(int, input().split())
inf = float('inf')
d = [[inf] * N for _ in range(N)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	d[A - 1][B - 1] = C
	d[B - 1][A - 1] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = inf
for i in permutations(r):
	v = 0
	for j in range(R - 1):
		v += d[i[j] - 1][i[j + 1] - 1]
	ans = min(ans, v)
print(ans)


from itertools import permutations as perm

def warshall(d, n):
	for k in range(n):
		for i in range(n):
			for j in range(n):
				if d[i][k] + d[k][j] < d[i][j]:
					d[i][j] = d[i][k] + d[k][j]
(n, m, r) = map(int, input().split())
rr = [i - 1 for i in map(int, input().split())]
inf = float('INF')
route = [[inf for j in range(n)] for i in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	route[a - 1][b - 1] = route[b - 1][a - 1] = c
warshall(route, n)
ans = inf
for tmp in perm(rr):
	cost = 0
	for i in range(r - 1):
		cost += route[tmp[i]][tmp[i + 1]]
	if cost < ans:
		ans = cost
print(int(ans))


from itertools import permutations
(N, M, R) = map(int, input().split())
r = [int(i) for i in input().split()]
inf = 10 ** 10
dp = [[[inf] * N for i in range(N)] for i in range(N + 1)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	dp[0][a - 1][b - 1] = c
	dp[0][b - 1][a - 1] = c
for k in range(1, N + 1):
	for j in range(N):
		for i in range(N):
			dp[k][j][i] = min(dp[k - 1][j][i], dp[k - 1][j][k - 1] + dp[k - 1][k - 1][i])
ans = inf
for elem in permutations(r, R):
	dis = 0
	for i in range(R - 1):
		dis += dp[N][elem[i + 1] - 1][elem[i] - 1]
	ans = min(ans, dis)
print(ans)


(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
INF = 10 ** 18
d = [[INF for i in range(N)] for j in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(N):
	d[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
import itertools
ans = INF
for v in itertools.permutations(r, R):
	anss = 0
	for i in range(R - 1):
		anss += d[v[i] - 1][v[i + 1] - 1]
	ans = min(ans, anss)
print(ans)


(N, M, R) = map(int, input().split())
rs = list(map(int, input().split()))
rs = [r - 1 for r in rs]
D = [[100000 * 2000] * N for i in range(N)]
for i in range(N):
	D[i][i] = 0
for i in range(M):
	(A, B, C) = map(int, input().split())
	(A, B) = (A - 1, B - 1)
	D[A][B] = C
	D[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
r = 100000 * 2000
import itertools
for rs2 in itertools.permutations(rs):
	r2 = 0
	for i in range(1, R):
		r2 += D[rs2[i - 1]][rs2[i]]
	r = min(r, r2)
print(r)


(n, m, r) = map(int, input().split())
root = list(map(int, input().split()))
abc = [list(map(int, input().split())) for _ in [0] * m]
inf = 10 ** 10
dist = [[inf for _ in [0] * n] for _ in [0] * n]
for i in range(n):
	dist[i][i] = 0
for (a, b, c) in abc:
	dist[a - 1][b - 1] = c
	dist[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
from itertools import permutations
p = list(permutations(root))
D = 10 ** 10
for i in p:
	d = 0
	for j in range(len(i) - 1):
		d += dist[i[j] - 1][i[j + 1] - 1]
	D = min(D, d)
print(D)


(n, m, R) = map(int, input().split())
r = [int(i) for i in input().split()]
inputs = []
d = [[10 ** 100 for i in range(n + 1)] for i in range(n + 1)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a][b] = c
	d[b][a] = c

def warshall_floyd(d):
	for k in range(n + 1):
		for i in range(n + 1):
			for j in range(n + 1):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
d = warshall_floyd(d)
ans = 10 ** 100
import itertools
for x in itertools.permutations(r):
	p = 0
	for i in range(1, R):
		p += d[x[i - 1]][x[i]]
	ans = min(ans, p)
print(ans)


from itertools import permutations, product

def inpl():
	return tuple(map(int, input().split()))
(N, M, R) = inpl()
inf = float('inf')
G = [[inf for _ in range(N + 1)] for _ in range(N + 1)]
D = inpl()
for _ in range(M):
	(a, b, c) = inpl()
	G[a][b] = c
	G[b][a] = c
for n in range(N + 1):
	G[n][n] = 0
for (i, j, k) in product(range(1, N + 1), repeat=3):
	G[j][k] = min(G[j][k], G[j][i] + G[i][k])
res = inf
for C in permutations(D, len(D)):
	res = min(res, sum([G[C[i]][C[i + 1]] for i in range(len(D) - 1)]))
print(res)


from itertools import permutations

def wf(d):
	n = len(d[0])
	for k in range(n):
		for i in range(n):
			for j in range(n):
				if d[i][j] > d[i][k] + d[k][j]:
					d[i][j] = d[i][k] + d[k][j]
	return d
(n, m, r) = map(int, input().split())
R = min(r, 8)
d = [[10 ** 12 for i in range(n)] for j in range(n)]
l = [int(i) - 1 for i in input().split()]
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = d[b - 1][a - 1] = c
d = wf(d)
ans = 30 ** 8
for v in permutations(l):
	cnt = sum((d[v][u] for (v, u) in zip(v, v[1:])))
	ans = min(ans, cnt)
print(ans)


import sys
from itertools import permutations

def input():
	return sys.stdin.readline().strip()

def main():
	(N, M, R) = list(map(int, input().split()))

	def warshall_floyd(d):
		for k in range(N):
			for i in range(N):
				for j in range(N):
					tmp = d[i][k] + d[k][j]
					if d[i][j] > tmp:
						d[i][j] = tmp
		return d
	r = list(map(int, input().split()))
	d = [[float('inf')] * N for i in range(N)]
	for i in range(M):
		(x, y, z) = list(map(int, input().split()))
		d[x - 1][y - 1] = z
		d[y - 1][x - 1] = z
	for i in range(N):
		d[i][i] = 0
	D = warshall_floyd(d)
	P = list(permutations(r))
	cost = float('inf')
	for p in P:
		c = 0
		for i in range(R - 1):
			c += D[p[i] - 1][p[i + 1] - 1]
		if c < cost:
			cost = c
	print(cost)

def __starting_point():
	main()
__starting_point()


from itertools import product, permutations
INF = 1145141919810
(N, M, R) = map(int, input().split())
r = map(int, input().split())
road = [[0 if x == y else INF for y in range(N + 1)] for x in range(N + 1)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	road[a][b] = road[b][a] = c
for (k, i, j) in product(range(N + 1), repeat=3):
	road[i][j] = min(road[i][j], road[i][k] + road[k][j])
print(min((sum((road[p[i - 1]][p[i]] for i in range(1, R))) for p in permutations(r))))


from itertools import *
(n, m, r) = map(int, input().split())
rc = list(map(int, input().split()))
INF = 10 ** 18
d = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
for i in permutations(rc):
	a = 0
	i = list(i)
	for j in range(r - 1):
		a += d[i[j] - 1][i[j + 1] - 1]
	ans = min(a, ans)
print(ans)


(n, m, z) = map(int, input().split())
r = list(map(int, input().split()))
wf = [[10 ** 10 for i in range(n)] for j in range(n)]
for i in range(m):
	(a, b, c) = list(map(int, input().split()))
	wf[a - 1][b - 1] = c
	wf[b - 1][a - 1] = c
for i in range(n):
	wf[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			wf[i][j] = min(wf[i][j], wf[i][k] + wf[k][j])
from itertools import *
ans = 10 ** 10
for vils in list(permutations(r)):
	cnt = 0
	for i in range(z - 1):
		cnt += wf[vils[i] - 1][vils[i + 1] - 1]
	if ans > cnt:
		ans = cnt
print(ans)


(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
d = [[float('inf')] * N for _ in range(N)]
for i in range(M):
	(a, b, t) = map(int, input().split())
	d[a - 1][b - 1] = t
	d[b - 1][a - 1] = t
for i in range(N):
	d[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
from itertools import groupby, accumulate, product, permutations, combinations
ans = float('inf')
for p in permutations(r, R):
	cnt = 0
	for i in range(1, R):
		cnt += d[p[i - 1] - 1][p[i] - 1]
	ans = min(ans, cnt)
print(ans)


from itertools import permutations

def WarshallFloyd(M):
	N = len(M)
	for k in range(N):
		for j in range(N):
			for i in range(N):
				M[i][j] = min(M[i][j], M[i][k] + M[k][j])
	return M
INF = 10000000000.0
(N, M, K) = map(int, input().split())
R = [int(i) - 1 for i in input().split()]
E = [[INF] * N for _ in range(N)]
for (a, b, c) in (map(int, input().split()) for _ in range(M)):
	a -= 1
	b -= 1
	E[a][b] = E[b][a] = c
E = WarshallFloyd(E)
ans = min((sum((E[p[i]][p[i + 1]] for i in range(K - 1))) for p in permutations(R)))
print(ans)


def Warshall_Floyd(edges, N):
	for k in range(N):
		for i in range(N):
			for j in range(N):
				edges[i][j] = min(edges[i][j], edges[i][k] + edges[k][j])
	return edges
import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
A = [[float('inf') for i in range(N)] for j in range(N)]
for m in range(M):
	(a, b, c) = map(int, input().split())
	A[a - 1][b - 1] = c
	A[b - 1][a - 1] = c
A = Warshall_Floyd(A, N)
ans = float('inf')
for road in list(itertools.permutations(r)):
	length = 0
	for i in range(R - 1):
		length += A[road[i] - 1][road[i + 1] - 1]
	ans = min(ans, length)
print(ans)


import sys
from itertools import permutations
sys.setrecursionlimit(10 ** 6)
INF = float('inf')
MOD = 10 ** 9 + 7

def input():
	return sys.stdin.readline().strip()

def main():
	(N, M, R) = list(map(int, input().split()))
	r = list([int(x) - 1 for x in input().split()])
	P = permutations(r)
	dp = [[float('inf')] * N for _ in range(N)]
	for i in range(N):
		dp[i][i] = 0
	for _ in range(M):
		(a, b, c) = list(map(int, input().split()))
		a -= 1
		b -= 1
		dp[a][b] = c
		dp[b][a] = c
	for k in range(N):
		for i in range(N):
			for j in range(N):
				tmp = dp[i][k] + dp[k][j]
				if tmp < dp[i][j]:
					dp[i][j] = tmp
	ans = float('inf')
	for p in P:
		tmp = 0
		for i in range(R - 1):
			tmp += dp[p[i]][p[i + 1]]
		if tmp < ans:
			ans = tmp
	print(ans)

def __starting_point():
	main()
__starting_point()


import itertools
(n, m, r) = map(int, input().split())
R = min(r, 8)
d = [[10 ** 12 for i in range(n)] for j in range(n)]
l = list(map(int, input().split()))
for i in range(r):
	l[i] -= 1
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = d[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = 10 ** 12
for v in itertools.permutations(l, R):
	cnt = 0
	for i in range(1, R):
		cnt += d[v[i - 1]][v[i]]
	ans = min(ans, cnt)
print(ans)


from itertools import permutations
(N, M, R) = [int(n) for n in input().split()]
r = [int(n) - 1 for n in input().split()]
d = [[100000000] * N for n in range(N)]
for i in range(N):
	d[i][i] = 0
for m in range(M):
	(A, B, C) = [int(n) for n in input().split()]
	A -= 1
	B -= 1
	d[A][B] = C
	d[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][k] + d[k][j], d[i][j])
res = 1000000000
for perm in permutations(range(R)):
	s = 0
	for i in range(R - 1):
		s += d[r[perm[i]]][r[perm[i + 1]]]
	res = min(s, res)
print(res)


import itertools
(N, M, R) = map(int, input().split())
towns = [int(i) for i in input().split()]
path = [[10 ** 20] * N for i in range(N)]
for i in range(N):
	path[i][i] = 0
for i in range(M):
	(a, b, c) = map(int, input().split())
	path[a - 1][b - 1] = c
	path[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(i):
			path[i][j] = min(path[i][j], path[i][k] + path[k][j])
			path[j][i] = path[i][j]
Roots = list(itertools.permutations(towns))
ans = 10 ** 20
s = 0
for r in Roots:
	for n in range(R - 1):
		s += path[r[n] - 1][r[n + 1] - 1]
	ans = min(ans, s)
	s = 0
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
Rlist = list(map(int, input().split()))
Dmat = [[1000000000.0] * N for i in range(N)]
for i in range(N):
	Dmat[i][i] = 0
for i in range(M):
	(f, t, c) = map(int, input().split())
	Dmat[f - 1][t - 1] = c
	Dmat[t - 1][f - 1] = c
for i in range(N):
	for j in range(N):
		for k in range(N):
			Dmat[j][k] = min(Dmat[j][k], Dmat[j][i] + Dmat[i][k])
ans = 1000000000.0
for p in permutations(Rlist):
	ans = min(ans, sum([Dmat[p[i] - 1][p[i + 1] - 1] for i in range(R - 1)]))
print(ans)


import sys, itertools
input = sys.stdin.readline
INF = float('inf')
(n, m, r) = map(int, input().split())
R = list(map(int, input().split()))
distance = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	distance[a - 1][b - 1] = c
	distance[b - 1][a - 1] = c
for i in range(n):
	for j in range(n):
		for k in range(n):
			distance[j][k] = min(distance[j][k], distance[j][i] + distance[i][k])
ans = INF
for p in itertools.permutations(R):
	cnt = 0
	for q in range(r - 1):
		cnt += distance[p[q] - 1][p[q + 1] - 1]
	ans = min(ans, cnt)
print(ans)


import itertools
(N, M, r) = map(int, input().split())
R = [int(a) - 1 for a in input().split()]
X = [[0 if i == j else 1 << 100 for j in range(N)] for i in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	X[a - 1][b - 1] = c
	X[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			X[i][j] = min(X[i][j], X[i][k] + X[k][j])
mi = 1 << 100
for A in itertools.permutations(R, r):
	mi = min(mi, sum([X[A[i - 1]][A[i]] for i in range(1, r)]))
print(mi)


(N, M, R) = map(int, input().split())
(*r,) = map(lambda x: int(x) - 1, input().split())
D = [[float('inf')] * N for _ in range(N)]
for i in range(N):
	D[i][i] = 0
for _ in range(M):
	(a, b, c) = map(int, input().split())
	D[a - 1][b - 1] = c
	D[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			D[i][j] = min(D[i][j], D[i][k] + D[k][j])
import itertools
ans = float('inf')
for s in itertools.permutations(r, R):
	tot = [D[s[i]][s[i + 1]] for i in range(R - 1)]
	tot = sum(tot)
	ans = min(ans, tot)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = [int(x) for x in input().split()]
INF = float('inf')
d = [[INF for x in range(N)] for y in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(N):
	for j in range(N):
		for k in range(N):
			if d[j][i] + d[i][k] < d[j][k]:
				d[j][k] = d[j][i] + d[i][k]
ans = INF
for root in permutations(r):
	cnt = 0
	for i in range(R - 1):
		cnt += d[root[i] - 1][root[i + 1] - 1]
		if cnt > ans:
			break
	ans = min(ans, cnt)
print(ans)


from itertools import permutations as p
(N, M, R) = map(int, input().split())
r = map(int, input().split())
inf = float('inf')
l = [[inf if i != j else 0 for i in range(N)] for j in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	l[a - 1][b - 1] = c
	l[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			l[i][j] = min(l[i][j], l[i][k] + l[k][j])
ans = inf
for i in p(r):
	v = 0
	for j in range(R - 1):
		v += l[i[j] - 1][i[j + 1] - 1]
	ans = min(ans, v)
print(ans)


from itertools import permutations
(N, M, R) = map(int, input().split())
r = list(map(lambda x: int(x) - 1, input().split()))
INF = 10 ** 9
cost = [[INF] * N for _ in range(N)]
for _ in range(M):
	(A, B, C) = map(int, input().split())
	A -= 1
	B -= 1
	cost[A][B] = C
	cost[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			cost[i][j] = min(cost[i][j], cost[i][k] + cost[k][j])
ans = INF
for route in permutations(r):
	temp = 0
	for i in range(R - 1):
		temp += cost[route[i]][route[i + 1]]
	ans = min(ans, temp)
print(ans)


from itertools import permutations as per
(n, m, r) = map(int, input().split())
v = list(map(int, input().split()))
for i in range(r):
	v[i] -= 1
INF = float('inf')
g = [[INF] * n for _ in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for i in range(n):
	g[i][i] = 0
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
ans = INF
for l in per(range(r)):
	t = 0
	for i in range(r - 1):
		t += g[v[l[i]]][v[l[i + 1]]]
	ans = min(ans, t)
print(ans)


(N, M, R) = map(int, input().split())
R = list(map(int, input().split()))
ABC = [tuple(map(int, input().split())) for i in range(M)]
INF = float('inf')
d = [[INF] * N for _ in range(N)]
for i in range(N):
	d[i][i] = 0
for (a, b, c) in ABC:
	(a, b) = (a - 1, b - 1)
	d[a][b] = d[b][a] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
ans = INF
import itertools
for ptn in itertools.permutations(R):
	tmp = 0
	for (a, b) in zip(ptn, ptn[1:]):
		tmp += d[a - 1][b - 1]
	ans = min(ans, tmp)
print(ans)


from itertools import permutations
(n, m, r) = map(int, input().split())
goals = list(map(int, input().split()))
goals = [g - 1 for g in goals]
g = [[pow(10, 16)] * n for i in range(n)]
for i in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	g[a][b] = c
	g[b][a] = c
for i in range(n):
	for s in range(n):
		for t in range(n):
			g[s][t] = min(g[s][t], g[s][i] + g[i][t])
ans = float('inf')
for p in permutations(goals):
	cost = 0
	for (s, t) in zip(p[:-1], p[1:]):
		cost += g[s][t]
	ans = min(ans, cost)
print(ans)


(N, M, r) = map(int, input().split())
R = list(map(lambda x: int(x) - 1, input().split()))
d = [[1 << 30] * N for _ in range(N)]
for i in range(M):
	(a, b, t) = map(int, input().split())
	d[a - 1][b - 1] = t
	d[b - 1][a - 1] = t
for i in range(N):
	d[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
from itertools import groupby, accumulate, product, permutations, combinations
ans = 1 << 30
for per in permutations(R, r):
	cnt = 0
	for i in range(r - 1):
		cnt += d[per[i]][per[i + 1]]
	ans = min(ans, cnt)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = [int(x) - 1 for x in input().split()]
dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	dist[a][b] = dist[b][a] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
ans = float('inf')
k = list(itertools.permutations(r))
for i in k:
	sumDist = 0
	for j in range(len(i) - 1):
		sumDist += dist[i[j]][i[j + 1]]
	ans = min(ans, sumDist)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
for i in range(R):
	r[i] -= 1
inf = 10 ** 12
dp = [[inf] * N for i in range(N)]
for i in range(M):
	(A, B, C) = map(int, input().split())
	A -= 1
	B -= 1
	dp[A][B] = C
	dp[B][A] = C
for k in range(N):
	for i in range(N):
		for j in range(N):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
l = list(range(R))
m = inf
for x in itertools.permutations(l, R):
	count = 0
	for i in range(len(l) - 1):
		count += dp[r[x[i]]][r[x[i + 1]]]
	m = min(count, m)
print(m)


(t, r, g) = map(int, input().split())
d = [[float('INF') for ccc in range(t)] for i in range(t)]
for i in range(t):
	d[i][i] = 0
gl = list(map(int, input().split()))
for j in range(r):
	(x, y, c) = map(int, input().split())
	d[x - 1][y - 1] = c
	d[y - 1][x - 1] = c
for i in range(t):
	for j in range(t):
		for k in range(t):
			d[k][j] = min(d[k][j], d[k][i] + d[i][j])
from itertools import permutations as pe
ans = float('INF')
for G in pe(gl):
	p = 0
	for qw in range(g - 1):
		p += d[G[qw] - 1][G[qw + 1] - 1]
	ans = min(ans, p)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
r = list(map(int, input().split()))
p = list(itertools.permutations(r))
dp = [[float('inf')] * N for _ in range(N)]
for i in range(M):
	(a, b, c) = map(int, input().split())
	dp[a - 1][b - 1] = c
	dp[b - 1][a - 1] = c
for i in range(N):
	dp[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
ans = float('inf')
for v in p:
	temp = 0
	for i in range(len(v) - 1):
		temp += dp[v[i] - 1][v[i + 1] - 1]
	ans = min(ans, temp)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
s = list(map(int, input().split()))
D = [[10 ** 9 + 7 for k in range(N)] for l in range(N)]
for k in range(N):
	D[k][k] = 0
for k in range(M):
	(A, B, C) = map(int, input().split())
	A -= 1
	B -= 1
	D[A][B] = C
	D[B][A] = C
for p in range(N):
	for q in range(N):
		for r in range(N):
			D[q][r] = min(D[q][r], D[q][p] + D[p][r])
cities = list(itertools.permutations(s))
ans = 10 ** 9 + 7
for p in cities:
	t = 0
	for k in range(R - 1):
		t += D[p[k] - 1][p[k + 1] - 1]
	ans = min(ans, t)
print(ans)


from itertools import permutations
(N, M, R, *L) = map(int, open(0).read().split())
city = L[:R]
inf = L[R:]
dist = [[10 ** 10] * N for i in range(N)]
for (x, y, c) in zip(*[iter(inf)] * 3):
	dist[x - 1][y - 1] = c
	dist[y - 1][x - 1] = c
for i in range(N):
	dist[i][i] = 0
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][k] + dist[k][j], dist[i][j])
ans = 10 ** 10
for m in permutations(city):
	n = 0
	for i in range(R - 1):
		n += dist[m[i] - 1][m[i + 1] - 1]
	ans = min(ans, n)
print(ans)


import itertools
(N, M, R) = [int(i) for i in input().split()]
rr = [int(i) for i in input().split()]
d = [[1 << 60 for j in range(N)] for i in range(N)]
for i in range(M):
	(a, b, c) = [int(i) for i in input().split()]
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for k in range(N):
	for i in range(N):
		for j in range(N):
			if d[i][j] > d[i][k] + d[k][j]:
				d[i][j] = d[i][k] + d[k][j]
ans = 1 << 60
for plan in itertools.permutations(rr):
	cnt = 0
	for i in range(R - 1):
		cnt += d[plan[i] - 1][plan[i + 1] - 1]
	ans = min(ans, cnt)
print(ans)


import itertools
(N, M, R) = map(int, input().split())
cities = list(map(int, input().split()))
INF = 10 ** 10
dp = [[INF] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
	(s, g, c) = map(int, input().split())
	dp[s][g] = c
	dp[g][s] = c
for i in range(N + 1):
	dp[i][i] = 0
for k in range(1, N + 1):
	for i in range(1, N + 1):
		for j in range(1, N + 1):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
patterns = itertools.permutations(cities, R)
ans = INF
for ptn in patterns:
	dis = 0
	for i in range(R - 1):
		dis += dp[ptn[i]][ptn[i + 1]]
	ans = min(ans, dis)
print(ans)


def i1():
	return int(input())

def i2():
	return [int(i) for i in input().split()]
[N, M, R] = i2()
r = i2()
f = float('inf')
d = [[f for j in range(N)] for i in range(N)]
for i in range(N):
	d[i][i] = 0
for i in range(M):
	a = i2()
	d[a[0] - 1][a[1] - 1] = a[2]
	d[a[1] - 1][a[0] - 1] = a[2]
for i in range(N):
	for j in range(N):
		for k in range(N):
			d[j][k] = min(d[j][k], d[j][i] + d[i][k])
c = float('inf')
import itertools
for i in list(itertools.permutations(r)):
	ddd = 0
	for j in range(len(i) - 1):
		ddd += d[i[j] - 1][i[j + 1] - 1]
	c = min(c, ddd)
print(c)


import itertools
(n, m, R) = map(int, input().split())
r = list(map(int, input().split()))
g = [[100000 * 200 + 1] * n for i in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	g[a - 1][b - 1] = g[b - 1][a - 1] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			g[i][j] = min(g[i][j], g[i][k] + g[k][j])
print(min((sum((g[s[i - 1] - 1][s[i] - 1] for i in range(1, R))) for s in itertools.permutations(r))))


(n, m, r) = map(int, input().split())
r = list(map(int, input().split()))
r = [t - 1 for t in r]
abc = [list(map(int, input().split())) for _ in range(m)]
d = [[2 ** 30 for _ in range(n)] for _ in range(n)]
for t in abc:
	d[t[0] - 1][t[1] - 1] = t[2]
	d[t[1] - 1][t[0] - 1] = t[2]
for k in range(n):
	for i in range(n):
		for j in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])
from itertools import permutations
res = []
for t in permutations(r):
	ans = 0
	for i in range(1, len(r)):
		ans += d[t[i - 1]][t[i]]
	res.append(ans)
print(min(res))


from itertools import permutations as p

def wf(d):
	for k in range(n):
		for i in range(n):
			for j in range(n):
				d[i][j] = min(d[i][j], d[i][k] + d[k][j])
	return d
(n, m, r) = map(int, input().split())
d = [[10 ** 9] * n for i in range(n)]
rl = list(map(int, input().split()))
for i in range(m):
	(a, b, c) = map(int, input().split())
	d[a - 1][b - 1] = c
	d[b - 1][a - 1] = c
for i in range(n):
	d[i][i] = 0
dl = wf(d)
pl = list(p(rl, r))
x = 10 ** 9
for i in pl:
	t = 0
	for j in range(r - 1):
		t += dl[i[j] - 1][i[j + 1] - 1]
	x = min(x, t)
print(x)


(n, m, r) = map(int, input().split())
R = tuple(map(lambda x: int(x) - 1, input().split()))
INF = 10 ** 9
dp = [[INF] * n for _ in range(n)]
for _ in range(m):
	(a, b, c) = map(int, input().split())
	a -= 1
	b -= 1
	dp[a][b] = c
	dp[b][a] = c
for k in range(n):
	for i in range(n):
		for j in range(n):
			dp[i][j] = min(dp[i][j], dp[i][k] + dp[k][j])
from itertools import permutations
ans = INF
for p in permutations(R):
	dist = 0
	for i in range(r - 1):
		dist += dp[p[i]][p[i + 1]]
	ans = min(ans, dist)
print(ans)