Connect the Campus
Time limit1sMemory limit128 MB
Given N points in the plane and some already-built zero-cost edges, add edges connecting all points at minimum total Euclidean length.
- Level
Medium6 of 10
- Topics
- Minimum spanning tree, Union-find, Graph, Geometry
- Solved
- No attempts yet
Problem
Many new buildings are under construction on the campus of the University of Polkaroo. The university wants every building to be connected to every other building — directly or indirectly — through a campus network of communication cables.
Each building is a point in the plane given by an -coordinate and a -coordinate. Each communication cable connects exactly two buildings along the straight line segment between them, and information travels along a cable in both directions. Cables may cross one another freely, but they are joined only at their endpoints (the buildings).
The campus map shows the locations of all buildings and all existing communication cables. You may not change the existing cables. Decide where to install new cables so that all buildings become connected, while minimizing the total length of new cable used.
Input
The input describes a single test case. The first line contains the number of buildings . The buildings are labelled from to . Each of the next lines gives the - and -coordinates of one building. These coordinates are integers whose absolute values are at most , and no two buildings share the same point.
The next line contains the number of existing cables , followed by lines. Each of those lines contains two integers: the numbers of the two buildings that the existing cable directly connects. At most one cable directly connects any given pair of buildings.
Output
Print, on a single line, the minimum possible total length of the new cables, rounded to two decimal places.