Problem:
 Score : 400 points    Problem Statement There are N towns in the State of Atcoder, connected by M bidirectional roads.  The i -th road connects Town A_i and B_i and has a length of C_i .  Joisino is visiting R towns in the state, r_1,r_2,..,r_R (not necessarily in this order).  She will fly to the first town she visits, and fly back from the last town she visits, but for the rest of the trip she will have to travel by road.  If she visits the towns in the order that minimizes the distance traveled by road, what will that distance be?      Constraints  2≤N≤200  1≤M≤N×(N-1)/2  2≤R≤min(8,N) ( min(8,N) is the smaller of 8 and N .)  r_i≠r_j (i≠j)  1≤A_i,B_i≤N, A_i≠B_i  (A_i,B_i)≠(A_j,B_j),(A_i,B_i)≠(B_j,A_j) (i≠j)  1≤C_i≤100000  Every town can be reached from every town by road.  All input values are integers.         Input Input is given from Standard Input in the following format:  N  M  R  r_1  ...  r_R  A_1  B_1  C_1  :  A_M  B_M  C_M       Output Print the distance traveled by road if Joisino visits the towns in the order that minimizes it.        Sample Input 1 3 3 3
1 2 3
1 2 1
2 3 1
3 1 4      Sample Output 1 2  For example, if she visits the towns in the order of 1 , 2 , 3 , the distance traveled will be 2 , which is the minimum possible.       Sample Input 2 3 3 2
1 3
2 3 2
1 3 6
1 2 2      Sample Output 2 4  The shortest distance between Towns 1 and 3 is 4 . Thus, whether she visits Town 1 or 3 first, the distance traveled will be 4 .       Sample Input 3 4 6 3
2 3 4
1 2 4
2 3 3
4 3 1
1 4 1
4 2 2
3 1 6      Sample Output 3 3

Solution (Python):
import sys
from itertools import permutations

sys.setrecursionlimit(10 ** 6)
INF = float("inf")
MOD = 10 ** 9 + 7


def input():
    return sys.stdin.readline().strip()


def main():
    N, M, R = map(int, input().split())
    r = list(map(int, input().split()))
    P = permutations(r)

    dp = [[INF] * N for _ in range(N)]
    for i in range(N):
        dp[i][i] = 0

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

    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                tmp = dp[i][k] + dp[k][j]
                if tmp < dp[i][j]:
                    dp[i][j] = tmp

    ans = INF
    for p in P:
        tmp = 0
        for i in range(R - 1):
            tmp += dp[p[i] - 1][p[i + 1] - 1]

        if tmp < ans:
            ans = tmp

    print(ans)


if __name__ == "__main__":
    main()

Solution (Python):
import collections

class WarshallFloyd():
    def __init__(self,N):  
        self.N = N
        self.d = [[float('inf')]*N for i in range(N)]  
    def add(self,u,v,c,directed = False):
        if directed is False:  
            self.d[u][v] = c
            self.d[v][u] = c
        else:  
            self.d[u][v] = c
    def WarshallFloyd_search(self):
        for k in range(self.N):
            for i in range(self.N):
                for j in range(self.N):
                    self.d[i][j] = min(self.d[i][j],self.d[i][k] + self.d[k][j])
        for i in range(self.N):
            self.d[i][i] = 0
        return self.d

N,M,R = map(int,input().split())
r = list(map(int,input().split()))
data = [list(map(int,input().split())) for i in range(M)]

G = WarshallFloyd(N)

for u,v,c in data:
    G.add(u-1,v-1,c,directed=False)

d = G.WarshallFloyd_search()

A = [i for i in range(R)]

ans = float('inf')

from itertools import permutations

for i in permutations(A,R):
    a = 0
    for j in range(R-1):
        a += d[r[i[j]]-1][r[i[j+1]]-1]
    ans = min(ans,a)

print(ans)

Solution (Python):
from itertools import permutations


def main():
    towns, roads, visit = map(int, input().split())
    visit_order = [int(x) - 1 for x in input().split()]
    dist = [[float("inf") for _ in range(towns)] for _ in range(towns)]
    for _ in range(roads):
        a, b, d = map(int, input().split())
        a -= 1
        b -= 1
        dist[a][b] = d
        dist[b][a] = d
    for k in range(towns):
        for i in range(towns):
            for j in range(towns):
                dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])
    answer = float("inf")
    for p in permutations(visit_order):
        now = 0
        for i in range(visit - 1):
            now += dist[p[i]][p[i + 1]]
        answer = min(answer, now)
    print(answer)


if __name__ == '__main__':
    main()

Solution (Python):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from itertools import permutations

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

g = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    g[A][B] = C
    g[B][A] = C
g = floyd_warshall(csgraph_from_dense(g))

result = 1000000000
for p in permutations(r):
    t = 0
    for i in range(R - 1):
        t += g[p[i]][p[i + 1]]
    result = min(result, t)
print(int(result))

Solution (Python):
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
from itertools import permutations

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

d = [[0] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    d[A][B] = C
    d[B][A] = C
g = csgraph_from_dense(d)
g = floyd_warshall(g)

result = 1000000000
for p in permutations(r):
    t = 0
    for i in range(R - 1):
        t += g[p[i]][p[i + 1]]
    result = min(result, t)
print(int(result))

Solution (Python):
from itertools import permutations


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


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

d = [[1000000000] * (N + 1) for _ in range(N + 1)]
for _ in range(M):
    A, B, C = map(int, input().split())
    d[A][B] = C
    d[B][A] = C
warshall_floyd(N + 1, d)

result = 1000000000
for p in permutations(r):
    t = 0
    for i in range(R - 1):
        t += d[p[i]][p[i + 1]]
    result = min(result, t)
print(result)

Solution (Python):
import itertools


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

INF = float("inf")
es = [[INF for _ in range(N)]  for _ in range(N)]
for i in range(N):
  es[i][i] = 0

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



for k in range(N):
  for i in range(N):
    for j in range(N):
      es[i][j] = min(es[i][j], es[i][k] + es[k][j])

ans = INF
for route in itertools.permutations(rs):
  tmp = 0
  for a,b in zip(route, route[1:]):
    tmp += es[a-1][b-1]
  ans = min(ans, tmp)

print(ans)

Solution (Python):
import itertools
import sys

def main():
  input = sys.stdin.readline
  n, m, r = map(int, input().split())
  R = [int(x) for x in input().split()]
  inf = pow(10, 9)+7

  roads = [[inf]*n for _ in range(n)]
  for i in range(m):
    a, b, c = map(int, input().split())
    roads[a-1][b-1] = c
    roads[b-1][a-1] = c

  for k in range(n):
    for i in range(n):
      for j in range(n):
        if roads[i][j] > roads[i][k]+roads[k][j]:
          roads[i][j] = roads[i][k]+roads[k][j]

  ans = inf
  for value in itertools.permutations(R):
    sub = 0
    for k in range(r-1):
      sub += roads[value[k]-1][value[k+1]-1]
    if ans > sub:
      ans = sub

  print(ans)

if __name__ == "__main__":
  main()

Solution (Python):
import itertools
n, m, r = map(int, input().split())
R = [int(x) for x in input().split()]
inf = pow(10, 9)+7

roads = [[inf]*n for _ in range(n)]
for i in range(m):
  a, b, c = map(int, input().split())
  roads[a-1][b-1] = c
  roads[b-1][a-1] = c

for k in range(n):
  for i in range(n):
    for j in range(n):
      if roads[i][j] > roads[i][k]+roads[k][j]:
        roads[i][j] = roads[i][k]+roads[k][j]

ans = inf
for value in itertools.permutations(R):
  sub = 0
  for k in range(r-1):
    sub += roads[value[k]-1][value[k+1]-1]
  if ans > sub:
    ans = sub

print(ans)

Solution (Python):
from itertools import permutations
n,m,r = map(int,input().split())
rl = list(map(int,input().split()))
d = [[10**8]*n for i in range(n)]
for i in range(m):
  a,b,c = map(int,input().split())
  d[a-1][b-1] = d[b-1][a-1] = c
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])
m = 10**9
for i in permutations(rl):
  c = 0
  for j in range(1,r):
    c += d[i[j-1]-1][i[j]-1]
  m = min(m,c)
print(m)

Solution (Python):
import itertools
N,M,R=map(int, input().split())
r=list(map(int, input().split()))
G = [[float("inf")]*N for i in range(N)] 
for i in range(M):
    a,b,c=map(int, input().split())
    G[a-1][b-1]=c
    G[b-1][a-1]=c

def warshall_floyd(G):
    
    for k in range(N):
        for i in range(N):
            for j in range(N):
                G[i][j] = min(G[i][j],G[i][k] + G[k][j])
    return G
G=warshall_floyd(G)
ans=10**8
for L in itertools.permutations(r):
    L=list(L)
    sub=0
    for i in range(1,R):
        s=L[i-1]
        t=L[i]
        sub+=G[s-1][t-1]
    ans=min(ans, sub)
print(ans)

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
from heapq import *

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**8
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,M,R = LI()
    r = _LI()
    root = [[] for _ in range(N)]
    for _ in range(M):
        a,b,c = LI()
        root[a-1].append((b-1,c))
        root[b-1].append((a-1,c))

    memo = [[0] * N for _ in range(N)]

    min_r = INF
    for x in itertools.permutations(r):
        it = 0
        s = x[0]
        for t in x[1:]:
            if memo[s][t]:
                it += memo[s][t]
            else:
                node = [INF] * N
                q = []
                heapify(q)
                heappush(q,(0,s))
                node[s] = 0
                while q:
                    l,u = heappop(q)
                    if l > node[u]: continue
                    for v,m in root[u]:
                        if node[v] > l + m:
                            node[v] = l + m
                            heappush(q,(l+m,v))
                memo[s][t] = node[t]
                memo[t][s] = node[t]
                it += node[t]
            s = t
        min_r = min(min_r,it)
    print(min_r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
from heapq import *

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**8
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,M,R = LI()
    r = _LI()
    root = [[] for _ in range(N)]
    for _ in range(M):
        a,b,c = LI()
        root[a-1].append((b-1,c))
        root[b-1].append((a-1,c))

    memo = [[0] * N for _ in range(N)]

    min_r = INF
    for x in itertools.permutations(r):
        it = 0
        s = x[0]
        for t in x[1:]:
            if memo[s][t]:
                it += memo[s][t]
            else:
                node = [INF] * N
                q = []
                heapify(q)
                heappush(q,(0,s))
                while q:
                    l,u = heappop(q)
                    if l >= node[u]: continue
                    node[u] = l
                    for v,m in root[u]:
                        if node[v] > l + m:
                            heappush(q,(l+m,v))
                memo[s][t] = node[t]
                memo[t][s] = node[t]
                it += node[t]
            s = t
        min_r = min(min_r,it)
    print(min_r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections
from heapq import *

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**8
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,M,R = LI()
    r = _LI()
    root = [[] for _ in range(N)]
    for _ in range(M):
        a,b,c = LI()
        root[a-1].append((b-1,c))
        root[b-1].append((a-1,c))

    memo = [[0] * N for _ in range(N)]

    min_r = INF
    for x in itertools.permutations(r):
        it = 0
        s = x[0]
        for t in x[1:]:
            if memo[s][t]:
                it += memo[s][t]
            else:
                node = [INF] * N
                q = []
                heapify(q)
                heappush(q,(0,s))
                node[s] = 0
                while q:
                    l,u = heappop(q)
                    for v,m in root[u]:
                        if node[v] > l + m:
                            node[v] = l + m
                            heappush(q,(l+m,v))
                memo[s][t] = node[t]
                memo[t][s] = node[t]
                it += node[t]
            s = t
        min_r = min(min_r,it)
    print(min_r)

if __name__ == '__main__':
    main()

Solution (Python):
import sys,queue,math,copy,itertools,bisect,collections,heapq

def main():
    sys.setrecursionlimit(10**7)
    INF = 10**8
    MOD = 10**9 + 7
    LI = lambda : [int(x) for x in sys.stdin.readline().split()]
    _LI = lambda : [int(x)-1 for x in sys.stdin.readline().split()]
    NI = lambda : int(sys.stdin.readline())

    N,M,R = LI()
    r = _LI()
    root = [[] for _ in range(N)]
    for _ in range(M):
        a,b,c = LI()
        root[a-1].append((b-1,c))
        root[b-1].append((a-1,c))

    memo = [[0] * N for _ in range(N)]

    min_r = INF
    for x in itertools.permutations(r):
        it = 0
        s = x[0]
        for t in x[1:]:
            if memo[s][t]:
                it += memo[s][t]
            else:
                node = [INF] * N
                q = collections.deque()
                q.append((s,0))
                node[s] = 0
                while q:
                    u,l = q.popleft()
                    for v,m in root[u]:
                        if node[v] > l + m:
                            node[v] = l + m
                            q.append((v,l+m))
                memo[s][t] = node[t]
                memo[t][s] = node[t]
                it += node[t]
            s = t
        min_r = min(min_r,it)
    print(min_r)

if __name__ == '__main__':
    main()

Solution (Python):

def warshall_floyd(dp):
    for k in range(n):
        for i in range(n):
            for j in range(n):
                dp[i][j] = min(dp[i][j] , dp[i][k] + dp[k][j])
    return dp

n, m,R = map(int, input().split())
r = list( map(int, input().split()))

d = [[float("inf")]*n for i in range(n)]

abc = [tuple(map(int,input().split())) for i in range(m)]
for a,b,c in abc:
    a,b = a-1,b-1
    d[a][b] = c
    d[b][a] = c

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

warshall_floyd(d)

import itertools

p = itertools.permutations(r, R)

INF = float('inf')
ans = INF
for path in p:
    tmp = 0
    for i in range(R-1):
        tmp += d[path[i]-1][path[i+1]-1]
    ans = min(ans,tmp)

print(ans)

Solution (Python):
def main():
    from itertools import permutations, tee

    INF = 1 << 30

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

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

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

    ans = INF
    for perm in permutations(r):
        perm_iter = iter(perm)
        frm_iter, to_iter = tee(perm_iter, 2)
        next(to_iter)
        t = 0
        for frm, to in zip(frm_iter, to_iter):
            t += d[frm][to]
        ans = min(ans, t)
    print(ans)


if __name__ == '__main__':
    main()

Solution (Python):
from itertools import permutations


n, m, r = map(int, input().split())
towns = list(map(int, input().split()))

INF = float('inf')

graph = [[INF for _ in range(n)] for _ in range(n)]

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



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

min_dist = 10**10
for route in permutations(towns, r):
    dist = 0
    for i in range(r-1):
        a = route[i] - 1
        b = route[i+1] - 1
        dist += graph[a][b]
    if dist < min_dist:
        min_dist = dist

print(min_dist)

Solution (Python):
from itertools import permutations
INF = float("inf")

n, m, r = map(int, input().split())
r = list(map(lambda x: int(x)-1, input().split()))
g = [[INF] * n for _ in range(n)]
abc = [tuple(map(int, input().split())) for _ in range(m)]
for a, b, c in abc:
    a -= 1; b -= 1
    g[a][b] = c
    g[b][a] = c

def warshall_floyd(d, n):
    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

d = warshall_floyd(g, n)
ans = INF
for route in permutations(r):
    res = 0
    for i in range(1, len(r)):
        res += d[route[i-1]][route[i]]
    ans = min(ans, res)

print(ans)

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
import numpy as np
from itertools import permutations, combinations
n,m,R = map(int,input().split())
r=list(map(int,input().split()))
for i in range(R):
    r[i]-=1
d = np.zeros((n,n)) 

for i in range(m):
    a,b,c = map(int,input().split())
    a,b = a-1, b-1
    d[a,b] = c
dist =floyd_warshall(d,directed=0).astype(int)
ans=10**10
for v in permutations(r):
    tmp=0
    for i in range(R-1):
        tmp+=dist[v[i],v[i+1]]
    ans=min(ans,tmp)
print(ans)

Solution (Python):
INF = 10**10
n,m,R = map(int,input().split())
r=list(map(int,input().split()))
for i in range(R):
    r[i]-=1
d = [[INF]*n for i in range(n)] 

for i in range(m):
    a,b,c = map(int,input().split())
    a,b = a-1, b-1
    d[a][b] = c
    d[b][a] = c 
for i in range(n):
    d[i][i] = 0

def FW():
    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])
FW()
from itertools import permutations, combinations
ans=INF
for v in permutations(r):
    tmp=0
    for i in range(R-1):
        tmp+=d[v[i]][v[i+1]]
    ans=min(ans,tmp)
print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10**7)
from collections import Counter, deque
from collections import defaultdict
from itertools import combinations, permutations, accumulate, groupby, product
from bisect import bisect_left,bisect_right
from heapq import heapify, heappop, heappush
from math import floor, ceil
from operator import itemgetter
def I(): return int(input())
def MI(): return map(int, input().split())
def LI(): return list(map(int, input().split()))
def LI2(): return [int(input()) for i in range(n)]
def MXI(): return [[LI()]for i in range(n)]
def printns(x): print('\n'.join(x))
def printni(x): print('\n'.join(list(map(str,x))))
inf = 10**17
mod = 10**9 + 7


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,w,r = map(int,input().split())
lis=LI()
d = [[float("inf")]*n for i in range(n)]

for i in range(w):
    x,y,z = map(int,input().split())
    x-=1
    y-=1
    d[x][y] = z
    d[y][x] = z
for i in range(n):
    d[i][i] = 0 


d=warshall_floyd(d)

z=permutations(lis,r)
mn=inf
for s in z:
    u=list(s)
    dis=0
    for i in range(r-1):
        dis+=d[u[i]-1][u[i+1]-1]
    
    if dis<mn:
        mn=dis
print(mn)

Solution (Python):
from collections import deque
from heapq import heapify,heappop,heappush,heappushpop
from copy import copy,deepcopy
from itertools import product,permutations,combinations,combinations_with_replacement
from collections import defaultdict,Counter
from bisect import bisect_left,bisect_right


from functools import reduce
from pprint import pprint

INF = float("inf")

def myinput():
    return map(int,input().split())

def mylistinput(n):
    return [ list(myinput()) for _ in range(n) ]

def mycol(data,col):
    return [ row[col] for row in data ]

def mysort(data,col,reverse_flag):
    data.sort(key=lambda x:x[col],reverse=reverse_flag)
    return data

def mymax(data):
    M = -1*float("inf")
    for i in range(len(data)):
        m = max(data[i])
        M = max(M,m)
    return M

def mymin(data):
    m = float("inf")
    for i in range(len(data)):
        M = min(data[i])
        m = min(m,M)
    return m

def mycount(ls,x):
    
    l = bisect_left(ls,x)
    r = bisect_right(ls,x)
    return (r-l)

def myoutput(ls,space=True):
    if space:
        if len(ls)==0:
            print(" ")
        elif type(ls[0])==str:
            print(" ".join(ls))
        elif type(ls[0])==int:
            print(" ".join(map(str,ls)))
        else:
            print("Output Error")
    else:
        if len(ls)==0:
            print("")
        elif type(ls[0])==str:
            print("".join(ls))
        elif type(ls[0])==int:
            print("".join(map(str,ls)))
        else:
            print("Output Error")

n,m,r = myinput()
R = list(myinput())
ABC = mylistinput(m)

ans = INF

d = [ [INF]*n for _ in range(n) ]



def warshall_floyd(n,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

for i in range(m):
    a = ABC[i][0] - 1
    b = ABC[i][1] - 1
    c = ABC[i][2]
    d[a][b] = c
    d[b][a] = c

d = warshall_floyd(n,d)

ls = list(permutations(R))


for i in range(len(ls)):
    l = ls[i]
    cost = 0
    for j in range(r-1):
        s = l[j] - 1
        t = l[j+1] - 1
        cost += d[s][t]
    ans = min( ans, cost )
print(ans)

Solution (Python):
import sys
from itertools import permutations

sys.setrecursionlimit(10 ** 7)
f_inf = float('inf')
mod = 10 ** 9 + 7


def resolve():
    n, m, r = map(int, input().split())
    R = list(map(int, input().split()))
    C = [[f_inf for _ in range(n)] for _ in range(n)]
    for i in range(n):
        C[i][i] = 0

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

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

    res = f_inf
    for pattern in permutations(R):
        cost = 0
        for k in range(r - 1):
            i, j = pattern[k], pattern[k + 1]
            cost += C[i - 1][j - 1]
        res = min(res, cost)

    print(res)


if __name__ == '__main__':
    resolve()

Solution (Python):
N, M, R = map(int, input().split())
r = list(map(int, input().split()))


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

for i in range(N):
    d[i][i] = 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])

from itertools import groupby, accumulate, product, permutations, combinations
def solve(r,d):
    ans = float('inf')
    for p in permutations(r,R):
        dist = 0
        for i in range(R-1):
            dist += d[p[i]-1][p[i+1]-1]
        ans = min(ans,dist)
    return ans
print(solve(r,d))

Solution (Python):
N,M,R = map(int,input().split())
r = list(map(int,input().split()))
INF = 10**18
d = [[INF for i in range(N)] for j in range(N)]
for i 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


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

import itertools
ans = INF
for v in itertools.permutations(r,R):
    anss = 0
    for i in range(R-1):
        anss += d[v[i]-1][v[i+1]-1]
    ans = min(ans,anss)
print(ans)

Solution (Python):
import itertools
inf=10**8
n,m,R=map(int,input().split())
r=list(map(int,input().split()))
d=[[inf]*n for i in range(n)]
for i in range(n):
    d[i][i]=0
ans=inf
for i in range(m):
    a,b,c=map(int,input().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):
            d[i][j]=min(d[i][j],d[i][k]+d[k][j])
for i in itertools.permutations(r):
    preans=0
    for j in range(R-1):
        preans+=d[i[j]-1][i[j+1]-1]
    ans=min(ans,preans)
print(ans)

Solution (Python):
from collections import Counter,defaultdict,deque
from heapq import heappop,heappush,heapify
import sys,bisect,math,itertools,fractions,pprint
sys.setrecursionlimit(10**8)
mod = 10**9+7
INF = float('inf')
def inp(): return int(sys.stdin.readline())
def inpl(): return list(map(int, sys.stdin.readline().split()))

n,m,r = inpl()
rr = inpl()
g = [[INF]*n for _ in range(n)]
for i in range(n):
    g[i][i] = 0
for i in range(m):
    a,b,c = inpl()
    a,b = a-1,b-1
    g[a][b] = c
    g[b][a] = c

for k in range(n):
    for i in range(n):
        for j in range(n):
            g[i][j] = min(g[i][j], g[i][k]+g[k][j])
res = INF
for root in itertools.permutations(range(r)):
    cnt = 0
    for i in range(r-1):
        a,b = rr[root[i]], rr[root[i+1]]
        a,b = a-1, b-1
        cnt += g[a][b]
    res = min(res, cnt)
print(res)

Solution (Python):
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines
sys.setrecursionlimit(10 ** 7)

from scipy.sparse import csr_matrix
from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations

n, m, r = map(int, readline().split())
rr = list(map(int, readline().split()))
graph = [[0] * n for _ in range(n)]
for i in range(m):
    a, b, c = map(int, readline().split())
    graph[a - 1][b - 1] = c
    graph[b - 1][a - 1] = c
cost = floyd_warshall(csr_matrix(graph))
ans = float('inf')
for bit in permutations(rr):
    cnt = 0
    for now, next in zip(bit, bit[1:]):
        cnt += cost[now - 1][next - 1]
    ans = min(cnt, ans)
print(int(ans))

Solution (Python):
from scipy.sparse.csgraph import floyd_warshall
from itertools import permutations
from sys import stdin
nii=lambda:map(int,stdin.readline().split())

n,m,r=nii()
r=list(nii())

s=[[float('inf')]*n for i in range(n)]
for i in range(n):
  for j in range(n):
    s[i][j]=0
for i in range(m):
  a,b,c=nii()
  a-=1
  b-=1
  s[a][b]=c
  s[b][a]=c

ws=floyd_warshall(s)

ans=10**9
for i in permutations(r):
  t_ans=0
  for j in range(1,len(i)):
    t_ans+=ws[i[j-1]-1][i[j]-1]
  ans=min(ans,t_ans)
print(int(ans))

Solution (Python):
from itertools import permutations
import numpy as np
from scipy.sparse.csgraph import dijkstra
from scipy.sparse import csr_matrix

N, M, R = map(int, input().split())
R = list(map(int, input().split()))
A, B, C = np.array([input().split() for _ in range(M)], dtype=int).T
G = csr_matrix((C, (A - 1, B - 1)), shape=(N, N))
path = dijkstra(G, directed=False)
path = (path + 0.5).astype(int)

ans = 10 ** 18
for p in permutations(R):
    cnt = 0
    for x, y in zip(p[:-1], p[1:]):
        x -= 1
        y -= 1
        cnt += path[x][y]
    ans = min(ans, cnt)

print(ans)

Solution (Python):
import itertools

def main():
    n, m, q = map(int, input().split())
    r = list(map(int, input().split()))
    dist = [[10**18 for _ in range(n)] for _ 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

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

    ans = 10 ** 18
    for p in itertools.permutations(r):
        total = 0
        for i in range(q-1):
            total += dist[p[i]-1][p[i+1]-1]
        ans = min(ans, total)

    print(ans)

if __name__ == '__main__':
    main()

Solution (Python):
import sys
input = sys.stdin.readline
sys.setrecursionlimit(10 ** 9)
MOD = 10 ** 9 + 7

from itertools import permutations

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

INF = 10 ** 9

G = [[INF] * (N + 1) for _ in range(N + 1)]
for i in range(N + 1):
    G[i][i] = 0

for _ in range(M):
    A, B, C = map(int, input().split())
    G[A][B] = C
    G[B][A] = C

for k in range(N + 1):
    for i in range(N + 1):
        for j in range(N + 1):
            G[i][j] = min(G[i][j], G[i][k] + G[k][j])

ans = INF
for tmp in permutations(R):
    score = 0
    for i in range(r - 1):
        score += G[tmp[i]][tmp[i + 1]]
    ans = min(ans, score)

print (ans)

Solution (Python):
from itertools import permutations

N, M, R = map(int, input().split())
R = list(map(lambda a: int(a) - 1, input().split()))

minDist = [[10**18] * N for _ in range(N)]
for _ in range(M):
    fr, to, cost = map(int, input().split())
    fr -= 1
    to -= 1
    minDist[fr][to] = cost
    minDist[to][fr] = cost
for i in range(N):
    minDist[i][i] = 0

for k in range(N):
    for i in range(N):
        for j in range(N):
            d = minDist[i][k] + minDist[k][j]
            if minDist[i][j] > d:
                minDist[i][j] = d

ans = 10**18
for P in permutations(R, r=len(R)):
    cost = 0
    for fr, to in zip(P, P[1:]):
        cost += minDist[fr][to]
    ans = min(ans, cost)
print(ans)

Solution (Python):
from itertools import permutations
from scipy.sparse.csgraph import csgraph_from_dense, floyd_warshall
inf = 10 ** 10
n, m, r = map(int,input().split())
R = list(map(int,input().split()))

edge = [[inf] * n for i in range(n)]
for i in range(m):
    a, b, c = map(int,input().split())
    edge[a - 1][b - 1] = c

G = csgraph_from_dense(edge, null_value = inf)
d = floyd_warshall(G, False)

rec = inf
for i in permutations(R, r):
    temp = 0
    
    for j in range(r):
        if j == r - 1:continue
        temp += d[i[j] - 1][i[j + 1] - 1]
    rec = min(rec, temp)

print(int(rec))

Solution (Python):
import heapq
import itertools

def dijkstra(s):
    inf = pow(10, 10)
    dist = [inf] * (n + 1)
    dist[s] = 0
    c = [0] * (n + 1)
    p = []
    heapq.heapify(p)
    heapq.heappush(p, (dist[s], s))
    while p:
        d, u = heapq.heappop(p)
        if dist[u] < d:
            continue
        c[u] = 1
        for g in G[u]:
            if c[g[0]] == 0 and dist[u] + g[1] < dist[g[0]]:
                dist[g[0]] = dist[u] + g[1]
                heapq.heappush(p, (dist[g[0]], g[0]))
    return dist

n, m, r = map(int, input().split())
t = list(map(int, input().split()))
G = [[] for _ in range(n + 1)]
for _ in range(m):
    a, b, c = map(int, input().split())
    G[a].append([b, c])
    G[b].append([a, c])
D = [[0] * r for _ in range(r)]
for i in range(r):
    d = dijkstra(t[i])
    for j in range(r):
        D[i][j] = d[t[j]]

x = [i for i in range(r)]
ans = pow(10, 10)
for y in itertools.permutations(x):
    dist = 0
    y = list(y)
    for i in range(r - 1):
        dist += D[y[i]][y[i + 1]]
    ans = min(ans, dist)
print(ans)