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 |
贴一下AC代码 313ms看了网上的思路,自己独自去写的。 #include <stdio.h> #include <math.h> #define MAXN 1001 #define To2(x) (x)*(x) const double eps=1e-4; typedef struct { int x,y,height; }Point; typedef struct { double distance,cost,weight; }Graph; Point vex[MAXN]; Graph G[MAXN][MAXN],dis[MAXN]; int vexnum,visited[MAXN]; double fach(int i,int j) { return sqrt((double)(To2(vex[i].x-vex[j].x)+To2(vex[i].y-vex[j].y))); } void Initial(int N) { int i,j; vexnum=N; for(i=1;i<=N;++i) { for(j=1;j<=i;++j) { G[i][j].cost=G[j][i].cost=fabs((double)vex[i].height-vex[j].height); G[i][j].distance=G[j][i].distance=fach(i,j); } } } int FindMin() { int i,k=-1; double min=1000000; for(i=1;i<=vexnum;++i) if(!visited[i]&&dis[i].weight<min) k=i,min=dis[i].weight; return k; } double Prim(int k,double rate) { int i,j; double Dissum=0,Costsum=0; for(i=1;i<=vexnum;++i) for(j=1;j<=i;++j) G[i][j].weight=G[j][i].weight=(double)G[i][j].cost-rate*G[i][j].distance; for(i=1;i<=vexnum;++i) dis[i]=G[k][i],visited[i]=0; dis[k].weight=0,visited[k]=1; for(i=1;i<vexnum;++i) { k=FindMin(); if(k==-1) break; visited[k]=1; Dissum+=dis[k].distance; Costsum+=dis[k].cost; for(j=1;j<=vexnum;++j) { if(!visited[j]&&dis[j].weight>G[k][j].weight) { dis[j].cost=G[k][j].cost; dis[j].distance=G[k][j].distance; dis[j].weight=G[k][j].weight; } } } return (double)Costsum/Dissum; } int main() { int N,i; int a,b,c; double rate,rateNex; while(scanf("%d",&N)&&N!=0) { for(i=1;i<=N;++i) scanf("%d %d %d",&vex[i].x,&vex[i].y,&vex[i].height); Initial(N); rate=0; while(1) { rateNex=Prim(1,rate); if(fabs((double)rate-rateNex)<eps) break; rate=rateNex; } printf("%.3lf\n",rateNex); } return 0; } Followed by: Post your reply here: |
All Rights Reserved 2003-2013 Ying Fuchen,Xu Pengcheng,Xie Di
Any problem, Please Contact Administrator