Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from sys import stdin
    input = stdin.readline

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    edge = [[] for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        edge[i-1].append((j-1, k))
        edge[j-1].append((i-1, k))

    # dijkstra
    from heapq import heapify, heappush, heappop

    distance = [[-1] * n for _ in range(n)]
    for start in range(n):
        todo = [(0, start)]
        heapify(todo)

        while len(todo) > 0:
            cost, node = heappop(todo)
            if distance[start][node] == -1:
                distance[start][node] = cost

                for i in edge[node]:
                    if distance[start][i[0]] == -1:
                        heappush(todo, (cost + i[1], i[0]))

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += distance[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()

Fixed submission:
def main():
    from sys import stdin
    input = stdin.readline

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    d = [[float('inf')] * n for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        d[i-1][j-1] = k
        d[j-1][i-1] = k

    # Warshall-Floyd algorithm
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += d[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()

Buggy python submission (verdict: Time Limit Exceeded):
def main():
    from sys import stdin
    def input():
        return stdin.readline().strip()

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    edge = [[] for _ in range(n)]
    weight = [[] for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        i -= 1
        j -= 1
        edge[i].append(j)
        edge[j].append(i)
        weight[i].append(k)
        weight[j].append(k)

    # dijkstra
    from heapq import heapify, heappush, heappop

    distance = [[-1] * n for _ in range(n)]
    for start in range(n):
        todo = [(0, start)]
        heapify(todo)

        while len(todo) > 0:
            cost, node = heappop(todo)
            if distance[start][node] == -1:
                distance[start][node] = cost

                for i in range(len(edge[node])):
                    if distance[start][edge[node][i]] == -1:
                        heappush(todo, (cost + weight[node][i], edge[node][i]))

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += distance[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()

Fixed submission:
def main():
    from sys import stdin
    input = stdin.readline

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    d = [[float('inf')] * n for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        d[i-1][j-1] = k
        d[j-1][i-1] = k

    # Warshall-Floyd algorithm
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += d[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()

Buggy python submission (verdict: Time Limit Exceeded):
from sys import stdin
def input():
    return stdin.readline().strip()

# 8! = 40320

n, m, r = map(int, input().split())
l = list(map(int, input().split()))
for i in range(r):
    l[i] -= 1
edge = [[] for _ in range(n)]
weight = [[] for _ in range(n)]
for _ in range(m):
    i, j, k = map(int, input().split())
    i -= 1
    j -= 1
    edge[i].append(j)
    edge[j].append(i)
    weight[i].append(k)
    weight[j].append(k)

# dijkstra
from heapq import heapify, heappush, heappop

distance = [[-1] * n for _ in range(n)]
for start in range(n):
    todo = [(0, start)]
    heapify(todo)

    while len(todo) > 0:
        cost, node = heappop(todo)
        if distance[start][node] == -1:
            distance[start][node] = cost

            for i in range(len(edge[node])):
                if distance[start][edge[node][i]] == -1:
                    heappush(todo, (cost + weight[node][i], edge[node][i]))

# full search
from itertools import permutations

answer = float('inf')
for i in permutations(l):
    ans = 0
    for j in range(r-1):
        ans += distance[i[j]][i[j+1]]
    if ans < answer:
        answer = ans

print(answer)

Fixed submission:
def main():
    from sys import stdin
    input = stdin.readline

    n, m, r = map(int, input().split())
    l = list(map(int, input().split()))
    for i in range(r):
        l[i] -= 1
    d = [[float('inf')] * n for _ in range(n)]
    for _ in range(m):
        i, j, k = map(int, input().split())
        d[i-1][j-1] = k
        d[j-1][i-1] = k

    # Warshall-Floyd algorithm
    for k in range(n):
        for i in range(n):
            for j in range(n):
                if d[i][j] > d[i][k] + d[k][j]:
                    d[i][j] = d[i][k] + d[k][j]

    # full search
    # 8! = 40320
    from itertools import permutations

    answer = float('inf')
    for i in permutations(l):
        ans = 0
        for j in range(r-1):
            ans += d[i[j]][i[j+1]]
        if ans < answer:
            answer = ans

    print(answer)

main()