Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2454 Accepted Submission(s): 1184
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 contains multiple test cases. Process to the end of file.
/*
HDU1162 Eddy's picture
题意:给定画上n个点,求最短的线段把所有点连起来,简单的最小生成树
*/
#include<stdio.h>
#include<math.h>
#include<string.h>
#define MAXN 110
struct Node//定义点
{
double x,y;
}node[MAXN];
double cost[MAXN][MAXN];//耗费矩阵
double distance(Node a,Node b)//两点的距离
{
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
//***********************************************************************
//套用prim模板
//***********************************************************************
#define typec double
const typec INF=0x3f3f3f3f;
int vis[MAXN];
typec lowc[MAXN];
typec prim(typec cost[][MAXN],int n)//点从0~n-1
{
int i,j,p;
typec minc,res=0;
memset(vis,0,sizeof(vis));
vis[0]=1;
for(i=1;i<n;i++) lowc[i]=cost[0][i];
for(i=1;i<n;i++)
{
minc=INF;
p=-1;
for(j=0;j<n;j++)
if(vis[j]==0&&minc>lowc[j])
{minc=lowc[j];p=j;}
if(minc==INF)return -1;//原图不连通
res+=minc;vis[p]=1;
for(j=0;j<n;j++)
if(vis[j]==0&&lowc[j]>cost[p][j])
lowc[j]=cost[p][j];
}
return res;
}
//********************************************************************
int main()
{
int i,j,n;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;i++)
scanf("%lf%lf",&node[i].x,&node[i].y);
for(i=0;i<n;i++)
for(j=0;j<n;j++)
{
if(i==j)cost[i][j]=0;
else
cost[i][j]=distance(node[i],node[j]);
}
printf("%.2lf\n",prim(cost,n));
}
return 0;
}