Buggy python submission (verdict: Runtime Error):
import sys
input = sys.stdin.readline

import numpy as np

N,M,R = map(int, input().split())
r = list(map(int, input().split()))
ds = np.zeros([N,N])
neighbors = [[]]*N
for _ in range(M):
    a,b,c = map(int, input().split())
    ds[a-1][b-1] = c
    ds[b-1][a-1] = c
    neighbors[a-1].append(b-1)
    neighbors[b-1].append(a-1)
visitedls = []
totalds = 0
minds = 1e9+7

def dfs(a, prev):
    global visitedls, totalds, ds, minds, neighbors, r
    visitedls.append(a+1)
    totalds += ds[a][prev]
    if set(r) & set(visitedls) == set(r):
        if minds > totalds:
                minds = totalds  
    for nxt in neighbors[a]:
        if nxt+1 not in visitedls:
            dfs(nxt, a)
    del visitedls[-1]
    totalds -= ds[a][prev]

for a in r:
    dfs(a-1, a-1)
print(int(minds))
    

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

adj = [[] for i in range(N)]
for i in range(M):
    A, B, C = map(int, input().split())
    adj[-A].append((B, C))
    adj[-B].append((A, C))

import heapq

def Dijkstra(N, v_st, v_go):
    cost = [float('inf') for i in range(N)]

    pq = []
    heapq.heappush(pq, (0, v_st))

    while pq:
        c, v_now = heapq.heappop(pq)

        if v_now == v_go:
            return c

        if cost[-v_now] < c:
            continue

        cost[-v_now] = c

        for v, c2 in adj[-v_now]:
            if cost[-v_now] + c2 < cost[-v]:
                cost[-v] = cost[-v_now] + c2
                heapq.heappush(pq, (cost[-v], v))

cost_min = [[float('inf') for i in range(R)] for j in range(R)]

for i in range(R):
    for j in range(i, R):
        cost_min[i][j] = Dijkstra(N, r[i], r[j])
        cost_min[j][i] = cost_min[i][j]

import itertools
p = list(itertools.permutations(range(R)))

ans = float('inf')
for pp in p:
    c = 0
    for i in range(R - 1):
        c += cost_min[pp[i]][pp[i + 1]]

    ans = min(ans, c)
    
print(ans)

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

import numpy as np

N,M,R = map(int, input().split())
r = list(map(int, input().split()))
ds = np.zeros([N,N])
neighbors = [[]]*N
for _ in range(M):
    a,b,c = map(int, input().split())
    ds[a-1][b-1] = c
    ds[b-1][a-1] = c
    neighbors[a-1].append(b-1)
    neighbors[b-1].append(a-1)
visitedls = []
totalds = 0
minds = 1e9+7

def dfs(a, prev):
    global visitedls, totalds, ds, minds, neighbors, r
    visitedls.append(a+1)
    totalds += ds[a][prev]
    if set(r) & set(visitedls) == set(r):
        if minds > totalds:
                minds = totalds  
    for nxt in neighbors[a]:
        if nxt+1 not in visitedls:
            dfs(nxt, a)
    del visitedls[-1]
    totalds -= ds[a][prev]

for a in r:
    dfs(a-1, a-1)
print(int(minds))
    

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

adj = [[] for i in range(N)]
for i in range(M):
    A, B, C = map(int, input().split())
    adj[-A].append((B, C))
    adj[-B].append((A, C))

import heapq

def Dijkstra(N, v_st, v_go):
    cost = [float('inf') for i in range(N)]

    pq = []
    heapq.heappush(pq, (0, v_st))

    while pq:
        c, v_now = heapq.heappop(pq)

        if v_now == v_go:
            return c

        if cost[-v_now] < c:
            continue

        cost[-v_now] = c

        for v, c2 in adj[-v_now]:
            if cost[-v_now] + c2 < cost[-v]:
                cost[-v] = cost[-v_now] + c2
                heapq.heappush(pq, (cost[-v], v))

cost_min = [[float('inf') for i in range(R)] for j in range(R)]

for i in range(R):
    for j in range(i, R):
        cost_min[i][j] = Dijkstra(N, r[i], r[j])
        cost_min[j][i] = cost_min[i][j]

import itertools
p = list(itertools.permutations(range(R)))

ans = float('inf')
for pp in p:
    c = 0
    for i in range(R - 1):
        c += cost_min[pp[i]][pp[i + 1]]

    ans = min(ans, c)
    
print(ans)

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

import numpy as np

N,M,R = map(int, input().split())
r = list(map(int, input().split()))
ds = np.zeros([N,N])
neighbors = [[]]*N
for _ in range(M):
    a,b,c = map(int, input().split())
    ds[a-1][b-1] = c
    ds[b-1][a-1] = c
    neighbors[a-1].append(b-1)
    neighbors[b-1].append(a-1)
visitedls = []
totalds = 0
minds = 1e9+7

def dfs(a, prev):
    global visitedls, totalds, ds, minds, neighbors, r
    visitedls.append(a+1)
    totalds += ds[a][prev]
    if set(r) & set(visitedls) == set(r):
        if minds > totalds:
                minds = totalds  
    for nxt in neighbors[a]:
        if nxt+1 not in visitedls:
            dfs(nxt, a)
    del visitedls[-1]
    totalds -= ds[a][prev]

def main():
    for a in r:
        dfs(a-1, a-1)
    print(int(minds))
    

if __name__ == "__main__":
    main()

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

adj = [[] for i in range(N)]
for i in range(M):
    A, B, C = map(int, input().split())
    adj[-A].append((B, C))
    adj[-B].append((A, C))

import heapq

def Dijkstra(N, v_st, v_go):
    cost = [float('inf') for i in range(N)]

    pq = []
    heapq.heappush(pq, (0, v_st))

    while pq:
        c, v_now = heapq.heappop(pq)

        if v_now == v_go:
            return c

        if cost[-v_now] < c:
            continue

        cost[-v_now] = c

        for v, c2 in adj[-v_now]:
            if cost[-v_now] + c2 < cost[-v]:
                cost[-v] = cost[-v_now] + c2
                heapq.heappush(pq, (cost[-v], v))

cost_min = [[float('inf') for i in range(R)] for j in range(R)]

for i in range(R):
    for j in range(i, R):
        cost_min[i][j] = Dijkstra(N, r[i], r[j])
        cost_min[j][i] = cost_min[i][j]

import itertools
p = list(itertools.permutations(range(R)))

ans = float('inf')
for pp in p:
    c = 0
    for i in range(R - 1):
        c += cost_min[pp[i]][pp[i + 1]]

    ans = min(ans, c)
    
print(ans)