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

def warshall_floyd(d):
  len_d = len(d)
  for k in range(len_d):
    for i in range(len_d):
      for j in range(len_d):
        if d[i][j] < d[i][k] + d[k][j]:
          d[i][j] = d[i][k] + d[k][j]
  return d

def main():
  n, m, r = map(int, input().split())
  list_r = list(map(int, input().split()))
  list_cand = list(itertools.permutations(list_r))

  dist = [[float("inf") if i != j else 0 for j in range(n)] for i in range(n)]
  for i in range(m):
    a, b, c = map(int, input().split())
    dist[a-1][b-1] = c
    dist[b-1][a-1] = c
  min_dist = warshall_floyd(dist)

  result = 10**100
  for one_cand in list_cand:
    temp = 0
    for i in range(r-1)
      temp += min_dist[one_cand[i]-1][one_cand[i+1]-1]
    if temp < result:
      result = temp
  print(result)

if __name__ == "__main__":
  main()

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

def warshall_floyd(d):
  len_d = len(d)
  for k in range(len_d):
    for i in range(len_d):
      for j in range(len_d):
        d[i][j] = min(d[i][j],d[i][k] + d[k][j])
  return d

def main():
  n, m, r = map(int, input().split())
  list_r = list(map(int, input().split()))
  list_cand = list(itertools.permutations(list_r))

  dist = [[float("inf") if i != j else 0 for j in range(n)] for i in range(n)]
  for i in range(m):
    a, b, c = map(int, input().split())
    dist[a-1][b-1] = c
    dist[b-1][a-1] = c
  min_dist = warshall_floyd(dist)

  result = 10**100
  for one_cand in list_cand:
    temp = 0
    idx = 0
    while idx < r-1:
      temp += min_dist[one_cand[idx]-1][one_cand[idx+1]-1]
      idx += 1
    if temp < result:
      result = temp
  print(result)

if __name__ == "__main__":
  main()

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

n, m, r = map(int, input().split())
list_r = [one_r-1 for one_r in map(int, input().split())]

dist = [[float("inf")] * n for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  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]

result = float("inf")
for one_cand in itertools.permutations(list_r):
  temp = 0
  for i in range(r-1):
    temp += dist[one_cand[i]][one_cand[i+1]]
  if temp < result:
    result = temp
print(result)

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
import sys

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)

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
n, m, r = map(int, input().split())
list_r = [one_r-1 for one_r in map(int, input().split())]

dist = [[float("inf")] * n for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  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]

result = float("inf")
for one_cand in permutations(list_r):
  temp = 0
  for i in range(r-1):
    temp += dist[one_cand[i]][one_cand[i+1]]
  if temp < result:
    result = temp
print(result)

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
n, m, r = map(int, input().split())
list_r = [one_r-1 for one_r in map(int, input().split())]

dist = [[float("inf")] * n for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  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]

result = float("inf")
for one_cand in itertools.permutations(list_r):
  temp = 0
  for i in range(r-1):
    temp += dist[one_cand[i]][one_cand[i+1]]
  if temp < result:
    result = temp
print(result)

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

def warshall_floyd(d):
  len_d = len(d)
  for k in range(len_d):
    for i in range(len_d):
      for j in range(len_d):
        d[i][j] = min(d[i][j], d[i][k] + d[k][j])
  return d


n, m, r = map(int, input().split())
list_r = list(map(int, input().split()))
list_r = [one_r-1 for one_r in list_r]
dist = [[float("inf") for _ in range(n)] for _ in range(n)]
for i in range(n):
  dist[i][i] = 0
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  dist[b-1][a-1] = c
min_dist = warshall_floyd(dist)

result = float("inf")
for one_cand in itertools.permutations(list_r):
  temp = 0
  for i in range(r-1):
    temp += min_dist[one_cand[i]][one_cand[i+1]]
  if temp < result:
    result = temp
print(result)

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

def warshall_floyd(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())
list_r = list(map(int, input().split()))
list_cand = itertools.permutations(list_r)
dist = [[float("inf") if i != j else 0 for j in range(n)] for i in range(n)]
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  dist[b-1][a-1] = c
min_dist = warshall_floyd(dist)

result = 10**100
for one_cand in list_cand:
  temp = 0
  for i in range(r-1):
    temp += min_dist[one_cand[i]-1][one_cand[i+1]-1]
  if temp < result:
    result = temp
print(result)

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

def warshall_floyd(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())
list_r = list(map(int, input().split()))
list_cand = list(itertools.permutations(list_r))

dist = [[float("inf") if i != j else 0 for j in range(n)] for i in range(n)]
for i in range(m):
  a, b, c = map(int, input().split())
  dist[a-1][b-1] = c
  dist[b-1][a-1] = c
min_dist = warshall_floyd(dist)

result = 10**100
for one_cand in list_cand:
  temp = 0
  idx = 0
  while idx < r-1:
    temp += min_dist[one_cand[idx]-1][one_cand[idx+1]-1]
    idx += 1
  if temp < result:
    result = temp
print(result)

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()