// ---------- begin chmin, chmax ---------- pub trait ChangeMinMax { fn chmin(&mut self, x: Self) -> bool; fn chmax(&mut self, x: Self) -> bool; } impl ChangeMinMax for T { fn chmin(&mut self, x: Self) -> bool { *self > x && { *self = x; true } } fn chmax(&mut self, x: Self) -> bool { *self < x && { *self = x; true } } } // ---------- end chmin, chmax ---------- // ---------- begin input macro ---------- // reference: 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 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, bytes) => { read_value!($iter, String).bytes().collect::>() }; ($iter:expr, usize1) => { read_value!($iter, usize) - 1 }; ($iter:expr, $t:ty) => { $iter.next().unwrap().parse::<$t>().expect("Parse error") }; } // ---------- end input macro ---------- use std::io::Write; use std::collections::*; use std::cmp::*; type Map = BTreeMap; type Set = BTreeSet; type Deque = VecDeque; fn diff(x: usize, y: usize) -> usize { x.max(y) - x.min(y) } #[derive(Clone, Copy)] pub struct F(f64); impl Ord for F { fn cmp(&self, other: &Self) -> std::cmp::Ordering { self.partial_cmp(other).unwrap() } } impl PartialOrd for F { fn partial_cmp(&self, other: &Self) -> Option { self.0.partial_cmp(&other.0) } } impl PartialEq for F { fn eq(&self, other: &Self) -> bool { self.0 == other.0 } } impl Eq for F {} fn run() { input! { dst: (usize1, usize1), n: usize, p: [(usize1, usize1, usize); n], } let m = 600; let w = 1000f64; let mut ans = (diff(dst.0, p[0].0) + diff(dst.1, p[0].1)) as f64 * w; let inf = ans; let src = p[0]; let mut point = vec![vec![]; 7]; for (x, y, v) in p { point[v - 1].push((x, y)); } let mut memo = vec![]; for (i, p) in point.iter().enumerate() { let mut dp = vec![vec![inf; m]; m]; let add = w / (i + 1) as f64; let mut q = Deque::new(); for &(x, y) in p.iter() { dp[x][y] = 0f64; q.push_back((x, y)); } while let Some((x, y)) = q.pop_front() { let d = dp[x][y] + add; for &(x, y) in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)].iter() { if x < m && y < m && dp[x][y].chmin(d) { q.push_back((x, y)); } } } memo.push(dp); } let mut dp = vec![vec![vec![inf; m]; m]; 7]; dp[src.2 - 1][src.0][src.1] = 0f64; let mut h = BinaryHeap::new(); h.push((Reverse(F(0f64)), src.2 - 1, src.0, src.1)); while let Some((Reverse(F(d)), v, x, y)) = h.pop() { if d > dp[v][x][y] { continue; } let val = d + w / (v + 1) as f64; let src = (x, y); for &(x, y) in [(x - 1, y), (x + 1, y), (x, y - 1), (x, y + 1)].iter() { if x < m && y < m && dp[v][x][y].chmin(val) { h.push((Reverse(F(val)), v, x, y)); for i in (v + 1)..7 { if memo[i][x][y] > d { let time = memo[i][x][y] - d; let rem = 0f64.max(w - (v + 1) as f64 * time); let meet = rem / (i + 1 + v + 1) as f64; if dp[i][x][y].chmin(memo[i][x][y] + meet + meet) { h.push((Reverse(F(dp[i][x][y])), i, x, y)); } let w = w - meet * (i + 1) as f64; if dp[i][src.0][src.1].chmin(memo[i][x][y] + meet + w / (i + 1) as f64) { h.push((Reverse(F(dp[i][src.0][src.1])), i, x, y)); } } } } } } for dp in dp { ans.chmin(dp[dst.0][dst.1]); } println!("{:.8}", ans); } fn main() { run(); }