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

给wa者的一点建议

Posted by hehexiaobai at 2010-08-23 15:27:06 on Problem 3625
如果你wa的话,是精度没控制好,输入是整数这是肯定的,在计算两点间的距离时

不妨把都转换成整数再计算。

prim能几十ms过,不要怀疑算法:原先一直wa,就是sqrt里少加了几个0.0转换成double。

最后贴下代码:
#include<stdio.h>
#include<cmath>
bool f[1001];
double dis[1001][1001];
double d[1001];
const double INF=1e30;
struct position
{
	int x, y;
}po[2001];

double caldistance(position &a , position &b)
{
	return sqrt((a.x-b.x+0.0)*(a.x-b.x+0.0)+(a.y-b.y+0.0)*(a.y-b.y+0.0)+0.0);
}

double prim(int n)
{
	double ans=0,min;
    int i,j,u;
	for( i=1; i<=n; i++)
			d[i]=dis[1][i];
	 d[1]=0;
	 f[1]=true;
	for(i=1; i<n; i++)
	{
		min=INF;
		for( j=1; j<=n; j++)
		{
			if(d[j]<min&&!f[j])
			{
				min=d[j];
				u=j;
			}
		}
		f[u]=1;
		ans+=min;
		
		for(j=1; j<=n; j++)
			if(!f[j]&&d[j]>dis[u][j])
				d[j]=dis[u][j];
	}

	return ans;
}

int main()
{
    int n,m,i,j;
	scanf("%d %d",&n,&m);

	for(i=1; i<=n; i++)
		scanf("%d %d",&po[i].x, &po[i].y);

	for(i=1; i<=n; i++)
		for(j=1; j<i; j++)
		{
			dis[i][j]=dis[j][i]=caldistance(po[i],po[j]);
		}
    
	int s,t;
    for(i=1; i<=m; i++)
	{
		scanf("%d %d",&s,&t);
		dis[s][t]=dis[t][s]=0;
	}

	double ans=prim(n);

	printf("%.2lf\n",ans);

	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