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,二分求解即可

代码:

 #include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#define mem(x,y) memset(x,y,sizeof(x))
using namespace std;
const int INF=0x3f3f3f3f;
typedef long long LL;
const int MAXN=;
double vis[MAXN],low[MAXN];
int N;
double R;
struct Node{
double x,y,h;
};
Node dt[MAXN];
double len[MAXN][MAXN],cost[MAXN][MAXN];
double getl(Node a,Node b){
double x=b.x-a.x,y=b.y-a.y;
return sqrt(x*x+y*y);
} bool prime(){
double total;
mem(vis,);
for(int i=;i<N;i++)low[i]=cost[][i]-R*len[][i];
total=;
vis[]=;//0没有被标记为1。。。错了半天;
for(int i=;i<N;i++){
double temp=INF;
int k;
for(int j=;j<N;j++)
if(!vis[j]&&low[j]<temp)temp=low[j],k=j;
if(temp==INF)break;
total+=temp;
vis[k]=;
for(int j=;j<N;j++)
if(!vis[j]&&low[j]>cost[k][j]-R*len[k][j])low[j]=cost[k][j]-R*len[k][j];
}
//printf("total=%lf R=%lf\n",total,R);
if(total>)return true;
else return false;
}
int main(){
while(scanf("%d",&N),N){
mem(len,INF);
mem(cost,INF);
double mxl=-INF,mil=INF,mxc=-INF,mic=INF;
for(int i=;i<N;i++)
scanf("%lf%lf%lf",&dt[i].x,&dt[i].y,&dt[i].h); for(int i=;i<N;i++){
for(int j=i+;j<N;j++){
len[j][i]=len[i][j]=getl(dt[i],dt[j]);
cost[j][i]=cost[i][j]=abs(dt[i].h-dt[j].h);
mxl=max(mxl,len[i][j]);
mxc=max(mxc,cost[i][j]);
mil=min(mil,len[i][j]);
mic=min(mic,cost[i][j]);
}
}
//printf("%lf %lf %lf %lf\n",mil,mic,mxl,mxc);
double l=mic/mxl,r=mxc/mil;//要是从0到mx会超时; // printf("%lf %lf\n",l,r);
while(r-l>1e-){
R=(l+r)/;
if(prime())l=R;
else r=R;
}
printf("%.3f\n",l);
}
return ;
}
上一篇:win7系统开机后电脑桌面背景变黑的解决方法


下一篇:解决COM组件在WPF设计器中命名空间不存在XXX的问题(附带如何在WPF中使用APlayer引擎)