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 in Java8 (OpenJDK 1.8.0) (judge verdict: Runtime Error)

package ABC073.D;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.PriorityQueue;
import java.util.Scanner;
import java.util.Set;
import java.util.stream.Collectors;

public class Main {
    // TODO solve
    public static void main(final String[] args) {
        final Scanner scanner = new Scanner(System.in);
        final int n = scanner.nextInt();
        final int m = scanner.nextInt();
        final int r = scanner.nextInt();

        final int[] toVisit = new int[r];
        for (int i = 0; i < r; i++) {
            toVisit[i] = scanner.nextInt() - 1;
        }

        final Map<Integer, List<Destination>> graph = new HashMap<>();
        for (int i = 0; i < m; i++) {
            final int a = scanner.nextInt() - 1;
            final int b = scanner.nextInt() - 1;
            final int c = scanner.nextInt();

            graph.computeIfAbsent(a, v -> new ArrayList<>()).add(new Destination(b, c));
            graph.computeIfAbsent(b, v -> new ArrayList<>()).add(new Destination(a, c));
        }

        final long[][] min = new long[n][n];

        for (int i = 0; i < toVisit.length; i++) {
            final int from = toVisit[i];
            for (int j = i + 1, toVisitLength = toVisit.length; j < toVisitLength; j++) {
                final int to = toVisit[j];
                if (from == to) {
                    continue;
                }

                final long[] minDistances = new long[n];
                Arrays.fill(minDistances, -1);
                minDistances[from] = 0;

                final PriorityQueue<Destination> queue = new PriorityQueue<>(Comparator.comparingLong(o -> o.distance));
                queue.addAll(graph.get(from));
                final Destination poll = queue.poll();
                minDistances[poll.city] = poll.distance;
                dijkstra(minDistances, poll.city, to, graph, queue);

                min[from][to] = minDistances[to];
                min[to][from] = minDistances[to];
            }
        }

        final Set<Integer> set = Arrays.stream(toVisit)
                .boxed()
                .collect(Collectors.toSet());

        permutation(0, r, set, new int[r], min);
        System.out.println(answer);
    }

    static long answer = Long.MAX_VALUE;

    private static void permutation(final int level, final int length, final Set<Integer> set, final int[] per, final long[][] min) {
        if (level == length) {
            long temp = 0;
            for (int i = 0; i < per.length - 1; i++) {
                temp += min[per[i]][per[i + 1]];
            }
            answer = Math.min(answer, temp);
            return;
        }

        for (final int element : set) {
            final Set<Integer> set2 = new HashSet<>(set);
            set2.remove(element);
            per[level] = element;
            permutation(level + 1, length, set2, per, min);
        }
    }

    private static void dijkstra(final long[] distances, final int current, final int finalDestination, final Map<Integer, List<Destination>> graph, final PriorityQueue<Destination> queue) {
        if (distances[finalDestination] >= 0 || queue.isEmpty()) {
            return;
        }

        for (final Destination dest : graph.get(current)) {
            if (dest.city == current || distances[dest.city] >= 0) {
                continue;
            }

            queue.add(new Destination(dest.city, distances[current] + dest.distance));
        }

        Destination nextCity;
        while (distances[(nextCity = queue.poll()).city] >= 0) ;

        distances[nextCity.city] = nextCity.distance;
        dijkstra(distances, nextCity.city, finalDestination, graph, queue);
    }

    static class Destination {
        int city;
        long distance;

        Destination(final int city, final long distance) {
            this.city = city;
            this.distance = distance;
        }
    }
}