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-2728Desert King(最优比率生成树)的更多相关文章

  1. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  2. Desert King(最优比率生成树)

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

  3. 【POJ2728】Desert King 最优比率生成树

    题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...

  4. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  5. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  6. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  7. POJ2728 Desert King 最优比率生成树

    题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...

  8. POJ 2728(最优比率生成树+01规划)

                                                                                                    Dese ...

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

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

随机推荐

  1. sql 函数 coalesce

    SQL函数 coalesce 功能: 返回参数中第一个非null的值. 语法: coalesce(参数1,参数2,参数3,...);返回第一个非null的值. 一般情况下会与Nullif()函数一起使 ...

  2. codevs 2277 爱吃皮蛋的小明(水题日常)

    时间限制: 1 s  空间限制: 32000 KB  题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...

  3. 简单明了理解Java移位运算符

    无须多言: @Test public void intro() { assertThat("应该相等", -1 >> 1, equalTo(-1)); assertTh ...

  4. 转载:收费版APP三年总结(个人经验+数据图分享)

    各位朋友好,apop感觉这里的朋友有许多是以广告收入为主,所以apop来分享另外一块(收费版APP)的个人三年来的总结分享,希望对各位有帮助.首 先,其实在AppStore(或GooglePlay)上 ...

  5. 爬虫4_python2

    import urllib2 response = urllib2.urlopen("https://www.baidu.com") print response.read() 构 ...

  6. Bootstrap历练实例:输入框组的大小

    <!DOCTYPE html><html><head><meta http-equiv="Content-Type" content=&q ...

  7. mac拷贝原版和权限修复的命令行工具

    建议直接从安装盘中用命令复制,因为上传的kext权限会变,导致签名失败. 假定安装盘盘符是install_osx: sudo cp -R /Volumes/install_osx/S*/L*/E*/A ...

  8. 通过工厂模式批量创建对象后调用其中方法 出现XXXis not a function()问题原因

    //通过工厂模式批量创建 function Computer(color,weight,logo){         var obj=new Object();         obj.color=c ...

  9. curl学习笔记(以php为例)

    一.demo,抓取百度页码代码: $url = 'https://www.baidu.com/'; $ch = curl_init($url); curl_setopt($ch, CURLOPT_RE ...

  10. HTML 文件类表单元素如何限制上传类型,Accept属性设置

    需求描述:简单的控制file的选择类型 解决方法:使用HTML  input file 的accept属性控制 实例: <form action="demo_form.asp" ...