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 Kotlin (1.0.0) (judge verdict: Wrong Answer)

import java.util.*

fun main(args: Array<String>) {
    val (N, M, R) = readLine()!!.split(" ").map { it.toInt() }
    val r = readLine()!!.split(" ").map { it.toInt() - 1 }
    val graph = AdjacentListGraph<Int, Long>(Array(N) { Vertex<Int, Long>(0) }.toMutableList())
    repeat(M) {
        val (a, b, c) = readLine()!!.split(" ").map { it.toInt() }
        graph.addEdge(a - 1, b - 1, c.toLong())
        graph.addEdge(b - 1, a - 1, c.toLong())
    }
    val distancesFromNode = mutableMapOf<Int, Array<Long>>()
    r.forEach { distancesFromNode[it] = distancesFrom(graph, it) }
    // 1 -> 4 -> 3 = d[1][4] + d[4][3]
    val orders = r.toList().permutations()
    var ans = Long.MAX_VALUE
    for (order in orders) {
        val ret = (0 until R - 1).map { distancesFromNode[order[it]]?.get(order[it + 1])!! }.sum()
        ans = Math.min(ans, ret)
    }
    println(ans)
}

interface Graph<T : Comparable<T>, E : Comparable<E>> {
    val size: Int
    fun getWeight(from: Int, to: Int): E?
    fun edgesFrom(from: Int): List<Edge<E>>
    fun addEdge(from: Int, to: Int, weight: E): Boolean
    fun removeEdge(from: Int, to: Int): Boolean
    fun isAdjacent(from: Int, to: Int): Boolean
}

data class Edge<T : Comparable<T>>(val to: Int, val weight: T)
data class Vertex<T : Comparable<T>, E : Comparable<E>>(val data: T) {
    val edges = mutableListOf<Edge<E>>()

    fun addEdge(to: Int, weight: E): Boolean{
        edges.add(Edge(to, weight))
        return true
    }

    fun removeEdge(to: Int): Boolean {
        for ((i, edge) in edges.withIndex()) {
            if (edge.to == to) {
                edges.removeAt(i)
                return true
            }
        }
        return false
    }
}

class AdjacentListGraph<T : Comparable<T>, E : Comparable<E>>(
    private val vertices: MutableList<Vertex<T, E>>
) : Graph<T, E> {
    override val size = vertices.size
    override fun getWeight(from: Int, to: Int): E? =
        vertices[from].edges.find { it.to == to }?.weight
    override fun edgesFrom(from: Int): List<Edge<E>> = vertices[from].edges
    override fun addEdge(from: Int, to: Int, weight: E) = vertices[from].addEdge(to, weight)
    override fun removeEdge(from: Int, to: Int) = vertices[from].removeEdge(to)
    override fun isAdjacent(from: Int, to: Int) =
        vertices[from].edges.find { it.to == to } != null
}

class AdjacencyMatrixGraph<T : Comparable<T>, E : Comparable<E>>(
    private val matrix: Array<Array<E?>>
) : Graph<T, E> {
    override val size = matrix.size
    override fun getWeight(from: Int, to: Int): E? = matrix[from][to]
    override fun edgesFrom(from: Int): List<Edge<E>> =
        matrix[from].withIndex()
            .filter { it.value != null }
            .map { Edge(it.index, it.value!!) }

    override fun addEdge(from: Int, to: Int, weight: E): Boolean {
        if (matrix[from][to] == null) {
            matrix[from][to] = weight
            return true
        }
        return false
    }

    override fun removeEdge(from: Int, to: Int): Boolean {
        if (from >= size || to >= size || matrix[from][to] == null) {
            return false
        }
        matrix[from][to] = null
        return true
    }

    override fun isAdjacent(from: Int, to: Int): Boolean = matrix[from][to] != null
}



fun <T : Comparable<T>> distancesFrom(
        graph: Graph<T, Long>, from: Int): Array<Long> {
    /**
    * Dijkstra's algorithm
    * weight must be non-negative number
    * complexity is O((E+V)logV)
    */
    val distances = Array(graph.size) { Long.MAX_VALUE }
    val isFixed = BooleanArray(graph.size)
    val pQueue = TreeSet<Int> { a, b -> distances[a].compareTo(distances[b]) }
    distances[from] = 0L
    pQueue.add(from)
    while (pQueue.isNotEmpty()) {
        val node = pQueue.pollLast()
        isFixed[node] = true
        for (edge in graph.edgesFrom(node)) {
            val dist = distances[node] + edge.weight
            if (dist < distances[edge.to]) {
                distances[edge.to] = dist
                pQueue.add(edge.to)
            }
        }
    }
    return distances
}
fun List<Int>.dot(other: List<Int>): Int =
    this.mapIndexed { index, value -> value * other[index] }.sum()

fun List<Long>.dot(other: List<Long>): Long =
    this.mapIndexed { index, value -> value * other[index] }.sum()

fun <T> product(vararg lists: List<T>): Sequence<List<T>> {
    fun <T> productTwoList(left: List<T>, right: List<T>): Sequence<List<T>> =
        left.asSequence().flatMap { leftElem -> right.asSequence().map { listOf(leftElem, it) } }

    fun <T> product2DListAnd1DList(list2D: Sequence<List<T>>, list1D: List<T>): Sequence<List<T>> =
        list2D.flatMap { seq -> list1D.asSequence().map { seq + it } }

    require(lists.size >= 2)
    return lists.asSequence()
        .drop(2)
        .fold(productTwoList(lists[0], lists[1])) { acc, suc -> product2DListAnd1DList(acc, suc) }
}

fun <T> Collection<T>.powerSet(): Set<Set<T>> = powerSet(this, setOf(setOf()))

private tailrec fun <T> powerSet(left: Collection<T>, acc: Set<Set<T>>): Set<Set<T>> = when {
    left.isEmpty() -> acc
    else -> powerSet(left.drop(1), acc + acc.map { it + left.first() })
}

fun <T> Set<T>.combinations(n: Int): Set<Set<T>> = when {
    n < 0 -> throw Error("n must be positive, but got $n")
    n == 0 -> setOf(setOf())
    n >= size -> setOf(toSet())
    else ->
        powerSet()
            .filter { it.size == n }
            .toSet()
}

fun <T> List<T>.insert(index: Int, element: T): List<T> = when {
    index < 0 || index > size -> throw Error("Can not insert to index = $index")
    index == 0 -> listOf(element) + this
    index == size -> this + element
    else -> dropLast(size - index) + element + drop(index)
}

fun <T> List<T>.permutations(): Set<List<T>> = when {
    isEmpty() -> setOf()
    size == 1 -> setOf(listOf(get(0)))
    else -> {
        val element = get(0)
        drop(1).permutations()
            .flatMap { sublist -> (0..sublist.size).map { i -> sublist.insert(i, element) } }
            .toSet()
    }
}

fun <T, R> Iterable<T>.scanLeft(initial: R, operation: (acc: R, T) -> R): Sequence<R> {
    var last = initial
    return this.asSequence().map { last = operation(last, it); last }
}