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

N, M, R = map(int, input().split())
r = [int(x) - 1 for x in input().split()]

dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	a, b, c = map(int, input().split())
	a -= 1
	b -= 1
	dist[a][b] = dist[b][a] = c

for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

ans = float('inf')
k = list(itertools.permutations(r))
for i in k:
	sumDist = 0
	for j in range(len(i) - 1):
		sumDist += dist[i[j]][i[j+1]]
	ans = min(ans, sumDist)
print(ans)

Fixed submission:
import itertools
 
N, M, R = map(int, input().split())
r = [int(x) - 1 for x in input().split()]
 
dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	a, b, c = map(int, input().split())
	a -= 1
	b -= 1
	dist[a][b] = dist[b][a] = c
 
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
ans = float('inf')
k = list(itertools.permutations(r))
for i in k:
	sumDist = 0
	for j in range(len(i) - 1):
		sumDist += dist[i[j]][i[j+1]]
	ans = min(ans, sumDist)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
N, M, R = map(int, input().split())
r = list(map(lambda x:int(x) - 1, input().split()))

dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	tmp = list(map(int, input().split()))
	dist[tmp[0] - 1][tmp[1] - 1] = dist[tmp[1] - 1][tmp[0] - 1] = tmp[2]

for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

ans = float('inf')
seen = []
def DFS():
	global ans
	for i in range(len(r)):
		if i not in seen:
			seen.append(i)
			if len(seen) == len(r):
				sumDist = 0
				for j in range(len(seen) - 1):
					sumDist += dist[r[seen[j]]][r[seen[j+1]]]
				ans = min(ans, sumDist)
			else:
				DFS()
			del seen[-1]

DFS()
print(ans)

Fixed submission:
import itertools
 
N, M, R = map(int, input().split())
r = [int(x) - 1 for x in input().split()]
 
dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	a, b, c = map(int, input().split())
	a -= 1
	b -= 1
	dist[a][b] = dist[b][a] = c
 
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
ans = float('inf')
k = list(itertools.permutations(r))
for i in k:
	sumDist = 0
	for j in range(len(i) - 1):
		sumDist += dist[i[j]][i[j+1]]
	ans = min(ans, sumDist)
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
'''
ワーシャルフロイド法
例題:https://atcoder.jp/contests/abc073/tasks/abc073_d
'''

N, M, R = map(int, input().split())
r = list(map(lambda x:int(x) - 1, input().split()))

dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	tmp = list(map(int, input().split()))
	dist[tmp[0] - 1][tmp[1] - 1] = dist[tmp[1] - 1][tmp[0] - 1] = tmp[2]

for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

ans = float('inf')
seen = []
def DFS():
	global ans
	for i in range(len(r)):
		if r[i] not in seen:
			seen.append(r[i])
			if len(seen) == len(r):
				sumDist = 0
				for j in range(len(seen) - 1):
					sumDist += dist[seen[j]][seen[j+1]]
				ans = min(ans, sumDist)
			else:
				DFS()
			del seen[-1]

DFS()
print(ans)

Fixed submission:
import itertools
 
N, M, R = map(int, input().split())
r = [int(x) - 1 for x in input().split()]
 
dist = [[float('inf')] * N for _ in range(N)]
for _ in range(M):
	a, b, c = map(int, input().split())
	a -= 1
	b -= 1
	dist[a][b] = dist[b][a] = c
 
for k in range(N):
	for i in range(N):
		for j in range(N):
			dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
 
ans = float('inf')
k = list(itertools.permutations(r))
for i in k:
	sumDist = 0
	for j in range(len(i) - 1):
		sumDist += dist[i[j]][i[j+1]]
	ans = min(ans, sumDist)
print(ans)