首页 > 代码库 > POJ 2728 Desert King 最优比例生成树
POJ 2728 Desert King 最优比例生成树
最优比例生成树,迭代方法.....
Desert King
Description David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way. After days of study, he finally figured his plan out. He wanted the average cost of each mile of the channels to be minimized. In other words, the ratio of the overall cost of the channels to the total length must be minimized. He just needs to build the necessary channels to bring water to all the villages, which means there will be only one way to connect each village to the capital. His engineers surveyed the country and recorded the position and altitude of each village. All the channels must go straight between two villages and be built horizontally. Since every two villages are at different altitudes, they concluded that each channel between two villages needed a vertical water lifter, which can lift water up or let water flow down. The length of the channel is the horizontal distance between the two villages. The cost of the channel is the height of the lifter. You should notice that each village is at a different altitude, and different channels can‘t share a lifter. Channels can intersect safely and no three villages are on the same line. As King David‘s prime scientist and programmer, you are asked to find out the best solution to build the channels. Input There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed. Output For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point. Sample Input 4 0 0 0 0 1 1 1 1 2 1 0 3 0 Sample Output 1.000 Source Beijing 2005 |
[Submit] [Go Back] [Status] [Discuss]
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int maxn=1100; const double eps=1e-6; int n; struct Town { int x,y,h; }T[maxn]; double dist[maxn][maxn],cost[maxn][maxn]; double Dist(int a,int b) { return sqrt(1.*(T[a].x-T[b].x)*(T[a].x-T[b].x)+1.*(T[a].y-T[b].y)*(T[a].y-T[b].y)); } double mp[maxn][maxn]; double d[maxn]; bool vis[maxn]; int pre[maxn]; double prime(double r) { ///.....rebuild............... for(int i=1;i<=n;i++) { pre[i]=-1; mp[i][i]=0.0; for(int j=i+1;j<=n;j++) { mp[i][j]=mp[j][i]=cost[i][j]-r*dist[i][j]; } } ///............................ for(int i=1;i<=n;i++) d[i]=1e30,vis[i]=false; d[1]=0; for(int i=1;i<=n;i++) { double mindist=1e30; int mark=-1; for(int j=1;j<=n;j++) { if(!vis[j]&&d[j]<mindist) { mindist=d[j]; mark=j; } } vis[mark]=true; for(int j=1;j<=n;j++) { if(!vis[j]&&mp[mark][j]<d[j]) { pre[j]=mark; d[j]=mp[mark][j]; } } } double p=0.,q=0.; for(int i=2;i<=n;i++) { int from=pre[i],to=i; p+=cost[from][to]; q+=dist[from][to]; } return p/q; } int main() { while(scanf("%d",&n)!=EOF&&n) { for(int i=1;i<=n;i++) { int a,b,c; scanf("%d%d%d",&a,&b,&c); //T[i]=(Town){a,b,c}; T[i].x=a; T[i].y=b; T[i].h=c; } for(int i=1;i<=n;i++) { dist[i][i]=cost[i][i]=0.0; for(int j=i+1;j<=n;j++) { dist[i][j]=dist[j][i]=Dist(i,j); cost[i][j]=cost[j][i]=fabs(1.*T[i].h-T[j].h); } } double r1=0.,r2=0.; while(true) { r2=prime(r1); if(fabs(r1-r2)<eps) break; r1=r2; } printf("%.3lf\n",r1); } return 0; }
POJ 2728 Desert King 最优比例生成树
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。