uvaoj 10397 - Connect the Campus【最小生成树】

时间:2022-10-19 19:50:02

uvaoj 10397 - Connect the Campus

Many new buildings are 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 have been hired to ensure that each building is connected to every other building (directly or indirectly) through the campus network of communication cables. We will treat each building as a point specified by an x-coordinate and a y-coordinate. Each communication cable connects exactly two buildings, following a straight line between the buildings. Information travels along a cable in both directions. Cables can freely cross each other, but they are only connected together at their endpoints (at buildings). You have 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 new communication cables so that all buildings are connected. Of course, the university wants you to minimize the amount of new cable that you use.

Input

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

Output

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

Sample Input

4

103 104

104 100

104 103

100 100

1

4 2

Sample Output

4.41

题意:给你一个数字n表示建筑的数量,接下来n行是每座建筑的坐标,然后一个数字m接下来m行每行两个数a,b,表示建筑a和建筑b之间已经联通,问联通n个建筑的最短距离

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#define INF 0x3f3f3f
#define DD double
#define MAX 1010
using namespace std;
int n,m;
int sum;
DD b[MAX],a[MAX];
DD map[MAX][MAX];
int vis[MAX];
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]));//求两个城市之间的距离
}
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;//已经联通的城市距离为0
}
int next;
DD 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;
}