poj-2728Desert King(最优比率生成树)

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

题意:在这么一个图中求一棵生成树,这棵树点权和边权之比最大是多少?

   题解:枚举rate,然后来解最大生成树,就可以了,w[u]-line[i]*rate,这样来搞。
 #include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define N 1007
#define eps 0.000001
using namespace std; int n;
double dis[N][N],h[N][N],line[N],p[N][N];
bool vis[N];
struct Node
{
double x,y,h;
}a[N]; double get_dis(int x,int y)
{return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));}
/*struct cmp
{
bool operator()(int x,int y)
{return line[x]>line[y];}
};*/
double prim(double num)
{
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
p[i][j]=h[i][j]-dis[i][j]*num;
//priority_queue<int,vector<int>,cmp>q;
//while(!q.empty()) q.pop();
memset(vis,,sizeof(vis));
memset(line,,sizeof(line));
line[]=;
//for (int i=1;i<=n;i++) q.push(i);
for (int i=;i<=n;i++)
{
int u=;
for (int j=;j<=n;j++)
if (!vis[j]&&line[j]<line[u]) u=j;
vis[u]=;
for (int j=;j<=n;j++)
if (!vis[j]) line[j]=min(line[j],p[u][j]);
}
double sum=;
for (int i=;i<=n;i++)
sum+=line[i];
return sum;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++)
scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].h);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
dis[i][j]=get_dis(i,j);
h[i][j]=fabs(a[i].h-a[j].h);
}
double l=0.0,r=100.0;
while(r-l>=eps)
{
double mid=(l+r)*1.0/;
if (prim(mid)>=) l=mid;
else r=mid;
}
printf("%.3f\n",l);
}
}
上一篇:POJ 2728 Desert King (最优比率树)


下一篇:阿里云Centos 6.3 64位 安全加固版 升级 Php 中的 Curl 7.19 到 7.35