Buggy python submission (verdict: Time Limit Exceeded):
import itertools
import heapq
N, M, R = map(int, input().split())
Visit = [int(i) for i in input().split()]
for i in range(R):
    Visit[i] -= 1
P = [[] for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    P[a-1].append((c, b-1))
    P[b-1].append((c, a-1))

def Dijk(i):
    Fin = [False for i in range(N)]
    Cost = [float("inf") for i in range(N)]
    Cost[i] = 0
    Q = [(0, i)]
    heapq.heapify(Q)
    while Q:
        cost, node = heapq.heappop(Q)
        Fin[node] = True
        for next in P[node]:
            if Fin[next[1]] == False:
                if next[0] + cost < Cost[next[1]]:
                    Cost[next[1]] = next[0] + cost
                    heapq.heappush(Q, (Cost[next[1]], next[1]))
    return Cost

E = []
for i in range(N):
    E.append(Dijk(i))

ans = float("inf")
for Order in itertools.permutations(Visit):
    temp = 0
    for i in range(R-1):
        temp += E[Order[i]][Order[i+1]]
    if temp < ans:
        ans = temp
print(ans)


Fixed submission:
import itertools
import heapq
N, M, R = map(int, input().split())
Visit = [int(i) for i in input().split()]
for i in range(R):
    Visit[i] -= 1
P = [[] for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    P[a-1].append((c, b-1))
    P[b-1].append((c, a-1))

def Dijk(i):
    Fin = [False for i in range(N)]
    Cost = [float("inf") for i in range(N)]
    Cost[i] = 0
    Q = [(0, i)]
    heapq.heapify(Q)
    while Q:
        cost, node = heapq.heappop(Q)
        if Fin[node] == False:
            Fin[node] = True
            for next in P[node]:
                if Fin[next[1]] == False:
                    if next[0] + cost < Cost[next[1]]:
                        Cost[next[1]] = next[0] + cost
                        heapq.heappush(Q, (Cost[next[1]], next[1]))
    return Cost

E = []
for i in range(N):
    E.append(Dijk(i))

ans = float("inf")
for Order in itertools.permutations(Visit):
    temp = 0
    for i in range(R-1):
        temp += E[Order[i]][Order[i+1]]
    if temp < ans:
        ans = temp
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import itertools
N, M, R = map(int, input().split())
Visit = [int(i) for i in input().split()]
for i in range(R):
    Visit[i] -= 1

E = [[float("inf") for i in range(N)] for j in range(N)]
for i in range(N):
    E[i][i] = 0
for i in range(M):
    a, b, c = map(int, input().split())
    E[a-1][b-1], E[b-1][a-1] = c, 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 = float("inf")
for Order in itertools.permutations(Visit):
    temp = 0
    for i in range(R-1):
        temp += E[Order[i]][Order[i+1]]
    if temp < ans:
        ans = temp
print(ans)

Fixed submission:
import itertools
import heapq
N, M, R = map(int, input().split())
Visit = [int(i) for i in input().split()]
for i in range(R):
    Visit[i] -= 1
P = [[] for i in range(N)]
for i in range(M):
    a, b, c = map(int, input().split())
    P[a-1].append((c, b-1))
    P[b-1].append((c, a-1))

def Dijk(i):
    Fin = [False for i in range(N)]
    Cost = [float("inf") for i in range(N)]
    Cost[i] = 0
    Q = [(0, i)]
    heapq.heapify(Q)
    while Q:
        cost, node = heapq.heappop(Q)
        if Fin[node] == False:
            Fin[node] = True
            for next in P[node]:
                if Fin[next[1]] == False:
                    if next[0] + cost < Cost[next[1]]:
                        Cost[next[1]] = next[0] + cost
                        heapq.heappush(Q, (Cost[next[1]], next[1]))
    return Cost

E = []
for i in range(N):
    E.append(Dijk(i))

ans = float("inf")
for Order in itertools.permutations(Visit):
    temp = 0
    for i in range(R-1):
        temp += E[Order[i]][Order[i+1]]
    if temp < ans:
        ans = temp
print(ans)