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



Sample input:
3 3 3
1 2 3
1 2 1
2 3 1
3 1 4


Sample output:
2

Submission status: Runtime Error

Solution code:
#coding=UTF-8

def search(jun):
    global ans
    global R
    global Kost
    tmpans=0
    if len(jun)==R:
        mae=jun[0]
        for idx in range(1,R,1):
            ba=jun[idx]
            tmpans += Kost[r[mae]][r[ba]]
            mae=ba

        if ans==None or tmpans<ans:
            ans=tmpans
    else:
        for me in range(0,R,1):
            if not (me in jun):
                search(jun+[me])

mozir=input()
hyo=mozir.split(' ')
N=int(hyo[0])
M=int(hyo[1])
R=int(hyo[2])

mozir=input()
hyo=mozir.split(' ')
r=[(int(mono)-1) for mono in hyo]

Kost=[]
for idx in range(0,N,1):
    Kost.append([None]*N)

for idx in range(0,N,1):
    Kost[idx][idx]=0


for idx in range(0,M,1):
    mozir=input()
    hyo=mozir.split(' ')
    A=int(hyo[0])-1
    B=int(hyo[1])-1
    C=int(hyo[2])

    Kost[A][B]=C
    Kost[B][A]=C

changed=True
while changed:
    changed=False
    for idx in range(0,N,1):
        for idt in range(0,N,1):
            if bool(Kost[idx][idt]) == False:
                continue
            else:
                zen=Kost[idx][idt]
                for idy in range(0,N,1):
                    tmpKost=zen+Kost[idt][idy]
                    if Kost[idx][idy]==None or Kost[idx][idy]>tmpKost:
                        Kost[idx][idy]=tmpKost
                        changed=True

#for gyo in Kost:
#    print(gyo)

ans=None
search([])
print(ans)

Submission status: Runtime Error

Solution code:
import itertools

N, M, R = [int(i) for i in input().split()]
must_visit = [int(i) for i in input().split()]
max_city = max(must_visit)
cities = [i+1 for i in range(max_city)]
min_cost = 999999999999
dic = {}

for _ in range(M):
	dept, dest, dist = [int(i) for i in input().split()]
	if dest < dept:
		dest, dept = dept, dest
	key1, key2 = "{}->{}".format(dept, dest),"{}->{}".format(dest, dept)
	if key1 in dic:
		dic[key1] = dist if dist < dic[key1] else dic[key1]
		dic[key2] = dist if dist < dic[key2] else dic[key2]
	else:
		dic[key1], dic[key2] = dist, dist

for c in list(itertools.permutations(cities)):
	visited = set()
	cost = 0
	for i,k in enumerate(c):
		visited.add(k)
		if set(must_visit) - set(visited) == set():
			break
		cost += dic["{}->{}".format(k,c[i+1])]
	min_cost = cost if min_cost > cost else min_cost

print(min_cost)

Submission status: Runtime Error

Solution code:
import itertools
N, M, R = [int(i) for i in input().split()]
must_visit = [int(i) for i in input().split()]
max_city = max(must_visit)
cities = [i+1 for i in range(max_city)]
min_cost = 999999999999
dic = {}

for _ in range(M):
	dept, dest, dist = [int(i) for i in input().split()]
	if dest < dept:
		dest, dept = dept, dest
	key1, key2 = "{}->{}".format(dept, dest),"{}->{}".format(dest, dept)
	if key1 in dic:
		dic[key1] = dist if dist < dic[key1] else dic[key1]
		dic[key2] = dist if dist < dic[key2] else dic[key2]
	else:
		dic[key1], dic[key2] = dist, dist

for c in list(itertools.permutations(cities)):
	visited = []
	cost = 0
	for i,k in enumerate(c):
		visited.append(k)
		if set(must_visit) - set(visited) == set():
			break
		# print(k,c, set(visited), set(must_visit), set(must_visit) - set(visited) )
		cost += dic["{}->{}".format(k,c[i+1])]
	min_cost = cost if min_cost > cost else min_cost
print(min_cost)

Submission status: Runtime Error

Solution code:
import itertools
N, M, R = [int(i) for i in input().split()]
cities = [int(i) for i in input().split()]
dic = {}

for _ in range(M):
	dept, dest, dist = [int(i) for i in input().split()]
	if dest < dept:
		dest, dept = dept, dest
	key1, key2 = "{}->{}".format(dept, dest),"{}->{}".format(dest, dept)
	if key1 in dic:
		dic[key1] = dist if dist < dic[key1] else dic[key1]
		dic[key2] = dist if dist < dic[key2] else dic[key2]
	else:
		dic[key1], dic[key2] = dist, dist


min_cost = 999999999999
for c in list(itertools.permutations(cities)):
	cost = 0
	for i,k in enumerate(c):
		if not i == len(cities) -1:
			cost += dic["{}->{}".format(k,c[i+1])]
	min_cost = cost if min_cost > cost else min_cost 
print(min_cost)

Submission status: Runtime Error

Solution code:
N,M,R = map(int,input().split())
r = map(int,input().split())
from itertools import permutations
from functools import reduce
import numpy as np
t = np.zeros((M+1,M+1))

for i in range(M):
    a,b,c = map(int, input().split())
    t[a][b] = c
    t[b][a] = c

def cost(l):
    last = len(l) - 1
    ret = 0
    for i,j in enumerate(l):
        if i == last:
            return ret
        ret += t[j][l[i+1]]
        
print(int(min([cost(p) for p in permutations(r)])))

Submission status: Runtime Error

Solution code:
import itertools

n, m, r = map(int, input().split())
order = list(map(int, input().split()))
dist = [[-1 for _ in range(n + 1)] for _ in range(n + 1)]
for i in range(1, n + 1):
    dist[i][i] = 0

for _ in range(m):
    a, b, c = map(int, input().split())
    if dist[a][b] == -1:
        dist[a][b] = c
    else:
        dist[a][b] = min(dist[a][b], c)

    dist[b][a] = dist[a][b]

for k in range(1, n + 1):
    for i in range(1, n + 1):
        if dist[i][k] == -1:
            continue
        for j in range(1, n + 1):
            if dist[k][j] == -1:
                continue

            dist[i][j] = min(dist[i][j], dist[i][k] + dist[k][j])

ans = float('inf')
for perm in itertools.permutations(order):
    cur = 0
    for i in range(1, r):
        if dist[perm[i - 1]][perm[i]] == -1:
            assert(1 == 0)

        cur += dist[perm[i - 1]][perm[i]]

    ans = min(ans, cur)

print(ans)