标签:define set ble const ons include index abs math
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 using namespace std; 7 #define inf 1e18 8 const int N=1000+7; 9 struct node{ 10 int x,y,z; 11 }num[N]; 12 double h[N][N],len[N][N],dis[N],g[N][N]; 13 bool vis[N]; 14 int n; 15 bool prim(double mid){ 16 memset(vis,0,sizeof(vis)); 17 double sum=0; 18 for(int i=1;i<=n;++i){ 19 for(int j=i+1;j<=n;++j){ 20 g[i][j]=g[j][i]=h[i][j]-mid*len[i][j]; 21 } 22 } 23 int all=1; 24 dis[1]=0;vis[1]=1; 25 for(int i=2;i<=n;++i) dis[i]=g[1][i]; 26 for(;all<n;){ 27 double minn=inf; 28 int index; 29 for(int i=1;i<=n;++i){ 30 if(vis[i]==0 && dis[i]<minn){ 31 minn=dis[i]; 32 index=i; 33 } 34 } 35 vis[index]=1; 36 ++all; 37 sum+=dis[index]; 38 for(int i=1;i<=n;++i) 39 if(vis[i]==0 && dis[i]>g[index][i]) 40 dis[i]=g[index][i]; 41 } 42 return sum<0.0; 43 } 44 double solve(int x1,int y1,int x2,int y2){ 45 double tem=(x1-x2)*(x1-x2)+(y1-y2)*(y1-y2); 46 return sqrt(tem); 47 } 48 int main(){ 49 while(~scanf("%d",&n)&&n){ 50 for(int i=1;i<=n;++i) 51 scanf("%d%d%d",&num[i].x,&num[i].y,&num[i].z); 52 for(int i=1;i<=n;++i){ 53 len[i][i]=h[i][i]=0; 54 for(int j=i+1;j<=n;++j){ 55 h[i][j]=h[j][i]=abs(num[i].z-num[j].z); 56 len[i][j]=len[j][i]=solve(num[i].x,num[i].y,num[j].x,num[j].y); 57 } 58 } 59 double l=0,r=100000.0; 60 while(r-l>1e-5){ 61 double mid=(l+r)/2; 62 if(prim(mid)) 63 r=mid;else l=mid; 64 } 65 printf("%.3lf\n",r); 66 } 67 return 0; 68 }
标签:define set ble const ons include index abs math
原文地址:https://www.cnblogs.com/xiaobuxie/p/11391883.html