hdu1162Eddy's picture 最小生成树

╮(╯_╰)╭模板是距离已知 这个多算个欧式距离

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
3 1.0 1.0 2.0 2.0 2.0 4.0

Sample Output
3.41
``````#include <iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
int flag1=0;
double sum;
double arr_list[110][110];
struct Edge
{
int point;
double lowcost;
int flag;
};
Edge edge[12100];
struct Point
{
double x,y;
}point[110];
double prim(int n)
{
int i,j,k=1,flag;
double min,sum2=0;
j=1;
for(i=1;i<=n;i++)
{
if(i!=j)
{
edge[i].point=i;
edge[i].lowcost=arr_list[j][i];
edge[i].flag=0;
}
}
edge[j].lowcost=0;
edge[j].flag=1;
for(i=2;i<=n;i++)
{
k=1;
min=65535;
flag=0;
for(j=2;j<=n;j++)
{
if(edge[j].flag==0&&edge[j].lowcost<min)
{
k=j;
min=edge[j].lowcost;
flag=1;
}
}
if(!flag) return -1;
sum2+=min;
edge[k].flag=1;
for(j=2;j<=n;j++)
{
if(edge[j].flag==0&&arr_list[k][j]<edge[j].lowcost)
{
edge[j].point=k;
edge[j].lowcost=arr_list[k][j];
}
}
}
return sum2;
}
int main()
{
// freopen("cin.txt","r",stdin);
int n;
while(~scanf("%d",&n))
{
for(int i=1;i<=n;i++)
{
cin>>point[i].x>>point[i].y;
arr_list[i][i]=65535;
}
for(int i=1;i<n;i++)
{
for(int j=i+1;j<=n;j++)
{
arr_list[i][j]=sqrt(pow((point[i].x-point[j].x),2)+pow((point[i].y-point[j].y),2));
arr_list[j][i]=arr_list[i][j];
//cout<<arr_list[i][j]<<endl;
}
}
sum=prim(n);
printf("%.2lf\n",sum);
}
return 0;
}
``````

2022-12-28 17:14

02-03 18:12

02-02 04:30

2022-12-13 23:34

2022-12-26 18:53

2022-12-12 11:15

2022-12-22 18:27

2022-12-13 17:11