let rec dikjstra ( + ) ( < ) q edge d =
  match q with
  | [] -> ()
  | u :: q ->
      let u, q =
        List.fold_left (fun (u, q') u' ->
          if d.(u) < d.(u') then (u, u' :: q')
          else (u', u :: q')) (u, []) q in
      List.iter (fun (v, c) ->
        if c + d.(u) < d.(v) then
          d.(v) <- c + d.(u)) (edge u);
      dikjstra ( + ) ( < ) q edge d

let () = Scanf.scanf "%d %d %d %d\n%d\n" @@ fun xs ys xt yt n ->
  let xyrs = Array.init (n + 2) @@ function
    | 0 -> (float_of_int xs, float_of_int ys, 0.)
    | 1 -> (float_of_int xt, float_of_int yt, 0.)
    | _ -> Scanf.scanf "%d %d %d\n" @@ fun x y r ->
        float_of_int x, float_of_int y, float_of_int r in
  let d = Array.make (n + 2) infinity in
  d.(0) <- 0.;
  dikjstra ( +. ) ( < )
    (Array.to_list @@ Array.init (n + 2) @@ fun v -> v)
    (fun v ->
      let (x, y, r) = xyrs.(v) in
      List.mapi (fun u (x', y', r') ->
        (u, max 0. (sqrt ((x -. x') ** 2. +. (y -. y') ** 2.) -. r -. r'))) @@
      Array.to_list xyrs) d;
  Printf.printf "%.16f" d.(1)

