use std::cmp::Reverse; use std::collections::BinaryHeap; use std::io::{self, BufRead}; fn main() { let stdin = io::stdin(); let mut input = stdin.lock().lines(); // Read N, M, L let first_line = input.next().unwrap().unwrap(); let mut first_line_split = first_line.split_whitespace(); let N: usize = first_line_split.next().unwrap().parse().unwrap(); let M: usize = first_line_split.next().unwrap().parse().unwrap(); let mut L: usize = first_line_split.next().unwrap().parse().unwrap(); // Read T let second_line = input.next().unwrap().unwrap(); let T: Vec = second_line .split_whitespace() .map(|x| x.parse().unwrap()) .collect(); if T.iter().filter(|&&x| x == 0).count() >= N - 1 { println!("0"); return; } L -= 1; let mut E = vec![vec![]; N]; for _ in 0..M { let line = input.next().unwrap().unwrap(); let mut parts = line.split_whitespace(); let a: usize = parts.next().unwrap().parse().unwrap(); let b: usize = parts.next().unwrap().parse().unwrap(); let c: i64 = parts.next().unwrap().parse().unwrap(); E[a - 1].push((b - 1, c)); E[b - 1].push((a - 1, c)); } let inf: i64 = 1 << 60; let mut D = vec![vec![inf; N]; N]; for i in 0..N { D[i][i] = 0; let mut Q = BinaryHeap::new(); Q.push((Reverse(0), i)); while let Some((Reverse(dis), ind)) = Q.pop() { if D[i][ind] != dis { continue; } for &(to, length) in &E[ind] { if D[i][to] > dis + length { D[i][to] = dis + length; Q.push((Reverse(D[i][to]), to)); } } } } let mut ANS = 1 << 61; for i in 0..N { let mut score: i64 = 0; for j in 0..N { score += T[j] * D[i][j] * 2; } for j in 0..N { if T[j] > 0 { score += D[L][j] - D[i][j]; ANS = ANS.min(score); score -= D[L][j] - D[i][j]; } } } println!("{}", ANS); }