hdu 1162 Eddy's picture(最小生成树,基础)

时间:2023-02-09 20:47:08
#define  _CRT_SECURE_NO_WARNINGS

#include <stdio.h>
#include<string.h>
#include <malloc.h>
#include<stdlib.h>
#include<algorithm>
#include<iostream>
#include<math.h>
using namespace std; #define M 110
#define inf 999999999
double mat[M][M];
struct tt
{
double x,y;
}dian[M]; double prim(int n,int sta)
{
double minn,sum=,dis[M];
int mark[M],i,j,flag;
for(i=;i<n;i++)
{
dis[i]=mat[sta][i];
mark[i]=;
}
mark[sta]=;
for(i=;i<n;i++)
{
minn=inf;
for(j=;j<n;j++)
{
if(mark[j]==&&dis[j]<minn)
{
minn=dis[j];
flag=j;
}
}
mark[flag]=;
sum+=dis[flag];
for(j=;j<n;j++)
{
if(dis[j]>mat[flag][j])
dis[j]=mat[flag][j];
}
}
return sum;
} int main()
{
int n,m,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=;i<n;i++)
{
scanf("%lf%lf",&dian[i].x,&dian[i].y);
for(j=;j<i;j++)
{
mat[i][j]=mat[j][i]=sqrt((dian[i].x-dian[j].x)*(dian[i].x-dian[j].x)+(dian[i].y-dian[j].y)*(dian[i].y-dian[j].y));
}
}
printf("%.2lf\n",prim(n,));
}
return ;
}