| ||||||||||
| Online Judge | Problem Set | Authors | Online Contests | User | ||||||
|---|---|---|---|---|---|---|---|---|---|---|
| Web Board Home Page F.A.Qs Statistical Charts | Current Contest Past Contests Scheduled Contests Award Contest | |||||||||
数据只能是float类型否则你自己看着办!#include <stdio.h>
#include <string.h>
#include <math.h>
#define len 110
#define INF 9999999.0
float map[len][len],dist[len];
int visit[len];
void init(int n)
{
int i,j;
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
map[i][j]=INF;
map[i][i]=0.0;
}
}
typedef struct point
{
float x;
float y;
}Point;
Point p[len];
int main()
{
int n,i,rec,j;
float sum,m,distance;
scanf("%d",&n);
getchar();
init(n);
for(i=1;i<=n;i++)
scanf("%f %f",&p[i].x,&p[i].y);
for(i=1;i<=n;i++)
{
for(j=i;j<=n;j++)
{
distance=sqrt((p[j].x-p[i].x)*(p[j].x-p[i].x)+(p[j].y-p[i].y)*(p[j].y-p[i].y));
if(distance<map[i][j])
{
map[i][j]=distance;
map[j][i]=map[i][j];
}
}
}
memset(visit,0,sizeof(visit));
for(i=1;i<=n;i++)
dist[i]=map[1][i];
sum=0.0;
visit[1]=1;
for(i=1;i<n;i++)
{
rec=-1;
m=INF;
for(j=1;j<=n;j++)
if(!visit[j]&&dist[j]<m)
{
m=dist[j];
rec=j;
}
sum+=m;
visit[rec]=1;
for(j=1;j<=n;j++)
if(map[rec][j]<dist[j]&&!visit[j])
dist[j]=map[rec][j];
}
printf("%.2f\n",sum);
return 0;
}
Followed by:
Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator