;; -*- coding: utf-8 -*-
(eval-when (:compile-toplevel :load-toplevel :execute)
  (defparameter OPT
    #+swank '(optimize (speed 3) (safety 2))
    #-swank '(optimize (speed 3) (safety 0) (debug 0)))
  #+swank (ql:quickload '(:cl-debug-print :fiveam))
  #-swank (set-dispatch-macro-character #\# #\> (lambda (s c p) (declare (ignore c p)) (read s nil (values) t))))
#+swank (cl-syntax:use-syntax cl-debug-print:debug-print-syntax)
#-swank (disable-debugger) ; for CS Academy

;; BEGIN_INSERTED_CONTENTS
;; unfinished.

(declaim (inline find-argopt))
(defun find-argopt (sequence predicate &key (start 0) end (key #'identity))
  "Returns an index x that satisfies (NOT (FUNCALL PREDICATE SEQUENCE[y]
SEQUENCE[x])) (i.e. SEQUENCE[x] >= SEQUENCE[y]) for all the indices y and
returns SEQUENCE[x] as the second value."
  (declare ((or null (integer 0 #.most-positive-fixnum)) end)
           ((integer 0 #.most-positive-fixnum) start)
           (function predicate)
           (sequence sequence))
  (labels ((invalid-range-error ()
             (error "Can't find optimal value in null interval [~A, ~A) on ~A" start end sequence)))
    (etypecase sequence
      (list
       (let ((sequence (nthcdr start sequence))
             (end (or end most-positive-fixnum)))
         (when (or (null sequence)
                   (>= start end))
           (invalid-range-error))
         (let ((opt-element (car sequence))
               (opt-index start)
               (pos start))
           (dolist (x sequence)
             (when (>= pos end)
               (return-from find-argopt (values opt-index opt-element)))
             (unless (funcall predicate (funcall key opt-element) (funcall key x))
               (setq opt-element x
                     opt-index pos))
             (incf pos))
           (values opt-index opt-element))))
      (vector
       (let ((end (or end (length sequence))))
         (when (or (>= start end)
                   (>= start (length sequence)))
           (invalid-range-error))
         (let ((opt-element (aref sequence start))
               (opt-index start))
           (loop for i from start below end
                 for x = (aref sequence i)
                 do (unless (funcall predicate (funcall key opt-element) (funcall key x))
                      (setq opt-element x
                            opt-index i)))
           (values opt-index opt-element)))))))

(declaim (ftype (function * (values fixnum &optional)) read-fixnum))
(defun read-fixnum (&optional (in *standard-input*))
  (declare #.OPT)
  (macrolet ((%read-byte ()
               `(the (unsigned-byte 8)
                     #+swank (char-code (read-char in nil #\Nul))
                     #-swank (sb-impl::ansi-stream-read-byte in nil #.(char-code #\Nul) nil))))
    (let* ((minus nil)
           (result (loop (let ((byte (%read-byte)))
                           (cond ((<= 48 byte 57)
                                  (return (- byte 48)))
                                 ((zerop byte) ; #\Nul
                                  (error "Read EOF or #\Nul."))
                                 ((= byte #.(char-code #\-))
                                  (setf minus t)))))))
      (declare ((integer 0 #.most-positive-fixnum) result))
      (loop
        (let* ((byte (%read-byte)))
          (if (<= 48 byte 57)
              (setq result (+ (- byte 48) (* 10 (the (integer 0 #.(floor most-positive-fixnum 10)) result))))
              (return (if minus (- result) result))))))))

(defmacro dbg (&rest forms)
  #+swank
  (if (= (length forms) 1)
      `(format *error-output* "~A => ~A~%" ',(car forms) ,(car forms))
      `(format *error-output* "~A => ~A~%" ',forms `(,,@forms)))
  #-swank (declare (ignore forms)))

(defmacro define-int-types (&rest bits)
  `(progn
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "UINT~A" b)) () '(unsigned-byte ,b))) bits)
     ,@(mapcar (lambda (b) `(deftype ,(intern (format nil "INT~A" b)) () '(signed-byte ,b))) bits)))
(define-int-types 2 4 7 8 15 16 31 32 62 63 64)

(declaim (inline println))
(defun println (obj &optional (stream *standard-output*))
  (let ((*read-default-float-format* 'double-float))
    (prog1 (princ obj stream) (terpri stream))))

(defconstant +mod+ 1000000007)

;; Body

(defconstant +eps+ 1d-10)

(defun main ()
  (declare #.OPT)
  (let* ((init-x (float (read) 1d0))
         (init-y (float (read) 1d0))
         (goal-x (float (read) 1d0))
         (goal-y (float (read) 1d0))
         (n (read))
         (xs (make-array (+ n 2) :element-type 'double-float))
         (ys (make-array (+ n 2) :element-type 'double-float))
         (rs (make-array (+ n 2) :element-type 'double-float :initial-element 0d0))
         (init n)
         (goal (+ 1 n))
         (q (loop for i below (+ n 2) collect i))
         (dists (make-array (+ n 2) :element-type 'double-float :initial-element most-positive-double-float)))
    (declare ((simple-array double-float (*)) dists)
             (list q)
             (uint16 n))
    (dotimes (i n)
      (setf (aref xs i) (float (read-fixnum) 1d0)
            (aref ys i) (float (read-fixnum) 1d0)
            (aref rs i) (float (read-fixnum) 1d0)))
    (setf (aref xs init) init-x
          (aref ys init) init-y
          (aref xs goal) goal-x
          (aref ys goal) goal-y
          (aref dists init) 0d0)
    (loop until (null q)
          for v = (nth-value 1 (find-argopt q #'< :key (lambda (v) (aref dists v))))
          for x0 = (aref xs v)
          for y0 = (aref ys v)
          for r0 = (aref rs v)
          do (setf q (delete v q :count 1))
             (dotimes (next (+ n 2))
               (let* ((x1 (aref xs next))
                      (y1 (aref ys next))
                      (r1 (aref rs next))
                      (delta (max 0d0 (- (sqrt (+ (expt (- x0 x1) 2)
                                                  (expt (- y0 y1) 2)))
                                         r0 r1))))
                 (when (< (+ (aref dists v) delta +eps+)
                          (aref dists next))
                   (setf (aref dists next)
                         (+ (aref dists v) delta))))))
    (println (aref dists goal))))

#-swank (main)


Measured memory_bytes: 17384