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

N, M, R = map(int, input().split())
r = list(map(int, input().split()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

for i in range(N + 1):
    for j in range(N + 1):
        w_ji = way[j][i]
        for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
            if k == 0:
                continue
            tmp = w_ji + w_ik
            if w_jk > tmp:
                way[j][k] = tmp
ans = float("inf")
for path in permutations(r):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
print(ans)


Fixed submission:
from itertools import permutations

def wfa(N):
    for i in range(N + 1):
        for j in range(N + 1):
            w_ji = way[j][i]
            for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
                if k == 0:
                    continue
                tmp = w_ji + w_ik
                if w_jk > tmp:
                    way[j][k] = tmp


N, M, R = map(int, input().split())
r = list(map(int, input().split()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

wfa(N)

ans = float("inf")
for path in permutations(r):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
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()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

for i in range(N + 1):
    for j in range(N + 1):
        w_ji = way[j][i]
        for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
            if k == 0:
                continue
            tmp = w_ji + w_ik
            if w_jk > tmp:
                way[j][k] = tmp
ans = float("inf")
for path in list(permutations(r)):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
print(ans)


Fixed submission:
from itertools import permutations

def wfa(N):
    for i in range(N + 1):
        for j in range(N + 1):
            w_ji = way[j][i]
            for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
                if k == 0:
                    continue
                tmp = w_ji + w_ik
                if w_jk > tmp:
                    way[j][k] = tmp


N, M, R = map(int, input().split())
r = list(map(int, input().split()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

wfa(N)

ans = float("inf")
for path in permutations(r):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
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()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

for i in range(N + 1):
    for j in range(N + 1):
        for k in range(N + 1):
            way[j][k] = min(way[j][k], way[j][i] + way[i][k])
ans = float("inf")
for path in list(permutations(r)):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
print(ans)


Fixed submission:
from itertools import permutations

def wfa(N):
    for i in range(N + 1):
        for j in range(N + 1):
            w_ji = way[j][i]
            for k, (w_ik, w_jk) in enumerate(zip(way[i], way[j])):
                if k == 0:
                    continue
                tmp = w_ji + w_ik
                if w_jk > tmp:
                    way[j][k] = tmp


N, M, R = map(int, input().split())
r = list(map(int, input().split()))

way = [[float("inf") if i != j else 0 for i in range(N + 1)] for j in range(N + 1)]
for m in range(M):
    a, b, c = map(int, input().split())
    way[a][b] = c
    way[b][a] = c

wfa(N)

ans = float("inf")
for path in permutations(r):
    time = 0
    for i in range(len(path) - 1):
        time += way[path[i]][path[i + 1]]
    ans = min(ans, time)
print(ans)