Uvaoj 10397-connect the Campus "minimum spanning tree"

Source: Internet
Author: User

Uvaoj 10397-connect the Campus

Many new buildings is under construction on the campus of the University of Waterloo. The university has hired bricklayers, electricians, plumbers, and a computer programmer. A computer programmer? Yes, you had been hired to ensure that each building was connected to every other building (directly or indirectly) Throug H The campus network of communication cables. We'll treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly and buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross all other, but they is only connected together at their endpoints (at buildings). You are been given a campus map which shows the locations of all buildings and existing communication cables. You must not alter the existing cables. Determine where to install the new communication cables so, all buildings is connected. Of course, the university wants you to Minimize the amount of new cable.

Input

The input file describes several test cases. The description of each test case was given Below:the first line of all test case contains the number of buildings N (1≤ n≤750). The buildings is labeled from 1 to N. The next N lines give the x and Y coordinates of the buildings. These coordinates is integers with absolute values in most 10000. No. Buildings occupy the same point. After that there was a line containing the number of existing cables m (0≤m≤1000) followed by M lines describing the ex Isting cables. Each cable was represented by and integers:the building numbers which are directly connected by the cable. There is at the most one cable directly connecting each pair of buildings.

Output

For each set of input, output in a, the total length of the new cables so you plan to use rounded to both deci Mal places.

Sample Input

4

103 104

104 100

104 103

100 100

1

4 2

Sample Output

4.41

Test instructions: give you a number n indicates the number of buildings, the next n line is the coordinates of each building, then a number m next m line two number A, a, a, said that building a and building B has been connected, asked the shortest distance of n buildings

#include <stdio.h> #include <string.h> #include <algorithm> #include <math.h> #define INF 0x3f3f3f#define DD double#define MAX 1010using namespace Std;int n,m;int sum;dd B[max],a[max];DD map[max][max];int Vis[MA X];DD Low[max];DD Fun (int i,int j) {return sqrt ((A[i]-a[j]) * (A[i]-a[j]) + (B[i]-b[j]) * (B[i]-b[j]));//Find the distance between two cities}void Init () {int i,j;for (i=1;i<=n;i++) for (j=1;j<=n;j++) Map[i][j]=i==j?0:inf;}    int main () {int t,i,j,k;    while (scanf ("%d", &n)!=eof) {init ();    for (i=1;i<=n;i++) scanf ("%lf%lf", &a[i],&b[i]); for (i=1;i<=n;i++) {for (j=i;j<=n;j++) {map[i][j]=map[j][i]=fun (I,J);}} scanf ("%d", &m), while (m--) {int x,y;scanf ("%d%d", &x,&y), map[x][y]=map[y][x]=0;//already connected city distance is 0}int next;d    D Min,mindis=0;memset (vis,0,sizeof (VIS)); for (i=1;i<=n;i++) low[i]=map[1][i]; Vis[1]=1;for (i=1;i<n;i++) {min=inf;next=1;for (j=1;j<=n;j++) {if (Min>low[j]&&!vis[j]) {next=j;min =LOW[J];}} Mindis+=min;vis[next]=1;for (j=1;j<=n;j++) {if (!vis[j]&&low[j]>map[next][j]) low[j]=map[next][j];}} printf ("%.2lf\n", Mindis);} return 0;}

  

Uvaoj 10397-connect the Campus "minimum spanning tree"

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.