#[allow(unused_imports)]
use std::cmp::{Ordering};

#[allow(unused_imports)]
use std::collections::{BTreeMap, BTreeSet, BinaryHeap, HashMap, HashSet, VecDeque};

#[allow(unused_imports)]
use std::io::{stdin, stdout, BufWriter, Write};

#[allow(unused_imports)]
use std::iter::FromIterator;

#[allow(unused_macros)]
macro_rules! min {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::min($x, min!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! max {
    ($x: expr) => ($x);
    ($x: expr, $($z: expr),+) => {{
        std::cmp::max($x, max!($($z),*))
    }}
}

#[allow(unused_macros)]
macro_rules! get { 
    ([$t: ty]) => { 
        { 
            let mut line = String::new(); 
            stdin().read_line(&mut line).unwrap(); 
            line.split_whitespace().map(|t|t.parse::<$t>().unwrap()).collect::<Vec<_>>()
        }
    };
    ([$t: ty];$n: expr) => {
        (0..$n).map(|_|get!([$t])).collect::<Vec<_>>()
    };
    ($t: ty) => {
        {
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            line.trim().parse::<$t>().unwrap()
        }
    };
    ($($t: ty),*) => {
        { 
            let mut line = String::new();
            stdin().read_line(&mut line).unwrap();
            let mut iter = line.split_whitespace();
            ($(iter.next().unwrap().parse::<$t>().unwrap(),)*)
        }
    };
    ($t: ty; $n: expr) => {
        (0..$n).map(|_|get!($t)).collect::<Vec<_>>()
    };
    ($($t: ty),*; $n: expr) => {
        (0..$n).map(|_|get!($($t),*)).collect::<Vec<_>>()
    };
}

#[allow(unused_macros)]
macro_rules! debug {
    ($($a:expr),*) => {
        #[cfg(debug_assertions)]
        writeln!(&mut std::io::stderr(), concat!("[DEBUG] ", $(stringify!($a), "={:?} "),*), $($a),*).unwrap();
    }
}

const BIG_STACK_SIZE: bool = true;

#[allow(dead_code)]
fn main() {
    use std::thread;
    if BIG_STACK_SIZE {
        thread::Builder::new()
            .stack_size(32 * 1024 * 1024)
            .name("solve".into())
            .spawn(solve)
            .unwrap()
            .join()
            .unwrap();
    } else {
        solve();
    }
}

#[derive(PartialEq, PartialOrd, Copy, Clone)]
pub struct Total<T>(pub T);
impl<T: PartialEq> Eq for Total<T> {}
impl<T: PartialOrd> Ord for Total<T> {
    fn cmp(&self, other: &Total<T>) -> std::cmp::Ordering {
        self.0.partial_cmp(&other.0).unwrap()
    }
}
impl<T: std::str::FromStr> std::str::FromStr for Total<T> {
    type Err = T::Err;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let res = T::from_str(s)?;
        Ok(Total(res))
    }
}
impl<T: std::fmt::Display> std::fmt::Debug for Total<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl<T: std::fmt::Display> std::fmt::Display for Total<T> {
    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
        write!(f, "{}", self.0)
    }
}
impl<T: std::ops::Add<Output = T>> std::ops::Add for Total<T> {
    type Output = Self;
    fn add(self, other: Self) -> Self {
        Total(self.0 + other.0)
    }
}
impl<T: std::ops::Sub<Output = T>> std::ops::Sub for Total<T> {
    type Output = Self;
    fn sub(self, other: Self) -> Self {
        Total(self.0 - other.0)
    }
}
impl<T: std::ops::Mul<Output = T>> std::ops::Mul for Total<T> {
    type Output = Self;
    fn mul(self, other: Self) -> Self {
        Total(self.0 * other.0)
    }
}
impl<T: std::ops::Div<Output = T>> std::ops::Div for Total<T> {
    type Output = Self;
    fn div(self, other: Self) -> Self {
        Total(self.0 / other.0)
    }
}
impl<T: std::ops::Neg<Output = T>> std::ops::Neg for Total<T> {
    type Output = Self;
    fn neg(self) -> Self::Output {
        Total(-self.0)
    }
}

#[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<Ordering> {
        other.0.partial_cmp(&self.0)
    }
}
impl<T: Ord> Ord for Rev<T> {
    fn cmp(&self, other: &Rev<T>) -> Ordering {
        other.0.cmp(&self.0)
    }
}

fn solve() {
    let (xs,ys,xt,yt) = get!(f64, f64, f64, f64);
    let n = get!(usize);
    let mut xyr = get!(f64,f64,f64; n);
    xyr.insert(0, (xs,ys,0.0));
    xyr.push((xt,yt,0.0));

    let mut nodes = vec![vec![0.0; n+2]; n+2];

    for i in 0..n+2 {
        let (x1,y1,r1) = xyr[i];
        for j in 0..n+2 {
            let (x2,y2,r2) = xyr[j];
            let dist = ((x1-x2).powf(2.0) + (y1-y2).powf(2.0)).sqrt();
            let cost = (dist-r1-r2).max(0.0);
            nodes[i][j] = cost;
        }
    }

    let mut heap = BinaryHeap::new();
    let mut dist = vec![1e15; n+2];
    heap.push((Rev(Total(0.0)), 0));
    dist[0] = 0.0;
    while let Some((Rev(Total(cost)), node)) = heap.pop() {
        for next in 0..n+2 {
            let ncost = cost+nodes[node][next];
            if dist[next] > ncost {
                dist[next] = ncost;
                heap.push((Rev(Total(ncost)), next)); 
            }
        }
    }
    println!("{}", dist[n+1]);
}


Measured memory_bytes: 23800