Buggy python submission (verdict: Runtime Error):
from itertools import permutations

inf = float("inf")
n,m,r = map(int,raw_input().split())
arr = map(int,raw_input().split())
x = range(n)
d = [[inf for i in x] for j in x]

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

for i in x:
	f,t,c = map(int,raw_input().split())
	d[f-1][t-1] = c
	d[t-1][f-1] = c



for i in x:
	for j in x:
		for k in x:
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])


ans = inf
for i in permutations(arr):
	distance = 0
	for j in range(r-1):
		distance += d[i[j]-1][i[j+1]-1]

	ans = min(ans,distance)

print ans

Fixed submission:
from itertools import permutations
import sys

def main():
    
    N, M, R = map(int, sys.stdin.readline().split())
    r = [i - 1 for i in map(int, sys.stdin.readline().split())]
    
    d = [[float('inf')] * N for _ in range(N)]
    
    for i in range(N):
        d[i][i] = 0
        
    for i 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 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]
    
    ans = 10**12
    
    for each in permutations(r):
        t_dis = 0
        for i in range(R-1):
            t_dis += d[each[i]][each[i+1]]  
        
        ans = min(ans, t_dis)
    
    print(ans)

if __name__ == '__main__':
    main()

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

inf = float("inf")
n,m,r = map(int,raw_input().split())
arr = map(int,raw_input().split())
d = [[inf for i in range(n)] for j in range(n)]

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

for i in range(m):
	f,t,c = map(int,raw_input().split())
	d[f-1][t-1] = c
	d[t-1][f-1] = c

for i in range(n):
	for j in range(n):
		for k in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])


ans = inf
for i in permutations(arr):
	distance = 0
	for j in range(r-1):
		distance += d[i[j]-1][i[j+1]-1]

	ans = min(ans,distance)

print ans

Fixed submission:
from itertools import permutations
import sys

def main():
    
    N, M, R = map(int, sys.stdin.readline().split())
    r = [i - 1 for i in map(int, sys.stdin.readline().split())]
    
    d = [[float('inf')] * N for _ in range(N)]
    
    for i in range(N):
        d[i][i] = 0
        
    for i 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 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]
    
    ans = 10**12
    
    for each in permutations(r):
        t_dis = 0
        for i in range(R-1):
            t_dis += d[each[i]][each[i+1]]  
        
        ans = min(ans, t_dis)
    
    print(ans)

if __name__ == '__main__':
    main()

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

inf = float("inf")
n,m,r = map(int,raw_input().split())
arr = map(int,raw_input().split())
d = [[inf for i in range(n)] for j in range(n)]

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

for i in range(m):
	f,t,c = map(int,raw_input().split())
	d[f-1][t-1] = c
	d[t-1][f-1] = c

for i in range(n):
	for j in range(n):
		for k in range(n):
			d[i][j] = min(d[i][j], d[i][k] + d[k][j])


per = list(itertools.permutations(range(r)))

ans = inf
for i in per:
	distance = 0
	for j in range(1,r):
		distance += d[arr[i[j-1]]-1][arr[i[j]]-1]

	ans = min(ans,distance)

print ans

Fixed submission:
from itertools import permutations
import sys

def main():
    
    N, M, R = map(int, sys.stdin.readline().split())
    r = [i - 1 for i in map(int, sys.stdin.readline().split())]
    
    d = [[float('inf')] * N for _ in range(N)]
    
    for i in range(N):
        d[i][i] = 0
        
    for i 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 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]
    
    ans = 10**12
    
    for each in permutations(r):
        t_dis = 0
        for i in range(R-1):
            t_dis += d[each[i]][each[i+1]]  
        
        ans = min(ans, t_dis)
    
    print(ans)

if __name__ == '__main__':
    main()