Ultraviolet A 10397-connect the campus

Source: Internet
Author: User

Problem E
Connect the campus
Input:Standard Input
Output:Standard output
Time limit:2 seconds

Creating new buildings are under construction on thecampus of the University of Waterloo. The University has hired bricklayers, electricians, plumbers, and a computerprogrammer. A computer programmer? Yes, you have been hired to ensure that eachbuilding is connected to every other building (directly or indirectly) throughthe campus network of communication cables.

We will treat each building as a point specifiedby an X-coordinate and a Y-coordinate. each communication cable connectsexactly two buildings, following a straight line between the buildings. information travels along a cable in both directions. cables can freely crosseach other, but they are only connected together at their endpoints (atbuildings ).

You have been given a campus map which shows thelocations of all buildings and existing communication cables. you must notalter the existing cables. determine where to install new communication cablesso that all buildings are connected. of course, the university wants you tominimize The amount of new cable that you use.

 

Fig:UniversityOfWaterlooCampus

 

Input

The input file describes several test case. The description of each test case is givenbelow:

The first line of each test case contains thenumber of BuildingsN (1 <= n <= 750). The buildings are labeled from1ToN. The nextNLines giveXAndYCoordinates of the buildings. thesecoordinates are integers with absolute values at most10000. No two buildings occupy the same point. After that there isa line containing the number of existing CablesM (0 <= m <= 1000)FollowedMLines describing the existing cables. Each cable is representedby two integers: The building numbers which are directly connected by thecable. There is at most one cable directly connecting each pair of buildings.

Output

For each set of input, output in a single linethe total length of the new cables that you plan to use, rounded to two decimalplaces.

Sample Input

4
103 104
104 100
104 103
100 100
1
4 2

4
103 104

104100

104103

100100

1

4 2

 

Sample output
4.41
4.41

# Include <iostream> # include <stack> # include <cstring> # include <cstdio> # include <string> # include <algorithm> # include <queue> # include <set> # include <map> # include <fstream> # include <stack> # include <list> # include <sstream> # include <cmath> using namespace STD; # define MS (ARR, Val) memset (ARR, Val, sizeof (ARR) # define MC (DEST, Src) memcpy (DEST, SRC, sizeof (SRC )) # define n 755 # define INF 0x3fffffff # define vi NT vector <int> # define setint set <int> # define mint Map <int, int> # define lint list <int> # define Sch stack <char> # define qch queue <char> # define Sint stack <int> # define qint queue <int>/* Minimum spanning Tree Algorithm prim: dense graph (N * n), Kruskal: sparse graph (E * LGE) */struct point {int X, Y;} in [N]; bool visit [N]; double Low [N]; double G [N] [N]; int n, m; int multi2 (INT X1, int X2, int Y1, int Y2) {int x = x1-X2; int y = Y1-Y2; return x * x + y * y ;} Void prim () {MS (visit, 0); double ans = 0; int S = n-1, _ MinP; double _ min; For (INT I = 1; I <= N; I ++) {LOW [I] = G [1] [I];} low [1] = inf; visit [1] = 1; while (s --) {_ min = low [1]; for (INT I = 1; I <= N; I ++) // find the smallest edge {If (! Visit [I] & low [I] <_ min) {_ min = low [I]; _ MinP = I ;}} visit [_ MinP] = true; ans + = SQRT (_ min); For (INT I = 1; I <= N; I ++) // update the low Array {If (! Visit [I] & G [I] [_ MinP] <low [I]) {LOW [I] = G [I] [_ MinP] ;}} printf ("%. 2lf \ n ", ANS);} int main () {While (~ Scanf ("% d", & N) {for (INT I = 1; I <= N; I ++) {scanf ("% d ", & in [I]. x, & in [I]. y); For (Int J = 1; j <I; j ++) {G [J] [I] = G [I] [J] = multi2 (in [I]. x, in [J]. x, in [I]. y, in [J]. y) ;}} MS (visit, 0); int S, E; scanf ("% d", & M); While (M --) {scanf ("% d", & S, & E); G [s] [e] = G [E] [s] = 0; // change the distance between connected points to 0. In the prim algorithm, the shortest side is definitely selected (key)} prim ();} return 0 ;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.