macro_rules ! input { ( source = $ s : expr , $ ( $ r : tt ) * ) => { let mut iter = $ s . split_whitespace ( ) ; input_inner ! { iter , $ ( $ r ) * } } ; ( iter = $ iter : ident , $ ( $ 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 ) * } } ; ( $ ( $ 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 , mut $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let mut $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , mut $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , mut $ var : usize $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident : $ t : tt $ ( $ r : tt ) * ) => { let $ var = read_value ! ( $ iter , $ t ) ; input_inner ! { $ iter $ ( $ r ) * } } ; ( $ iter : expr , $ var : ident $ ( $ r : tt ) * ) => { input_inner ! { $ iter , $ var : usize $ ( $ 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 ::< Vec < _ >> ( ) } ; ( $ iter : expr , { chars : $ base : expr } ) => { read_value ! ( $ iter , String ) . chars ( ) . map ( | c | ( c as u8 - $ base as u8 ) as usize ) . collect ::< Vec < usize >> ( ) } ; ( $ iter : expr , { char : $ base : expr } ) => { read_value ! ( $ iter , { chars : $ base } ) [ 0 ] } ; ( $ iter : expr , chars ) => { read_value ! ( $ iter , String ) . chars ( ) . collect ::< Vec < char >> ( ) } ; ( $ iter : expr , char ) => { read_value ! ( $ iter , chars ) [ 0 ] } ; ( $ iter : expr , usize1 ) => { read_value ! ( $ iter , usize ) - 1 } ; ( $ iter : expr , $ t : ty ) => { $ iter . next ( ) . unwrap ( ) . parse ::<$ t > ( ) . unwrap ( ) } ; }
#[derive(Debug)]
pub struct WeightedGraph<T> {
    graph: Vec<Vec<(usize, T)>>,
}
impl<T: Clone> WeightedGraph<T> {
    pub fn new(n: usize) -> WeightedGraph<T> {
        WeightedGraph {
            graph: vec![vec![]; n],
        }
    }
    pub fn add_edge(&mut self, u: usize, v: usize, c: T) {
        self.graph[u].push((v, c))
    }
}
#[derive(Eq, PartialEq, Clone, Debug)]
pub struct Rev<T>(pub T);
impl<T: PartialOrd> PartialOrd for Rev<T> {
    fn partial_cmp(&self, other: &Rev<T>) -> Option<std::cmp::Ordering> {
        other.0.partial_cmp(&self.0)
    }
}
impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> std::cmp::Ordering {
        other.0.cmp(&self.0)
    }
}
#[derive(PartialEq, PartialOrd)]
pub struct TotalOrd<T>(pub T);
impl<T: PartialEq> Eq for TotalOrd<T> {}
impl<T: PartialOrd> Ord for TotalOrd<T> {
    fn cmp(&self, other: &TotalOrd<T>) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}
impl WeightedGraph<f64> {
    pub fn dijkstra(&self, s: usize) -> Vec<f64> {
        use std::collections::BinaryHeap;
        const INF: f64 = std::f64::INFINITY;
        let mut cost = vec![INF; self.graph.len()];
        let mut heap = BinaryHeap::new();
        cost[s] = 0.;
        heap.push((TotalOrd(Rev(0.)), s));
        while let Some((d, u)) = heap.pop() {
            let d = (d.0).0;
            for &(v, c) in self.graph[u].iter() {
                if cost[v] > d + c {
                    cost[v] = d + c;
                    heap.push((TotalOrd(Rev(d + c)), v));
                }
            }
        }
        cost
    }
}
fn dist(x1: f64, y1: f64, x2: f64, y2: f64) -> f64 {
    ((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2)).sqrt()
}
use std::cmp::max;
fn main() {
    input! { s: (f64,f64), t: (f64,f64), n, circles: [(f64,f64,f64); n] };
    let mut g = WeightedGraph::new(n + 2);
    g.add_edge(0, 1, dist(s.0, s.1, t.0, t.1));
    for i in 0..n {
        let (x, y, r) = circles[i];
        let c = max(TotalOrd(0.), TotalOrd(dist(s.0, s.1, x, y) - r)).0;
        g.add_edge(0, i + 2, c);
        let c = max(TotalOrd(0.), TotalOrd(dist(t.0, t.1, x, y) - r)).0;
        g.add_edge(i + 2, 1, c);
    }
    for i in 0..n - 1 {
        for j in i + 1..n {
            let (x1, y1, r1) = circles[i];
            let (x2, y2, r2) = circles[j];
            let c = max(TotalOrd(0.), TotalOrd(dist(x1, y1, x2, y2) - r1 - r2)).0;
            g.add_edge(i + 2, j + 2, c);
            g.add_edge(j + 2, i + 2, c);
        }
    }
    println!("{}", g.dijkstra(0)[1]);
}


Measured memory_bytes: 30760