Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**6
path=[[INF]*n for i in range(n)]
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 i in range(n):
  for j in range(n):
    for k in range(n):
      if i!=j and j!=k and k!=i:
        path[i][j]=min(path[i][j],path[i][k]+path[k][j])
        path[j][i]=path[i][j]
ans=10**9
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(ans)

Fixed submission:
from itertools import permutations
from scipy.sparse.csgraph import floyd_warshall
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**18
path=[[INF]*n for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  path[a-1][b-1]=c
  path[b-1][a-1]=c
path=floyd_warshall(path)
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**6
path=[[INF]*n for i in range(n)]
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 i in range(n):
  for j in range(n):
    for k in range(n):
      if i!=j and j!=k and k!=i:
        path[i][j]=min(path[i][j],path[i][k]+path[k][j])
        path[j][i]=path[i][j]
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(ans)

Fixed submission:
from itertools import permutations
from scipy.sparse.csgraph import floyd_warshall
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**18
path=[[INF]*n for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  path[a-1][b-1]=c
  path[b-1][a-1]=c
path=floyd_warshall(path)
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(int(ans))

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**6
path=[[INF]*n for i in range(n)]
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 i in range(n):
  for j in range(n):
    for k in range(n):
      if i!=j and j!=k and k!=i:
        path[i][j]=min(path[i][j],path[i][k]+path[k][j])
        path[j][i]=path[i][j]
seq=[i for i in range(r)]
num=list(itertools.permutations(seq))
ans=10**18
for root in num:
  cnt=0
  for j in range(1,len(root)):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(ans)

Fixed submission:
from itertools import permutations
from scipy.sparse.csgraph import floyd_warshall
n,m,r=map(int,input().split())
vis=list(map(int,input().split()))
INF=10**18
path=[[INF]*n for i in range(n)]
for i in range(m):
  a,b,c=map(int,input().split())
  path[a-1][b-1]=c
  path[b-1][a-1]=c
path=floyd_warshall(path)
ans=10**18
for root in permutations(vis):
  cnt=0
  for j in range(1,r):
    cnt+=path[root[j-1]-1][root[j]-1]
  ans=min(ans,cnt)
print(int(ans))