The coordinates (x, y, z) of each vertex are given. The distance between the two points is the linear distance between x and y, and the edge weight is the Z difference, calculate the minimum value of the Σ Edge Weight/Σ distance.
Optimal Rate generation tree! (Score Planning)
It is based on the idea of score planning, and the sum shown each time is exactly negative.
Binary Code:
#include<string.h>#include<stdio.h>#include<stdlib.h>#include<math.h>#define N 1010typedef struct KSD{int x,y,z;}ksd;typedef struct LEONA{int cost;double len,rate;}leona;ksd s[N];leona e[N][N];double dist[N];int v[N],n;int jkl(double ans){int i,j,k;dist[1]=0;memset(v,0,sizeof(v));for(i=2;i<=n;i++){dist[i]=9999999.9;}for(i=1;i<=n;i++){for(j=1;j<=n;j++){e[i][j].rate=e[i][j].cost-ans*e[i][j].len;}}for(ans=0,j=1;j<=n;j++){int lord;double evil=9999999.9;for(i=1;i<=n;i++){if(v[i]==0&&dist[i]<evil){lord=i;evil=dist[i];}}v[lord]=1;ans+=evil;for(i=1;i<=n;i++){if(v[i]==0&&dist[i]-0.000001>e[lord][i].rate){dist[i]=e[lord][i].rate;}}}return ans-0.0000001<0?1:0;}int main(){int i,j,k;double l,r,mid;while(scanf("%d",&n),n){for(i=1;i<=n;i++){scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);}for(i=1;i<=n;i++){for(j=1;j<=n;j++){e[i][j].cost=abs(s[i].z-s[j].z);e[i][j].len=sqrt((double)(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y));}}l=0.0;r=100.0;for(i=1;i<=25;i++){mid=(l+r)/2;if(jkl(mid)){r=mid+0.0000001;}else{l=mid;}}printf("%.3lf\n",mid);}return 0;}
Iteration code:
#include<string.h>#include<stdio.h>#include<stdlib.h>#include<math.h>#define N 1010typedef struct KSD{int x,y,z;}ksd;typedef struct LEONA{int cost;double len,rate;}leona;ksd s[N];leona e[N][N];double dist[N];int v[N],f[N],n;double jkl(double ans){int i,j,k;double maimeng,mai_xie;maimeng=mai_xie=0;dist[1]=0;memset(v,0,sizeof(v));for(i=2;i<=n;i++){dist[i]=9999999.9;}for(i=1;i<=n;i++){for(j=1;j<=n;j++){e[i][j].rate=e[i][j].cost-ans*e[i][j].len;}}for(j=1;j<=n;j++){int lord;double evil=9999999.9;for(i=1;i<=n;i++){if(v[i]==0&&dist[i]<evil){lord=i;evil=dist[i];}}v[lord]=1;maimeng+=e[f[lord]][lord].cost;mai_xie+=e[f[lord]][lord].len;for(i=1;i<=n;i++){if(v[i]==0&&dist[i]-0.000001>e[lord][i].rate){dist[i]=e[lord][i].rate;f[i]=lord;}}}ans=maimeng/mai_xie;return ans;}int main(){//freopen("test.in","r",stdin);int i,j,k;double l,r,mid;while(scanf("%d",&n),n){for(i=1;i<=n;i++){scanf("%d%d%d",&s[i].x,&s[i].y,&s[i].z);}for(i=1;i<=n;i++){for(j=1;j<=n;j++){e[i][j].cost=abs(s[i].z-s[j].z);e[i][j].len=sqrt((double)(s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y));}}r=100.0;for(i=1;i<=25;i++){r=jkl(r);}printf("%.3lf\n",r);}return 0;}
13250412 |
18357 |
2728 |
Accepted |
23944 K |
1704 Ms |
C ++ |
1468b |
15:59:04 |
13249973 |
18357 |
2728 |
Accepted |
23948 K |
1672 Ms |
C ++ |
1406b |
15:19:12 |
The above is an iteration, and below is a binary classification. Maybe it's a waste? Iteration is actually slower than binary.
Copy the Translation results to Google
[Poj] [2728] desert King optimal rate Spanning Tree