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)

pub fn read<T: std::str::FromStr>() -> T {
    let mut s = String::new();
    std::io::stdin().read_line(&mut s).ok();
    s.trim().parse().ok().unwrap()
}

pub fn read_vec<T: std::str::FromStr>() -> Vec<T> {
    read::<String>()
        .split_whitespace()
        .map(|e| e.parse().ok().unwrap())
        .collect()
}

pub fn read_vec2<T: std::str::FromStr>(n: usize) -> Vec<Vec<T>> {
    (0..n).map(|_| read_vec()).collect()
}

pub fn read_col<T: std::str::FromStr>(n: usize) -> Vec<T> {
    (0..n).map(|_| read()).collect()
}

fn index(v: &Vec<usize>, n: usize) -> Option<usize> {
    for i in 0..v.len() {
        if n == v[i] {
            return Some(i);
        }
    }
    None
}

fn fun(
    u: usize,
    edges: &Vec<Vec<(usize, u32)>>,
    cost: &mut Vec<u32>,
    set: &mut BTreeSet<usize>,
    c: u32,
) {
    cost[u] = c;
    for i in 0..edges[u].len() {
        let v = edges[u][i].0;
        let ct = edges[u][i].1;
        if c + ct < cost[v] {
            cost[v] = c + ct;
        }
    }
    let mut id: usize = 300;
    let mut tmp: u32 = 1000000000;
    if !set.is_empty() {
        for v in set.clone() {
            if cost[v] < tmp {
                tmp = cost[v];
                id = v;
            }
        }
        set.remove(&id);
        fun(id, edges, cost, set, tmp);
    }
}

fn fun2(u: usize, mat: &Vec<Vec<u32>>, set: &mut BTreeSet<usize>) -> u32 {
    if set.is_empty() {
        return 0;
    }
    let mut ans: u32 = 1000000000;
    for v in set.clone() {
        set.remove(&v);
        let tmp = mat[u][v] + fun2(v, mat, set);
        set.insert(v);
        if tmp < ans {
            ans = tmp;
        }
    }
    ans
}

use std::collections::BTreeSet;

fn main() {
    let nmr: Vec<usize> = read_vec();
    let n = nmr[0];
    let m = nmr[1];
    let r = nmr[2];
    let rtmp: Vec<usize> = read_vec();
    let rvec: Vec<usize> = rtmp.into_iter().map(|a| a - 1).collect();

    let mut edges: Vec<Vec<(usize, u32)>> = vec![vec![]; n];
    let mut mat: Vec<Vec<u32>> = vec![vec![0; r]; r];

    for _ in 0..m {
        let abc: Vec<usize> = read_vec();
        let a = abc[0] - 1;
        let b = abc[1] - 1;
        let c = abc[2] as u32;
        edges[a].push((b, c));
        edges[b].push((a, c));
    }

    for i in 0..r {
        let mut cost: Vec<u32> = vec![1000000000; n];
        let mut set: BTreeSet<usize> = BTreeSet::new();
        for j in 0..n {
            if j != rvec[i] {
                set.insert(j);
            }
        }
        fun(rvec[i], &edges, &mut cost, &mut set, 0);
        for j in 0..r {
            mat[i][j] = cost[rvec[j]];
        }
    }

    let mut ans: u32 = 1000000000;
    for u in 0..r {
        let mut set: BTreeSet<usize> = BTreeSet::new();
        for v in 0..r {
            if v != u {
                set.insert(v);
            }
        }
        let tmp = fun2(u, &mat, &mut set);
        if tmp < ans {
            ans = tmp;
        }
    }
    println!("{}", ans);
}