Buggy python submission (verdict: Time Limit Exceeded):
import sys, copy

def wf(d):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        d[i][j] = min(d[i][j],d[i][k]+d[k][j])
  return d

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

d = [[float('inf') for i in range(N)] for i in range(N)]
for _ in range(M):
  a,b,c = map(int,sys.stdin.readline().split())
  d[a-1][b-1] = c
  d[b-1][a-1] = c

for i in range(N):
  d[i][i] = 0

wf(d)

from collections import deque

ans = float('inf')
for start_i in Rs:
  q = deque()
  q.append([start_i-1,0,set([start_i-1])])
  while len(q) > 0:
    prv_node, prv_cost, done = q.pop()

    min_cost = float('inf')
    min_nodes = []
    for r in Rs:
      r -= 1
      if r in done: continue
      if min_cost > d[prv_node][r]:
        min_cost = d[prv_node][r]
        min_nodes = [r]
      elif min_cost == d[prv_node][r]:
        min_cost = d[prv_node][r]
        min_nodes.append(r)
    
    for node in min_nodes:   
      tmp_done = copy.deepcopy(done)
      tmp_done.add(node)
      if len(tmp_done) == R:
        if ans > prv_cost+min_cost: ans = prv_cost+min_cost
      else: q.append([node, prv_cost+min_cost, tmp_done])

print(ans)

Fixed submission:
import sys, copy
from itertools import permutations

def wf(d):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        d[i][j] = min(d[i][j],d[i][k]+d[k][j])
  return d

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

d = [[float('inf') for i in range(N)] for i in range(N)]
for _ in range(M):
  a,b,c = map(int,sys.stdin.readline().split())
  d[a-1][b-1] = c
  d[b-1][a-1] = c

for i in range(N):
  d[i][i] = 0

wf(d)

ans = float('inf')
for prm in permutations(Rs, R):
  ans = min(ans, sum([d[prm[i-1]-1][prm[i]-1] for i in range(1,R)]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
def wf(d):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        d[i][j] = min(d[i][j],d[i][k]+d[k][j])
  return d

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

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

for i in range(N):
  d[i][i] = 0

wf(d)

from collections import deque

ans = float('inf')
for start_i in Rs:
  q = deque()
  q.append([start_i-1,0,[start_i-1]])
  while len(q) > 0:
    prv_node, prv_cost, done = q.pop()

    min_cost = float('inf')
    min_nodes = []
    for r in Rs:
      r -= 1
      if r in done: continue
      if min_cost > d[prv_node][r]:
        min_cost = d[prv_node][r]
        min_nodes = [r]
      elif min_cost == d[prv_node][r]:
        min_cost = d[prv_node][r]
        min_nodes.append(r)
    
    for node in min_nodes:      
      if len(done)+1 == R:
        if ans > prv_cost+min_cost: ans = prv_cost+min_cost
      else: q.append([node, prv_cost+min_cost, done+[node]])

print(ans)

Fixed submission:
import sys, copy
from itertools import permutations

def wf(d):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        d[i][j] = min(d[i][j],d[i][k]+d[k][j])
  return d

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

d = [[float('inf') for i in range(N)] for i in range(N)]
for _ in range(M):
  a,b,c = map(int,sys.stdin.readline().split())
  d[a-1][b-1] = c
  d[b-1][a-1] = c

for i in range(N):
  d[i][i] = 0

wf(d)

ans = float('inf')
for prm in permutations(Rs, R):
  ans = min(ans, sum([d[prm[i-1]-1][prm[i]-1] for i in range(1,R)]))
print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
from collections import defaultdict, deque
from itertools import permutations
inf = float('inf')

def min_dis_dfs(graph, start_node, end_node):
  stack = deque()
  stack.append([start_node,0])
  done = []
  min_dis = inf
  
  while len(stack) > 0:
    prv_node, prv_dis = stack.pop()
    #print('#'*20)
    #print("prv_node:{}, prv_dis:{}".format(prv_node, prv_dis))
    for node, dis in graph[prv_node]:
      if node not in done:
        if node == end_node and min_dis > prv_dis + dis:min_dis=prv_dis + dis
        else:
          done.append(node)
          stack.append([node,prv_dis + dis])
      #print('node:{}, min_dis:{}, dis:{}'.format(node, min_dis, dis))
  return min_dis
  
n,m,r =map(int,input().split())
rlist = [int(r)-1 for r in input().split()]
graph = defaultdict(list)

for _ in range(m):
  a,b,c = map(int, input().split())
  graph[a-1].append([b-1,c])
  graph[b-1].append([a-1,c])

ans = inf
for r_case in permutations(rlist, r):
  dis = 0
  for i in range(r-1):
    dis += min_dis_dfs(graph, r_case[i], r_case[i+1])
  if ans > dis:ans = dis
print(ans)

Fixed submission:
import sys, copy
from itertools import permutations

def wf(d):
  for k in range(N):
    for i in range(N):
      for j in range(N):
        d[i][j] = min(d[i][j],d[i][k]+d[k][j])
  return d

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

d = [[float('inf') for i in range(N)] for i in range(N)]
for _ in range(M):
  a,b,c = map(int,sys.stdin.readline().split())
  d[a-1][b-1] = c
  d[b-1][a-1] = c

for i in range(N):
  d[i][i] = 0

wf(d)

ans = float('inf')
for prm in permutations(Rs, R):
  ans = min(ans, sum([d[prm[i-1]-1][prm[i]-1] for i in range(1,R)]))
print(ans)