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 Common Lisp (SBCL 1.1.14) (judge verdict: Accepted)

;; -*- 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))
  (etypecase sequence
    (list
     (let ((end (or end most-positive-fixnum))
           (optimum (funcall key (car sequence)))
           (index 0)
           (pos 0))
       (dolist (x sequence (values index optimum))
         (when (>= pos end)
           (return (values index optimum)))
         (when (>= pos start)
           (unless (funcall predicate optimum (funcall key x))
             (setq optimum (funcall key x)
                   index pos)))
         (incf pos))))
    (vector
     (let ((end (or end (length sequence))))
       (unless (<= start end)
         (error "Can't find optimal value in null interval [~A, ~A)" start end))
       (let ((optimum (funcall key (aref sequence 0)))
             (index 0))
         (dotimes (i (length sequence) (values index optimum))
           (unless (funcall predicate optimum (funcall key (aref sequence i)))
             (setq optimum (aref sequence i)
                   index i))))))))

(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 i = (find-argopt q #'< :key (lambda (v) (aref dists v)))
          for v = (nth i q)
          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)