HDU 1162 Eddy's picture 最小生成树

时间:2023-02-20 15:59:03


Eddy's picture

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 164 Accepted Submission(s): 120

 

Problem Description



Eddy begins to like painting pictures recently ,he is sure of himself to become a painter.Every day Eddy draws pictures in his small room, and he usually puts out his newest pictures to let his friends appreciate. but the result it can be imagined, the friends are not interested in his picture.Eddy feels very puzzled,in order to change all friends 's view to his technical of painting pictures ,so Eddy creates a problem for the his friends of you.
Problem descriptions as follows: Given you some coordinates pionts on a drawing paper, every point links with the ink with the straight line, causes all points finally to link in the same place. How many distants does your duty discover the shortest length which the ink draws?



 

Input



The first line contains 0 < n <= 100, the number of point. For each point, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the point.

Input contains multiple test cases. Process to the end of file.



 

Output



Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the points.



 

Sample Input




31.0 1.02.0 2.02.0 4.0




 

Sample Output




3.41





/*
HDOJ 1162 最小生成树
找出一条连接所有的最短路径
*/
#include<iostream>
#include<stdio.h>
#include<math.h>
using namespace std;

#define N 101
#define INF 1000000000

struct point{
double x,y;
};
point z[N];
int vis[N];
int n;
double map[N][N],dis[N];

double getDis(point a,point b)
{
return sqrt(pow(a.x-b.x,2)+pow(a.y-b.y,2));
}

void Prime()
{
int i,j,loc;
double sum,min;

memset(vis,0,sizeof(vis));
for(i=1;i<=n;i++)
{
if(i==1) dis[i]=0;
else dis[i]=map[1][i];
}
sum=0;
for(i=1;i<=n;i++)
{
min=INF;
for(j=1;j<=n;j++)
{
if(!vis[j]&&dis[j]<min)
{
min=dis[j];
loc=j;
}
}

vis[loc]=1;
sum+=min;
for(j=1;j<=n;j++)
{
if(!vis[j]&&map[loc][j]<dis[j])//loc到j的值 小于dis[j]
dis[j]=map[loc][j];
}
}
printf("%.2lf\n",sum);
}

int main()
{
int i,j;

//freopen("test.txt","r",stdin);
while(scanf("%d",&n)!=EOF)
{
for(i=1;i<=n;i++)
scanf("%lf%lf",&z[i].x,&z[i].y);

for(i=1;i<=n;i++)
for(j=1;j<=n;j++)
map[i][j]=getDis(z[i],z[j]);
Prime();
}
return 0;
}