Optical Fiber

Time limit1sMemory limit128 MB

Summary
Given a tree of cities, each with up to 50 candidate router sites, pick one site per city to minimize the sum of Euclidean edge lengths.
Level

Hard8 of 10

Topics
Dynamic programming, Tree, DFS, Geometry
Solved
No attempts yet

Problem

A developing country wants to improve its communication infrastructure. Right now each city has its own local computer network, but there is no fast link between the cities. The country's communications ministry has decided to build a fast optical fiber network that connects every city.

To connect the cities, an optical fiber link is installed between selected pairs of cities. To keep the cost low, the links are chosen so that there is exactly one fiber path between any pair of cities. In other words, the NN cities and the N−1N-1 links form a tree.

Each city installs a single optical router, and every fiber link that ends in that city is connected to this router. In each city there are several candidate locations where the router may be installed. Your task is to choose one installation location for every city so that the total length of optical fiber needed for the project is minimized.

The length of a link equals the Euclidean distance between the chosen locations of the two cities it connects.

Input

The input consists of several test cases. The first line of each test case contains the number of cities NN (1≤N≤10001 \le N \le 1000).

The description of each city follows. The first line for a city contains the city's (unique) name — capital letters only, at most 15 characters — and the number of candidate sites CiC_i (1≤Ci≤501 \le C_i \le 50) where its router may be installed. Each of the next CiC_i lines contains two integers XX and YY (−10000≤X,Y≤10000-10000 \le X, Y \le 10000), the coordinates of a candidate site.

After all cities have been described, N−1N-1 lines follow, each containing the names of two cities that will be joined by a fiber link.

The end of the input is indicated by N=0N = 0.

Output

For each test case, print on a single line the minimum total length of optical fiber needed to connect all the cities. Round the answer to one decimal place.

Examples1

  1. Example 1

    Input
    3
    AUSTIN 1
    500 500
    DALLAS 2
    1000 10
    990 -10
    ELPASO 2
    0 0
    30 0
    ELPASO AUSTIN
    DALLAS ELPASO
    3
    HUSTON 3
    100 0
    100 50
    100 100
    AUSTIN 2
    200 0
    180 40
    SANANTONIO 2
    0 -10
    10 -50
    HUSTON AUSTIN
    HUSTON SANANTONIO
    0
    
    Expected output
    1646.3
    189.9