Buggy python submission (verdict: Time Limit Exceeded):
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):
      edges[i][j] = min(edges[i][k]+edges[k][j], edges[i][j])
      edges[j][i] = edges[i][j]

# print(edges)

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)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

n,m,r = map(int,input().split())
R = list(map(int,input().split()))

edges=[[10**9 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+1):
    for j in range(i,n+1):
      edges[i][j] = min(edges[i][k]+edges[k][j], edges[i][j])
      edges[j][i] = edges[i][j]

# print(edges)

permutations
ans = 10**9
for i in permutations(R):
  tmp = 0
  for j in range(r-1):
    # print("i",i,"j",j)
    tmp += edges[i[j]][i[j+1]]
  ans = min(tmp, ans)

print(ans)

Fixed submission:
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)

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations

n,m,r = map(int,input().split())
R = list(map(int,input().split()))

edges=[[10**9 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+1):
    for j in range(1,n+1):
      edges[i][j] = min(edges[i][k]+edges[k][j], edges[i][j])
      edges[j][i] = edges[i][j]

# print(edges)

permutations
ans = 10**9
for i in permutations(R):
  tmp = 0
  for j in range(r-1):
    # print("i",i,"j",j)
    tmp += edges[i[j]][i[j+1]]
  ans = min(tmp, ans)

print(ans)

Fixed submission:
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)