Online JudgeProblem SetAuthorsOnline ContestsUser
Web Board
Home Page
F.A.Qs
Statistical Charts
Problems
Submit Problem
Online Status
Prob.ID:
Register
Update your info
Authors ranklist
Current Contest
Past Contests
Scheduled Contests
Award Contest
User ID:
Password:
  Register

数据只能是float类型否则你自己看着办!

Posted by chenxuan123456789 at 2012-09-09 10:11:21 on Problem 2560
#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:
User ID:
Password:
Title:

Content:

Home Page   Go Back  To top


All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator