Problem:
Score : 600 points

Problem Statement

On the xy-plane, Snuke is going to travel from the point (x_s, y_s) to the point (x_t, y_t).
He can move in arbitrary directions with speed 1.
Here, we will consider him as a point without size.

There are N circular barriers deployed on the plane.
The center and the radius of the i-th barrier are (x_i, y_i) and r_i, respectively.
The barriers may overlap or contain each other.

A point on the plane is exposed to cosmic rays if the point is not within any of the barriers.

Snuke wants to avoid exposure to cosmic rays as much as possible during the travel.
Find the minimum possible duration of time he is exposed to cosmic rays during the travel.

Constraints

All input values are integers.

-10^9 ≤ x_s, y_s, x_t, y_t ≤ 10^9

(x_s, y_s) ≠ (x_t, y_t)

1≤N≤1,000

-10^9 ≤ x_i, y_i ≤ 10^9

1 ≤ r_i ≤ 10^9

Input

The input is given from Standard Input in the following format:

x_s y_s x_t y_t
N
x_1 y_1 r_1
x_2 y_2 r_2
:
x_N y_N r_N

Output

Print the minimum possible duration of time Snuke is exposed to cosmic rays during the travel.
The output is considered correct if the absolute or relative error is at most 10^{-9}.

Sample Input 1

-2 -2 2 2
1
0 0 1

Sample Output 1

3.6568542495

An optimal route is as follows:

Sample Input 2

-2 0 2 0
2
-1 0 2
1 0 2

Sample Output 2

0.0000000000

An optimal route is as follows:

Sample Input 3

4 -2 -2 4
3
0 0 2
4 0 1
0 4 1

Sample Output 3

4.0000000000

An optimal route is as follows:

Solution in Rust (1.15.1) (judge verdict: Wrong Answer)

#![allow(unused_imports, unused_macros, unknown_lints)]

use std::collections::*;
use std::f64::*;
use std::cmp::*;
use std::cmp::Ordering::*;

macro_rules! dump {($($e:expr), +) => {print!(concat!($(stringify!($e), " = {:?}\n"), +), $($e), +)}}

fn main() {
  let (xs,ys,xt,yt) = {
    let vec = get::vec::<f64>();
    (vec[0], vec[1], vec[2], vec[3])
  };
  let n = get::val::<usize>();
  let xyrs = get::tuple3s::<f64,f64,f64>(n);
  
  let dist = |(xi,yi,ri),(xj,yj,rj)| {
    f64::sqrt((xj-xi)*(xj-xi)+(yj-yi)*(yj-yi)) - ri - rj
  };
  
  let mut es = vec![];
  for i in 0 .. n {
    for j in 0 .. n {
      if i == j {continue}
      es.push((i,j,f64::max(0.0,dist(xyrs[i],xyrs[j]))))
    }
    es.push((n,i,f64::max(0.0,dist((xs,ys,0.0),xyrs[i]))));
    es.push((i,n+1,f64::max(0.0,dist(xyrs[i],(xt,yt,0.0)))))
  }
  
  let mut g = vec![vec![]; n+2];
  for (u,v,c) in es {g[u].push((v,c))}

  let mut dk = vec![None; n+2];
  dk[n] = Some(0.0);
  
  let mut pq = BinaryHeap::new();
  pq.push((F64Total(0.0),n));
  while let Some((F64Total(d),v)) = pq.pop() {
    let d = -d;
    for &(w,c) in &g[v] {
      match dk[w] {
        Some(d0) if d0 <= d+c => {continue},
        _ => {dk[w] = Some(d+c); pq.push((F64Total(-(d+c)),w))}
      }
    }
  }
   
  println!("{}", dk[n+1].unwrap())
}

#[derive(PartialEq, PartialOrd)]
struct F64Total(f64);

impl Eq for F64Total {}
impl Ord for F64Total {
  fn cmp(&self, other: &F64Total) -> Ordering {
    let F64Total(f1) = *self;
    let F64Total(f2) = *other;
    if f1.is_nan() {
      Less
    } else if f2.is_nan() {
      Greater
    } else {
      if (f1-f2).is_sign_positive() {Greater} else {Less}
    }
  }
}

#[allow(dead_code)]
mod get {
  use std::io::*;
  use std::str::*;
  use std::process::*;

  pub fn val<T: FromStr>() -> T {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().parse::<T>().ok().unwrap()
  }

  pub fn vals<T: FromStr>(n: usize) -> Vec<T> {
    let mut vec: Vec<T> = vec![];
    for _ in 0 .. n {
      vec.push(val());
    }
    vec
  }

  pub fn tuple<T1: FromStr, T2: FromStr>() -> (T1, T2) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    (x, y)
  }

  pub fn tuples<T1: FromStr, T2: FromStr>(n: usize) -> Vec<(T1, T2)> {
    let mut vec: Vec<(T1, T2)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple());
    }
    vec
  }

  pub fn tuple3<T1: FromStr, T2: FromStr, T3: FromStr>() -> (T1, T2, T3) {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    let mut it = buf.trim_right().split_whitespace();
    let x = it.next().unwrap().parse::<T1>().ok().unwrap();
    let y = it.next().unwrap().parse::<T2>().ok().unwrap();
    let z = it.next().unwrap().parse::<T3>().ok().unwrap();
    (x, y, z)
  }

  pub fn tuple3s<T1: FromStr, T2: FromStr, T3: FromStr>(n: usize) -> Vec<(T1, T2, T3)> {
    let mut vec: Vec<(T1, T2, T3)> = vec![];
    for _ in 0 .. n {
      vec.push(tuple3());
    }
    vec
  }

  pub fn vec<T: FromStr>() -> Vec<T> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().split_whitespace().map(|t| t.parse::<T>().ok().unwrap()).collect()
  }

  pub fn mat<T: FromStr>(h: usize) -> Vec<Vec<T>> {
    let mut mat = vec![];
    for _ in 0 .. h {
      mat.push(vec());
    }
    mat
  }

  pub fn chars() -> Vec<char> {
    let mut buf = String::new();
    let s = stdin();
    s.lock().read_line(&mut buf).ok();
    if buf.is_empty() {println!("No input"); exit(0)}
    buf.trim_right().chars().collect()
  }
}

#[allow(dead_code)]
mod put {
  use std::string::*;

  pub fn vec<T: ToString>(vec: &Vec<T>, sep: &str) {
    let out = vec.iter().map(|e| e.to_string()).collect::<Vec<_>>().as_slice().join(sep);
    println!("{}", out);
  }

  pub fn mat<T: ToString>(mat: &Vec<Vec<T>>, sep: &str) {
    for v in mat {
      vec(v, sep);
    }
  }
}