Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3746 Accepted Submission(s): 1844
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.
#include<stdio.h>
#include<math.h>
struct node
{
float x;
float y;
}point[105];
float G[105][105];
float dis(int i,int j)
{
return (float)sqrt(pow((point[i].x-point[j].x),2)+pow((point[i].y-point[j].y),2));
}
float prim(int n)
{
int i,j,t,k;
int visit[105];
float min,len=0;
float D[105];
visit[0]=1;
D[0]=0;
for(i=1;i<n;++i)
{
D[i]=G[0][i];
visit[i]=0;
}
for(i=1;i<n;++i)
{
k=0;
min=(float)0x7fffffff;
for(j=0;j<n;++j)
{
if(!visit[j]&&min>D[j])//注意这点与dijst求最短路径的不同
{
k=j;
min=D[j];
}
}
visit[k]=1;
len+=min;
for(t=0;t<n;++t)
if(!visit[t]&&G[k][t]<D[t])
D[t]=G[k][t];
}
return len;
}
int main()
{
int n,i,j;
while(scanf("%d",&n)!=EOF)
{
for(i=0;i<n;++i)
scanf("%f%f",&point[i].x,&point[i].y);
for(i=0;i<n;++i)
for(j=0;j<=i;++j)
G[i][j]=G[j][i]=dis(i,j);
printf("%.2f\n",prim(n));
}
return 0;
}