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: Accepted)

//package Qd
import java.util.*
import java.math.*
import java.lang.Math.*
import java.nio.IntBuffer


private val readString: ()->String = {readLine()!!}
private val readInt: ()->Int = {readLine()!!.toInt()}
private val readLong: ()->Long = {readLine()!!.toLong()}
private val readIntArray: ()->IntArray = {readLine()!!.split(' ').map{it.toInt()}.toIntArray()}
private val readLongArray: ()->LongArray = {readLine()!!.split(' ').map{it.toLong()}.toLongArray()}
private val errPrintln: (String)->Unit = {msg -> System.err.println(msg)}
private val MOD = 1e9.toLong()+7
private val INF = Int.MAX_VALUE/2
private val LINF = Long.MAX_VALUE/2

private fun permutation(A: IntArray): List<IntArray> {
    val ary = A.sorted()
    val N = ary.size
    val used = BooleanArray(N){false}
    val hm = HashMap<IntBuffer, IntArray>()
    val ptn = ArrayList<Int>(0)
    fun dfs(n: Int) {
        if (n >= N) {
            val value = ptn.toIntArray()
            val key = IntBuffer.wrap(value)
            hm[key] = value
        }
        for (i in 0 until N) {
            if (used[i]) continue
            used[i] = true
            ptn.add(ary[i])
            dfs(n+1)
            ptn.removeAt(ptn.lastIndex)
            used[i] = false
        }
    }
    dfs(0)
    val res = ArrayList<IntArray>()
    for (key in hm.keys.sorted()) {
        res.add(hm[key]!!)
    }
    return res.toList()
}

data class To(val idx: Int, val cost: Long)
private class Warshall(val eg: Array<ArrayList<To>>) {
    private var dist = Array(0){LongArray(0)}
    init {
        val N = eg.size
        dist = Array(N){LongArray(N){LINF}}
        for (i in 0 until N) {
            dist[i][i] = 0
        }
        for (s in 0 until N) {
            for (t in eg[s]) {
                dist[s][t.idx] = t.cost
                dist[t.idx][s] = t.cost
            }
        }
        for (k in 0 until N) {
            for (i in 0 until N) {
                if (dist[i][k] == LINF) continue
                for (j in 0 until N) {
                    if (dist[k][j] == LINF) continue
                    if (dist[i][j] > dist[i][k] + dist[k][j]) {
                        dist[i][j] = dist[i][k] + dist[k][j]
                    }
                }
            }
        }
    }
    fun distance(i: Int, j: Int): Long {
        return dist[i][j]
    }
    fun hasNegativeCycle(): Boolean {
        val N = dist.size
        var res = false
        for(i in 0 until N) {
            if (dist[i][i] < 0) {
                res = true
                break
            }
        }
        return res
    }
}

private fun solveD(R: IntArray, eg: Array<ArrayList<To>>): Long {
    val patterns = permutation(R)

    val ws = Warshall(eg)

    var res = LINF
    for (cities in patterns) {
        var distSum = 0L
        val K = cities.size
        for (i in 0 until K-1) {
            distSum += ws.distance(cities[i], cities[i+1])
        }
        res = min(res, distSum)
    }

    return res
}


fun main(args: Array<String>) {
    val (N, M, K) = readIntArray()
    val R = readIntArray().map{ it-1 }
    val eg = Array(N) {ArrayList<To>()}
    for (i in 0 until M) {
        var (a, b, c) = readIntArray()
        a--; b--;
        eg[a].add(To(b, c.toLong()))
        eg[b].add(To(a, c.toLong()))
    }

    val ans = solveD(R.toIntArray(), eg)

    println(ans)
}