// https://qiita.com/tanakh/items/0ba42c7ca36cd29d0ac8 macro_rules! input { (source = $s:expr, $($r:tt)*) => { let mut iter = $s.split_whitespace(); input_inner!{iter, $($r)*} }; ($($r:tt)*) => { let mut s = { use std::io::Read; let mut s = String::new(); std::io::stdin().read_to_string(&mut s).unwrap(); s }; let mut iter = s.split_whitespace(); input_inner!{iter, $($r)*} }; } macro_rules! input_inner { ($iter:expr) => {}; ($iter:expr, ) => {}; ($iter:expr, $var:ident : $t:tt $($r:tt)*) => { let $var = read_value!($iter, $t); input_inner!{$iter $($r)*} }; } macro_rules! read_value { ($iter:expr, ( $($t:tt),* )) => { ( $(read_value!($iter, $t)),* ) }; ($iter:expr, [ $t:tt ; $len:expr ]) => { (0..$len).map(|_| read_value!($iter, $t)).collect::>() }; ($iter:expr, chars) => { read_value!($iter, String).chars().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } type Cost = u64; #[derive(Clone)] pub struct Edge { to: usize, cost: Cost, } type Graph = Vec>; use std::cmp::Ordering; #[derive(PartialEq, Eq, PartialOrd)] struct State { at: usize, cost: Cost, } impl Ord for State { fn cmp(&self, other: &Self) -> Ordering { other.cost.cmp(&self.cost) } } use std::collections::BinaryHeap; const INF : Cost = std::u64::MAX; pub fn dijkstra(s: usize, g: &Graph) -> Vec { let mut minc = vec![INF; g.len()]; minc[s] = 0; let mut pq = BinaryHeap::new(); pq.push(State { at: s, cost: 0 }); while !pq.is_empty() { let cur = pq.pop().unwrap(); if minc[cur.at] < cur.cost { continue; } for e in g[cur.at].iter() { let cost = cur.cost + e.cost; if minc[e.to] <= cost { continue; } minc[e.to] = cost; pq.push(State { at: e.to, cost }); } } minc } fn main() { input! { n: usize, m: usize, p: usize1, q: usize1, t: Cost, edges: [(usize1, usize1, Cost); m], } let mut g = vec![vec![]; n]; for (a, b, c) in edges { g[a].push(Edge { to: b, cost: c }); g[b].push(Edge { to: a, cost: c }); } let minc1 = dijkstra(0, &g); let mincp = dijkstra(p, &g); let mincq = dijkstra(q, &g); let mut ans : i64 = -1; let cost = minc1[p] + mincp[q] + mincq[0]; if cost <= t { ans = std::cmp::max(ans, cost as i64); } let cost = minc1[q] + mincq[p] + mincp[0]; if cost <= t { ans = std::cmp::max(ans, cost as i64); } for i in 0..n { for j in 0..n { let cost = minc1[i] + std::cmp::max(mincp[i] + mincp[j], mincq[i] + mincq[j]) + minc1[j]; if cost > t { continue; } let score = minc1[i] + minc1[j] + t - cost; ans = std::cmp::max(ans, score as i64); } } println!("{}", ans); }