use std::{cmp::Reverse, collections::BinaryHeap}; const INF: usize = 1usize << 60; fn dijkstra(start: usize, paths: &Vec>) -> Vec { let n = paths.len(); let mut que = BinaryHeap::new(); que.push((Reverse(0), start)); let mut costs = vec![INF; n]; costs[start] = 0; while let Some((Reverse(cost), u)) = que.pop() { if cost > costs[u] { continue; } for &(v, w) in paths[u].iter() { if costs[u] + w < costs[v] { costs[v] = costs[u] + w; que.push((Reverse(costs[v]), v)); } } } costs } fn main() { let mut nml = String::new(); std::io::stdin().read_line(&mut nml).ok(); let nml: Vec = nml.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let n = nml[0]; let m = nml[1]; let l = nml[2]-1; let mut t = String::new(); std::io::stdin().read_line(&mut t).ok(); let t: Vec = t.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let mut paths = vec![vec![]; n]; for _ in 0..m { let mut temp = String::new(); std::io::stdin().read_line(&mut temp).ok(); let temp: Vec = temp.trim().split_whitespace().map(|s| s.parse().unwrap()).collect(); let a = temp[0]-1; let b = temp[1]-1; let c = temp[2]; paths[a].push((b, c)); paths[b].push((a, c)); } if t.iter().filter(|&&v| v > 0).count() == 1 { println!("0"); return; } let mut dists = Vec::with_capacity(n); for i in 0..n { dists.push(dijkstra(i, &paths)); } let mut result = INF; for center in 0..n { let total = 2 * (0..n).map(|i| dists[center][i] * t[i]).sum::(); let shortcut = (0..n).filter(|&i| t[i] > 0).map(|i| dists[l][center] + 2*dists[center][i] - dists[l][i] - dists[i][center]).max().unwrap(); result = result.min(total + dists[l][center] - shortcut); } println!("{}", result); }