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 Go (1.6) (judge verdict: Accepted)

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"math"
	"os"
	"strconv"
)

func getScanner(fp *os.File) *bufio.Scanner {
	scanner := bufio.NewScanner(fp)
	scanner.Split(bufio.ScanWords)
	scanner.Buffer(make([]byte, 500001), 500000)
	return scanner
}

func getNextString(scanner *bufio.Scanner) string {
	scanner.Scan()
	return scanner.Text()
}

func getNextInt(scanner *bufio.Scanner) int {
	i, _ := strconv.Atoi(getNextString(scanner))
	return i
}

func getNextInt64(scanner *bufio.Scanner) int64 {
	i, _ := strconv.ParseInt(getNextString(scanner), 10, 64)
	return i
}

func getNextUint64(scanner *bufio.Scanner) uint64 {
	i, _ := strconv.ParseUint(getNextString(scanner), 10, 64)
	return i
}

func getNextFloat64(scanner *bufio.Scanner) float64 {
	i, _ := strconv.ParseFloat(getNextString(scanner), 64)
	return i
}

func main() {
	fp := os.Stdin
	wfp := os.Stdout

	if os.Getenv("MASPY") == "ますピ" {
		fp, _ = os.Open(os.Getenv("BEET_THE_HARMONY_OF_PERFECT"))
	}
	if os.Getenv("MASPYPY") == "ますピッピ" {
		wfp, _ = os.Create(os.Getenv("NGTKANA_IS_GENIUS10"))
	}

	scanner := getScanner(fp)
	writer := bufio.NewWriter(wfp)

	pp := make([]P, 2)
	for i := 0; i < 2; i++ {
		pp[i].x = getNextFloat64(scanner)
		pp[i].y = getNextFloat64(scanner)
		pp[i].r = 0.0
	}
	n := getNextInt(scanner)

	for i := 0; i < n; i++ {
		x := getNextFloat64(scanner)
		y := getNextFloat64(scanner)
		r := getNextFloat64(scanner)
		pp = append(pp, P{
			x: x,
			y: y,
			r: r,
		})
	}

	costs := make([][]float64, n+2)
	for i := 0; i < n+2; i++ {
		costs[i] = make([]float64, n+2)
	}
	for i := 0; i < n+2; i++ {
		for j := i + 1; j < n+2; j++ {
			cost := math.Sqrt(math.Pow(pp[i].x-pp[j].x, 2)+math.Pow(pp[i].y-pp[j].y, 2)) - pp[i].r - pp[j].r
			if cost < 0 {
				cost = 0
			}
			costs[i][j] = cost
			costs[j][i] = cost
		}
	}

	dd := make([]float64, n+2)
	for i := 1; i < n+2; i++ {
		dd[i] = math.MaxFloat64
	}

	visited := make([]int, n+2)
	hp := &HH{}
	heap.Init(hp)

	heap.Push(hp, HP{
		i:    0,
		cost: 0,
	})
	for hp.Len() > 0 && visited[1] == 0 {
		p := heap.Pop(hp).(HP)
		if visited[p.i] == 1 {
			continue
		}
		visited[p.i] = 1
		for i := 0; i < n+2; i++ {
			if visited[i] == 1 {
				continue
			}
			if dd[i] < p.cost+costs[p.i][i] {
				continue
			}
			dd[i] = p.cost + costs[p.i][i]
			heap.Push(hp, HP{
				i:    i,
				cost: p.cost + costs[p.i][i],
			})
		}
	}

	fmt.Fprintln(writer, dd[1])
	writer.Flush()
}

type HP struct {
	i    int
	cost float64
}
type HH []HP

func (h HH) Len() int           { return len(h) }
func (h HH) Less(i, j int) bool { return h[i].cost < h[j].cost }
func (h HH) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }
func (h *HH) Push(x interface{}) {
	*h = append(*h, x.(HP))
}
func (h *HH) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

type P struct {
	x, y, r float64
}