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)


#[macro_use]
mod input {

use std;
use std::io;

const SPLIT_DELIMITER: char = ' ';

#[macro_export]
#[allow(unused_macros)]
macro_rules! input {
    ( $($x:expr ),*) => {
        {
            let temp_str = input_line_str();
            let mut split_result_iter = temp_str.split_whitespace();
                $(
                let buf_split_result = split_result_iter.next();
                let buf_split_result = buf_split_result.unwrap();
                    ($x) = buf_split_result.parse().unwrap();
                )*
        }
    };
}

#[allow(dead_code)]
pub fn input_line_str() -> String {
    let mut s = String::new();
    io::stdin().read_line(&mut s).unwrap();
    s.trim().to_string()
}

#[allow(dead_code)]
pub fn p<T>(t: T)
where
    T: std::fmt::Display,
{
    println!("{}", t);
}

#[allow(dead_code)]
pub fn input_vector2d<T>(line: usize) -> Vec<Vec<T>>
where
    T: std::str::FromStr,
{
    let mut v: Vec<Vec<T>> = Vec::new();

    for _ in 0..line {
        let vec_line = input_vector();
        v.push(vec_line);
    }
    v
}

#[allow(dead_code)]
pub fn input_vector<T>() -> Vec<T>
where
    T: std::str::FromStr,
{
    let mut v: Vec<T> = Vec::new();

    let s = input_line_str();
    let split_result = s.split(SPLIT_DELIMITER);
    for z in split_result {
        let buf = match z.parse() {
            Ok(r) => r,
            Err(_) => panic!("Parse Error"),
        };
        v.push(buf);
    }
    v
}

#[allow(dead_code)]
pub fn str2vec(s: &str) -> Vec<char> {
    let mut v: Vec<char> = Vec::new();
    for c in s.chars() {
        v.push(c);
    }
    v
}

}

use input::*;
use std::cmp;

fn next_permutation(v: &mut Vec<usize>) {
    let mut i = 1;
    let mut j = 0;
    let mut jj = (99999999, 0);
    while i < v.len() {
        if v[i] < v[i - 1] {
            break;
        }
        i += 1;
    }
    if i == v.len() {
        return;
    }
    while j < i {
        if v[i] < v[j] {
            if jj.0 > v[j] {
                jj = (v[j], j);
            }
        }
        j += 1;
    }
    v.swap(i, jj.1);
    v[0..i].reverse();
}

fn get_permutation(n: usize) -> usize {
    if n == 1 {
        return 1;
    }
    get_permutation(n - 1) * n
}

fn main() {
    let (n, m, r): (usize, usize, usize);
    input!(n, m, r);
    let mut graph = vec![vec![999999999999; n]; n];
    let mut rv = input_vector::<usize>();
    let mut sum = 99999999999;
    for z in &mut rv {
        *z -= 1;
    }
    for _ in 0..m {
        let (a, b, c): (usize, usize, usize);
        input!(a, b, c);
        graph[a - 1][b - 1] = c;
        graph[b - 1][a - 1] = c;
    }

    //ワーシャルフロイド法
    for z in 0..n {
        for x in 0..n {
            for y in 0..n {
                graph[x][y] = cmp::min(graph[x][y], graph[x][z] + graph[z][y]);
            }
        }
    }
    rv.sort();
    rv.reverse();
    for _ in 0..get_permutation(r) {
        let mut sum_buf = 0;
        for z in 0..r - 1 {
            sum_buf += graph[rv[z]][rv[z + 1]];
        }
        sum = cmp::min(sum_buf, sum);
        next_permutation(&mut rv);
    }
    p(sum);
}