This page is still under construction.

Parts of this page are still being built. What you see may change.

Highways

Time limit1sMemory limit128 MB

Summary
Connect all towns with the cheapest new highways, where some links already exist, and report the summed squared lengths of the added edges.
Level

Medium5 of 10

Topics
Minimum spanning tree, Union-find
Solved
No attempts yet

Problem

The island nation of Flatopia is perfectly flat, but its network of public highways is poor. The government has already built a number of highways connecting some of the towns, yet some towns still cannot be reached by highway. More highways must be built so that it is possible to drive between every pair of towns using only the highway system.

The towns are numbered from 11 to NN, and town ii is at Cartesian coordinates (xi,yi)(x_i, y_i). Each highway connects exactly two towns, runs in a straight line, and can be driven in both directions; its length equals the Euclidean distance between the two towns. Highways may cross one another, but a driver can only switch between two highways at a town that is an endpoint of both.

The government wants every town to be reachable from every other town while spending as little as possible. Because the terrain is flat, the cost of a highway is proportional to its length, so the cheapest system is the one that minimizes the total length of the newly built highways.

Input

The first line contains a single integer NN (1≤N≤7501 \le N \le 750), the number of towns.

Each of the next NN lines contains two integers xix_i and yiy_i (∣xi∣,∣yi∣≤10000|x_i|, |y_i| \le 10000), the coordinates of town ii (for ii from 11 to NN). Every town has a unique location.

The next line contains a single integer MM (0≤M≤10000 \le M \le 1000), the number of highways that have already been built. Each of the next MM lines contains two distinct town numbers that are already directly connected by a highway. Each pair of towns is joined by at most one existing highway.

Output

Build new highways so that every town becomes reachable from every other town, using the existing and the new highways together, with the minimum possible total length of the new highways.

Because that minimum total length is a sum of Euclidean distances (a sum of square roots), report it as an integer: output a single integer equal to the sum of the squared lengths of the new highways, i.e. the sum of (xi−xj)2+(yi−yj)2(x_i - x_j)^2 + (y_i - y_j)^2 over every new highway between towns ii and jj in the minimum-total-length system.

This value is uniquely determined even when several different systems achieve the minimum total length. If all towns are already connected and no new highway is needed, output 00.

Examples6

  1. Example 1

    Input
    9
    1 5
    0 0 
    3 2
    4 5
    5 1
    0 4
    5 2
    1 2
    5 3
    3
    1 3
    9 7
    1 2
    
    Expected output
    16
    
  2. Example 2

    Input
    1
    0 0
    0
    
    Expected output
    0
    
  3. Example 3

    Input
    2
    0 0
    3 4
    0
    
    Expected output
    25
    
  4. Example 4

    Input
    2
    0 0
    3 4
    1
    1 2
    
    Expected output
    0
    
  5. Example 5

    Input
    3
    0 0
    0 3
    4 0
    0
    
    Expected output
    25
    
  6. Example 6

    Input
    4
    0 0
    0 2
    2 0
    2 2
    0
    
    Expected output
    12