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 |
郁闷啊!求距离调用一个POW函数竟然TLE了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: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator