Desert King(最优比率生成树)

Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 22717   Accepted: 6374

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
题解:先是超时,然后wa,最小生成树生疏了。。。
这个就是01分数规划的变形,即找到K求hi-li*K的最小生成树使得k最小;

有N个村庄,给出每个村庄的坐标和海拔,,benifit为两点之间的距离,cost为两点的高度差,现在要求一棵树使得 cost / benift 最小,即求一个最优比例生成树

第一次遇见这种生成树,在网上找了个解法

假设sigma(h)/sigma(l)==K 均值K可取,即: sigma(h)==K*sigma(l)

 sigma(h)==K*(l1+l2+l3+...lm)

 sigma(h)==K*l1+K*l2+K*l3+...K*lm

 把原来的每个边的h都减去K*l

 即hi'=hi-li'==hi-li*K

 然后问题可以转换到求hi'这些边的最小生成树了

如果hi'这些边得最小生成树权值和<=0.0,说明K这个均值可取

对于k,二分求解即可

代码:

 1 #include<cstdio>
 2 #include<iostream>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #define mem(x,y) memset(x,y,sizeof(x))
 7 using namespace std;
 8 const int INF=0x3f3f3f3f;
 9 typedef long long LL;
10 const int MAXN=1010;
11 double vis[MAXN],low[MAXN];
12 int N;
13 double R;
14 struct Node{
15     double x,y,h;
16 };
17 Node dt[MAXN];
18 double len[MAXN][MAXN],cost[MAXN][MAXN];
19 double getl(Node a,Node b){
20     double x=b.x-a.x,y=b.y-a.y;
21     return sqrt(x*x+y*y);
22 }
23 
24 bool prime(){
25     double total;
26     mem(vis,0);
27     for(int i=0;i<N;i++)low[i]=cost[0][i]-R*len[0][i];
28     total=0;
29     vis[0]=1;//0没有被标记为1。。。错了半天; 
30     for(int i=0;i<N;i++){
31         double temp=INF;
32         int k;
33         for(int j=0;j<N;j++)
34             if(!vis[j]&&low[j]<temp)temp=low[j],k=j;
35         if(temp==INF)break;
36         total+=temp;
37         vis[k]=1;
38         for(int j=0;j<N;j++)
39             if(!vis[j]&&low[j]>cost[k][j]-R*len[k][j])low[j]=cost[k][j]-R*len[k][j];
40     }
41     //printf("total=%lf R=%lf
",total,R);
42     if(total>0)return true;
43     else return false;
44 }
45 int main(){
46     while(scanf("%d",&N),N){
47         mem(len,INF);
48         mem(cost,INF);
49         double mxl=-INF,mil=INF,mxc=-INF,mic=INF;
50         for(int i=0;i<N;i++)
51             scanf("%lf%lf%lf",&dt[i].x,&dt[i].y,&dt[i].h);
52             
53         for(int i=0;i<N;i++){
54             for(int j=i+1;j<N;j++){
55                 len[j][i]=len[i][j]=getl(dt[i],dt[j]);
56                 cost[j][i]=cost[i][j]=abs(dt[i].h-dt[j].h);
57                 mxl=max(mxl,len[i][j]);
58                 mxc=max(mxc,cost[i][j]);
59                 mil=min(mil,len[i][j]);
60                 mic=min(mic,cost[i][j]);
61             }
62         }
63         //printf("%lf %lf %lf %lf
",mil,mic,mxl,mxc);
64         double l=mic/mxl,r=mxc/mil;//要是从0到mx会超时; 
65         
66     //    printf("%lf %lf
",l,r);
67         while(r-l>1e-4){
68             R=(l+r)/2;
69             if(prime())l=R;
70             else r=R;
71         }
72         printf("%.3f
",l);
73     }
74     return 0;
75 }
原文地址:https://www.cnblogs.com/handsomecui/p/4972701.html