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


def warshall_floyd(d):
    #d[i][j]: iからjへの最短距離
    n = len(d[0])
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = numpy.minimum(d[i][j], d[i][k] + d[k][j])
    return d


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

INF = float("inf")
route_list = [[INF]*N for _ in range(N)]

for i in range(M):
    a, b, c = [int(x) for x in input().split()]
    route_list[a-1][b-1] = c
    route_list[b-1][a-1] = c

wa_list = warshall_floyd(route_list)
ans = 30**9

for perm in permutations(r):
    # print(perm)
    cur = sum(wa_list[v][u] for v, u in zip(perm, perm[1:]))
    ans = min(ans, cur)

print(int(ans))


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

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


def warshall_floyd(d):
    m = min
    #d[i][j]: iからjへの最短距離
    n = len(d[0])
    for k in range(n):
        for i in range(n):
            for j in range(n):
                d[i][j] = m(d[i][j], d[i][k] + d[k][j])
    return d


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

INF = float("inf")
route_list = [[INF]*N for _ in range(N)]

for i in range(M):
    a, b, c = [int(x) for x in input().split()]
    route_list[a-1][b-1] = c
    route_list[b-1][a-1] = c

wa_list = warshall_floyd(route_list)
ans = 30**9

for perm in permutations(r):
    # print(perm)
    cur = sum(wa_list[v][u] for v, u in zip(perm, perm[1:]))
    ans = min(ans, cur)

print(ans)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

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


def warshall_floyd(d):
    global range, min
    #d[i][j]: iからjへの最短距離
    n = len(d[0])
    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 = [int(x) for x in input().split()]
r = [int(x)-1 for x in input().split()]

INF = float("inf")
route_list = [[INF]*N for _ in range(N)]

for i in range(M):
    a, b, c = [int(x) for x in input().split()]
    route_list[a-1][b-1] = c
    route_list[b-1][a-1] = c

wa_list = warshall_floyd(route_list)
ans = 30**9

for perm in permutations(r):
    # print(perm)
    cur = sum(wa_list[v][u] for v, u in zip(perm, perm[1:]))
    ans = min(ans, cur)

print(ans)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Runtime Error):
import numpy
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
n1 = n + 1
d = numpy.full((n1 * n1), inf)
for i in range(n1):
    d[i, i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a * n1 + b] = d[b, + a] = c
for k, i, j in itertools.product(range(1, n1), repeat=3):
    d[i, j] = d[j, i] = min(
        d[i, j],
        d[i, k] + d[k, j])
s = inf
for q in itertools.permutations(range(r)):
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]], rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
n1 = n + 1
d = [inf] * (n1 * n1)
for i in range(n1):
    d[i * n1 + i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a * n1 + b] = d[b * n1 + a] = c
for k, i, j in itertools.product(range(1, n1), repeat=3):
    d[i * n1 + j] = d[j * n1 + i] = min(
        d[i * n1 + j],
        d[i * n1 + k] + d[k * n1 + j])
s = inf
for q in itertools.permutations(range(r)):
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]] * n1 + rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
n1 = n + 1
d = [inf] * (n1 * n1)
for i in range(n1):
    d[i * n1 + i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a * n1 + b] = d[b * n1 + a] = c
for k in range(1, n+1):
    for i in range(1, n+1):
        d = d
        for j in range(i+1, n+1):
            d[i * n1 + j] = d[j * n1 + i] = min(
                d[i * n1 + j],
                d[i * n1 + k] + d[k * n1 + j])
s = inf
for q in itertools.permutations(range(r)):
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]] * n1 + rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
n1 = n + 1
d = [inf] * (n1 * n1)
for i in range(n1):
    d[i * n1 + i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a * n1 + b] = d[b * n1 + a] = c


def update(i, j, k):
    d[i * n1 + j] = d[j * n1 +
                      i] = min(d[i * n1 + j], d[i * n1 + k] + d[k * n1 + j])


[update(i, j, k) for k in range(1, n+1)
 for i in range(1, n+1) for j in range(i+1, n+1)]
s = inf
for q in itertools.permutations(range(r)):
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]] * n1 + rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
n1 = n + 1
d = [inf] * (n1 * n1)
for i in range(n1):
    d[i * n1 + i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a * n1 + b] = d[b * n1 + a] = c


def update(i, j, k):
    d[i * n1 + j] = d[j * n1 +
                      i] = min(d[i * n1 + j], d[i * n1 + k] + d[k * n1 + j])


[update(i, j, k) for k in range(1, n+1)
 for i in range(1, n+1) for j in range(i+1, n+1)]
p = list(itertools.permutations(range(r)))
s = inf
for q in p:
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]] * n1 + rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)

Buggy python submission (verdict: Time Limit Exceeded):
import sys
import itertools
input = sys.stdin.readline
inf = 200000000
n, m, r = map(int, input().split())
rs = list(map(int, input().split()))
d = [[inf] * (n+1) for j in range(n+1)]
for i in range(n+1):
    d[i][i] = 0
for i in range(m):
    a, b, c = map(int, input().split())
    d[a][b] = d[b][a] = c


def update(i, j, k):
    d[i][j] = d[j][i] = min(d[i][j], d[i][k] + d[k][j])


[update(i, j, k) for k in range(1, n+1)
 for i in range(1, n+1) for j in range(i+1, n+1)]
p = list(itertools.permutations(range(r)))
s = inf
for q in p:
    t = 0
    for i in range(r-1):
        t += d[rs[q[i]]][rs[q[i+1]]]
    if s > t:
        s = t
print(s)


Fixed submission:
from itertools import product, permutations


def warshall_floyd(d):
    # d        : nxn adjacent matrix
    for k, i, j in product(range(n), repeat=3):
        if d[i][j] > d[i][k] + d[k][j]:
            d[i][j] = d[i][k] + d[k][j]


INF = float("inf")
n, m, r = map(int, input().split())
r_list = list(map(lambda x: int(x) - 1, input().split()))
d = [[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, input().split())
    a, b = a - 1, b - 1
    d[a][b] = d[b][a] = c
warshall_floyd(d)

ans = float("inf")
for np in permutations(r_list):
    cand = sum([d[np[i]][np[i + 1]] for i in range(r - 1)])
    ans = min(ans, cand)

print(ans)