Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
import sys
N, M, R = map(int, input().split())
rs = [x - 1 for x in map(int, input().split())]
# グラフを作る
dist = [[float('inf')] * (N) for _ in range(N)]
for e in sys.stdin:
    A, B, C = map(int, e.split())
    dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
# ワーシャルフロイドで全頂点間距離算出
for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
# 組み合わせ全探索
ans = float('inf')
for p in permutations(rs):
    tmp = 0
    for u, v in zip(p, p[1:]):
        tmp += dist[u][v]
        if tmp > ans:
            break
    else:
        ans = tmp
print(ans)

Fixed submission:
from itertools import permutations
import sys
def main():
    N, M, R = map(int, input().split())
    rs = [x - 1 for x in map(int, input().split())]
    # グラフを作る
    dist = [[float('inf')] * (N) for _ in range(N)]
    for e in sys.stdin:
        A, B, C = map(int, e.split())
        dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
    # ワーシャルフロイドで全頂点間距離算出
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
    # 組み合わせ全探索
    ans = float('inf')
    for p in permutations(rs):
        tmp = 0
        for u, v in zip(p, p[1:]):
            tmp += dist[u][v]
            if tmp > ans:
                break
        else:
            ans = tmp
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
N, M, R = map(int, input().split())
rs = [x - 1 for x in map(int, input().split())]
dist = [[float('inf')] * (N) for _ in range(N)]
for _ in range(M):
    A, B, C = map(int, input().split())
    dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
ans = float('inf')
for p in permutations(rs):
    tmp = 0
    for i in range(R - 1):
        tmp += dist[p[i]][p[i + 1]]
        if tmp > ans:
            break
    else:
        ans = tmp
print(ans)

Fixed submission:
from itertools import permutations
import sys
def main():
    N, M, R = map(int, input().split())
    rs = [x - 1 for x in map(int, input().split())]
    # グラフを作る
    dist = [[float('inf')] * (N) for _ in range(N)]
    for e in sys.stdin:
        A, B, C = map(int, e.split())
        dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
    # ワーシャルフロイドで全頂点間距離算出
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
    # 組み合わせ全探索
    ans = float('inf')
    for p in permutations(rs):
        tmp = 0
        for u, v in zip(p, p[1:]):
            tmp += dist[u][v]
            if tmp > ans:
                break
        else:
            ans = tmp
    print(ans)
if __name__ == '__main__':
    main()

Buggy python submission (verdict: Time Limit Exceeded):
from itertools import permutations
N, M, R = map(int, input().split())
r = [x - 1 for x in map(int, input().split())]
dist = [[float('inf')] * (N) for _ in range(N)]
for i in range(N): dist[i][i] = 0
for _ in range(M):
    A, B, C = map(int, input().split())
    dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
# Warshall–Floyd algorithm
for k in range(N):
    for i in range(N):
        for j in range(N):
            if dist[i][j] > dist[i][k] + dist[k][j]:
                dist[i][j] = dist[i][k] + dist[k][j]
ans = float('inf')
for p in permutations(r, R):
    ans = min(ans, sum(dist[u][v] for u, v in zip(p, p[1:])))
print(ans)

Fixed submission:
from itertools import permutations
import sys
def main():
    N, M, R = map(int, input().split())
    rs = [x - 1 for x in map(int, input().split())]
    # グラフを作る
    dist = [[float('inf')] * (N) for _ in range(N)]
    for e in sys.stdin:
        A, B, C = map(int, e.split())
        dist[A - 1][B - 1] = dist[B - 1][A - 1] = C
    # ワーシャルフロイドで全頂点間距離算出
    for k in range(N):
        for i in range(N):
            for j in range(N):
                if dist[i][j] > dist[i][k] + dist[k][j]:
                    dist[i][j] = dist[i][k] + dist[k][j]
    # 組み合わせ全探索
    ans = float('inf')
    for p in permutations(rs):
        tmp = 0
        for u, v in zip(p, p[1:]):
            tmp += dist[u][v]
            if tmp > ans:
                break
        else:
            ans = tmp
    print(ans)
if __name__ == '__main__':
    main()