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 Rust (1.15.1) (judge verdict: Accepted)

#[test]
fn test_solve() {
    assert_eq!(solve("3 3 3\n1 2 3\n1 2 1\n2 3 1\n3 1 4\n"), "2\n");
    assert_eq!(solve("3 3 2\n1 3\n2 3 2\n1 3 6\n1 2 2\n"), "4\n");
    assert_eq!(
        solve("4 6 3\n2 3 4\n1 2 4\n2 3 3\n4 3 1\n1 4 1\n4 2 2\n3 1 6\n"),
        "3\n"
    );
}
fn solve(src: &str) -> String {
    let mut sc = Scanner::from_str(src);

    let n: usize = sc.read();
    let m: usize = sc.read();
    let rn: usize = sc.read();
    let r: Vec<usize> = (0..rn).map(|_| sc.read()).collect();
    let mut nbr = vec![Vec::new(); n];
    for _ in 0..m {
        let a: usize = sc.read();
        let b: usize = sc.read();
        let c: i64 = sc.read();
        nbr[a - 1].push((b - 1, c));
        nbr[b - 1].push((a - 1, c));
    }
    let nbr = nbr;

    let mut ans = i64::max_value();
    let sds = shortest_dists(&nbr);
    let mut prm = (0..rn).collect::<Vec<_>>();
    while {
        let s = (0..rn - 1)
            .map(|i| sds[r[prm[i]] - 1][r[prm[i + 1]] - 1])
            .sum();
        ans = std::cmp::min(ans, s);
        next_permutation(&mut prm)
    } {}

    format!("{}\n", ans)
}
fn shortest_dists(neighbor: &[Vec<(usize, i64)>]) -> Vec<Vec<i64>> {
    let n = neighbor.len();
    let mut d = vec![vec![i64::max_value() >> 1; n]; n];
    for v in 0..n {
        for &(w, c) in &neighbor[v] {
            d[v][w] = c;
        }
    }
    for k in 0..n {
        for i in 0..n {
            for j in 0..n {
                d[i][j] = std::cmp::min(d[i][j], d[i][k] + d[k][j])
            }
        }
    }
    d
}
fn next_permutation<T: Ord>(v: &mut [T]) -> bool {
    let len = v.len();
    if len <= 1 {
        return false;
    }
    let mut i = len - 1;
    loop {
        let i1 = i;
        i -= 1;
        if v[i] < v[i1] {
            let mut i2 = len - 1;
            while v[i] >= v[i2] {
                i2 -= 1;
            }
            v.swap(i, i2);
            for j in 0..(len - i1) >> 1 {
                v.swap(i1 + j, len - j - 1);
            }
            return true;
        }
        if i == 0 {
            v.reverse();
            return false;
        }
    }
}
fn main() {
    let mut buf = String::new();
    use std::io::{Read, Write};
    std::io::stdin().read_to_string(&mut buf).unwrap();
    std::io::stdout().write_all(solve(&buf).as_bytes()).unwrap();
}
struct Scanner<'a>(std::str::SplitWhitespace<'a>);
impl<'a> Scanner<'a> {
    fn from_str(src: &str) -> Scanner {
        Scanner(src.split_whitespace())
    }
    fn get(&mut self) -> &str {
        self.0.next().unwrap()
    }
    fn read<T: std::str::FromStr>(&mut self) -> T {
        self.get().parse().ok().unwrap()
    }
}