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. WINDOWS-基础:_T

    _T("")是一个宏,定义于tchar.h下. #define __T(x) L ## x #define _T(x) __T(x) 作用 他的作用是让你的程序支持Unicode编 ...

  2. nyoj-1103-区域赛系列一多边形划分

    http://acm.nyist.net/JudgeOnline/problem.php?pid=1103 区域赛系列一多边形划分 时间限制:1000 ms  |  内存限制:65535 KB 难度: ...

  3. shell脚本,awk实现每个数字加1.

    [root@localhost add]# cat file [root@localhost add]# cat file|awk '{for(i=1;i<=NF;i++){$i+=1}}1' ...

  4. 摘抄 Promise原理

    1.简单的promise: //极简promise雏形 function Promise(fn){ var value = null; callbacks = [];//callback为数组,因为可 ...

  5. Luogu P2664 树上游戏 dfs+树上统计

    题目: P2664 树上游戏 分析: 本来是练习点分治的时候看到了这道题.无意中发现题解中有一种方法可以O(N)解决这道题,就去膜拜了一下. 这个方法是,假如对于某一种颜色,将所有这种颜色的点全部删去 ...

  6. hihoCoder第一周---最长回文子串(1032)

    其实这就是mancher算法的板子题,贴个代码好了. 思想请见我的另一篇博客: https://blog.csdn.net/qq_41090676/article/details/86768361 # ...

  7. mysql delete 表无法用别名

    delete from exam_paper_question_opt a WHERE a.OPTION_ID = 65630 and a.QUESTION_ID = 28656 AND a.EXAM ...

  8. (8)zabbix监控项item是什么

    什么是item Items是从主机里面获取的所有数据.通常情况下我叫itme为监控项,例如服务器加入了zabbix监控,我需要监控它的cpu负载,那么实现这个方法的东西就叫item item构成 it ...

  9. Linux基础学习-Postfix与Dovecot部署邮件系统

    电子邮件系统 电子邮件系统是我们在日常工作.生活中最常用的一种网络服务. 部署基础的电子邮件系统 [root@qdlinux ~]# yum install bind-chroot -y [root@ ...

  10. perl学习之:localtime

    Perl中localtime()函数以及sprintf (2011-4-25 19:39)localtime函数 localtime函数,根据它所在的上下文,可以用两种完全不同的方法来运行.在标量上下 ...