Buggy python submission (verdict: Time Limit Exceeded):
import sys, heapq, itertools
input = sys.stdin.readline
inf = float('inf')
N, M, R = map(int, input().split())
r = list(map(lambda x: int(x)-1, input().split()))

sec = [[inf] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            sec[i][j] == 0
for abc in [list(map(int, input().split())) for _ in range(M)]: 
    sec[abc[0]-1][abc[1]-1] = abc[2]
    sec[abc[1]-1][abc[0]-1] = abc[2]
def marsh():
    for k in range(N):
        for i in range(N):
            for j in range(N):
                sec[i][j] = min(sec[i][j] , sec[i][k] + sec[k][j])

marsh()
cost = inf
for perm in itertools.permutations(r):
    temp = 0
    for i in range(R-1):
        temp += sec[perm[i]][perm[i+1]]
    if temp < cost:
        cost = temp

print(cost)



Fixed submission:
import sys, heapq, itertools
input = sys.stdin.readline
inf = float('inf')
N, M, R = map(int, input().split())
r = list(map(lambda x: int(x)-1, input().split()))

sec = [[inf] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            sec[i][j] == 0
for abc in [list(map(int, input().split())) for _ in range(M)]: 
    sec[abc[0]-1][abc[1]-1] = abc[2]
    sec[abc[1]-1][abc[0]-1] = abc[2]
def marsh():
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if sec[i][j] > sec[i][k] + sec[k][j]:
                    sec[i][j] = sec[i][k] + sec[k][j]
marsh()
cost = inf
for perm in itertools.permutations(r):
    temp = 0
    for i in range(R-1):
        temp += sec[perm[i]][perm[i+1]]
    if temp < cost:
        cost = temp

print(cost)

Buggy python submission (verdict: Time Limit Exceeded):
import sys, heapq, itertools
input = sys.stdin.readline
inf = float('inf')
N, M, R = map(int, input().split())
r = list(map(lambda x: int(x)-1, input().split()))

sec = [[inf] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            sec[i][j] == 0
for abc in [list(map(int, input().split())) for _ in range(M)]: 
    sec[abc[0]-1][abc[1]-1] = abc[2]
    sec[abc[1]-1][abc[0]-1] = abc[2]

for k in range(N):
    for i in range(N):
        for j in range(N):
            if sec[i][j] > sec[i][k] + sec[k][j]:
                sec[i][j] = sec[i][k] + sec[k][j]

cost = inf
for perm in itertools.permutations(r):
    temp = 0
    for i in range(R-1):
        temp += sec[perm[i]][perm[i+1]]
    if temp < cost:
        cost = temp

print(cost)



Fixed submission:
import sys, heapq, itertools
input = sys.stdin.readline
inf = float('inf')
N, M, R = map(int, input().split())
r = list(map(lambda x: int(x)-1, input().split()))

sec = [[inf] * N for _ in range(N)]
for i in range(N):
    for j in range(N):
        if i == j:
            sec[i][j] == 0
for abc in [list(map(int, input().split())) for _ in range(M)]: 
    sec[abc[0]-1][abc[1]-1] = abc[2]
    sec[abc[1]-1][abc[0]-1] = abc[2]
def marsh():
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if sec[i][j] > sec[i][k] + sec[k][j]:
                    sec[i][j] = sec[i][k] + sec[k][j]
marsh()
cost = inf
for perm in itertools.permutations(r):
    temp = 0
    for i in range(R-1):
        temp += sec[perm[i]][perm[i+1]]
    if temp < cost:
        cost = temp

print(cost)