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

郁闷啊!求距离调用一个POW函数竟然TLE了

Posted by thincal at 2004-09-26 21:36:38 on Problem 1841
TLE的代码:
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

#define MAXNODE 2000
#define MAXCOST 1.0e7

float gm[MAXNODE][MAXNODE];
float lowcost[MAXNODE];
float dis[MAXNODE];

int cmp(void const *p,void const *q)
{
	float temp=*(float *)p-*(float *)q;
 	if(temp==0)return 0;
	if(temp>0)return -1;
	return 1;
}

float Prim(int n,int m)
{
	float mincost;
	memset(dis,0,sizeof(dis));
	int i,j,k,count=0;
	for(i=1;i<n;i++)
		lowcost[i]=gm[0][i];
	lowcost[0]=0;
	
	for(i=1;i<n;i++){
		mincost=MAXCOST;
		j=1; k=1;
		for(j=1;j<n;j++){
			if(lowcost[j]<mincost && lowcost[j]!=0){
				mincost=lowcost[j];
				k=j;
			}
		}
		dis[count++]=lowcost[k];
		lowcost[k]=0;
		for(j=1;j<n;j++)
			if(gm[k][j]<lowcost[j])
				lowcost[j]=gm[k][j];
	}
	qsort(dis,count,sizeof(float),cmp);
	return dis[m-1];
}

int main()
{
    int i,j;
	int F,B;
	int buf[MAXNODE][2];
	while(cin>>F>>B){		
		memset(gm,0,sizeof(gm));
		for(i=0;i<F;i++)
			cin>>buf[i][0]>>buf[i][1];
		for(i=0;i<F-1;i++)
			for(j=i;j<F;j++){
				gm[i][j]=sqrt(pow(buf[i][0]-buf[j][0],2)+pow(buf[i][1]-buf[j][1],2));
				gm[j][i]=gm[i][j];
			}
		printf("%.2f\n",Prim(F,B));
	}
	return 0;
}

AC的代码:
#include<stdio.h>
#include<iostream.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>

#define MAXNODE 2000
#define MAXCOST 1.0e7

float gm[MAXNODE][MAXNODE];
float lowcost[MAXNODE];
float dis[MAXNODE];

int cmp(void const *p,void const *q)
{
	float temp=*(float *)p-*(float *)q;
 	if(temp==0)return 0;
	if(temp>0)return -1;
	return 1;
}

float Prim(int n,int m)
{
	float mincost;
	memset(dis,0,sizeof(dis));
	int i,j,k,count=0;
	for(i=1;i<n;i++)
		lowcost[i]=gm[0][i];
	lowcost[0]=0;
	
	for(i=1;i<n;i++){
		mincost=MAXCOST;
		j=1; k=1;
		for(j=1;j<n;j++){
			if(lowcost[j]<mincost && lowcost[j]!=0){
				mincost=lowcost[j];
				k=j;
			}
		}
		dis[count++]=lowcost[k];
		lowcost[k]=0;
		for(j=1;j<n;j++)
			if(gm[k][j]<lowcost[j])
				lowcost[j]=gm[k][j];
	}
	qsort(dis,count,sizeof(float),cmp);
	return dis[m-1];
}

int main()
{
    int i,j;
	int F,B;
	int buf[MAXNODE][2];
	while(cin>>F>>B){		
		memset(gm,0,sizeof(gm));
		for(i=0;i<F;i++)
			cin>>buf[i][0]>>buf[i][1];
		for(i=0;i<F-1;i++)
			for(j=i;j<F;j++){
				gm[i][j]=sqrt((buf[i][0]-buf[j][0])*(buf[i][0]-buf[j][0])+(buf[i][1]-buf[j][1])*(buf[i][1]-buf[j][1]));
				gm[j][i]=gm[i][j];
			}
		printf("%.2f\n",Prim(F,B));
	}
	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