Desert King
Time Limit: 3000MS   Memory Limit: 65536K
     

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

Source

 
题目大意:构建最优比率生成树
每条边的花费=连接两点的高度差,距离=平面两点间距离(官方名称:欧几里得距离)
最小化 ∑ 每条边的花费/距离
01分数规划+prim
prim构造最小生成树的标准是 w=这条边的花费-这条边的距离*二分的mid
最后判断选用边的w是否大于0
01分数规划就用在这里
然而初学,并没有想到,一直在思考怎么在prim过程中套01规划
再就是判断式子>0,移动下界,否则移动上界
这是固定的,与最终求最大最小值没有关系
受了刚做的一道01规划题影响http://www.cnblogs.com/TheRoadToTheGold/p/6546981.html
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-6
using namespace std;
int n;
double l,r,mid,ans,p;
double x[],y[],h[],dis[][],cost[][],minn[],w[][];
bool v[];
bool check(double k)
{
p=;
memset(v,,sizeof(v));
for(int i=;i<=n;i++) minn[i]=w[][i];
minn[]=;v[]=true;
int s=n-;
while(s--)
{
int point;double d=;
for(int i=;i<=n;i++)
if(!v[i]&&minn[i]<d)
{
point=i;d=minn[i];
}
p+=d;v[point]=true;
for(int i=;i<=n;i++)
if(!v[i]&&w[point][i]<minn[i])
minn[i]=w[point][i];
}
return p>=;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(!n) return ;
for(int i=;i<=n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&h[i]);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
dis[i][j]=sqrt(pow(abs(x[i]-x[j]),)+pow(abs(y[i]-y[j]),));
cost[i][j]=abs(h[i]-h[j]);
}
l=,r=;ans=;
while(fabs(l-r)>eps)
{
mid=(l+r)/;
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
w[i][j]=w[j][i]=cost[i][j]-mid*dis[i][j];
if(check(mid))
{
ans=mid;
l=mid+eps;
}
else r=mid-eps;
}
printf("%.3lf\n",ans);
} }

poj 2728 Desert King (最优比率生成树)的更多相关文章

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

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

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

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

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

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

  4. POJ 2728 Desert King (最优比率树)

    题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...

  5. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

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

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

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

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

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

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

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

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

随机推荐

  1. 使用成员资格管理用户Membership

    ASP.NET成员资格使您可以验证和管理Web应用程序的用户信息.它提供验证用户凭据,创建和修改成员资格用户以及管理用户设置(如密码和电子邮件地址)的功能. ASP.NET成员资格主要用于ASP.NE ...

  2. 饭来了小组Alpha冲刺阶段记录

    一.第一天 日期:2018/6/13 1.1今日完成任务情况以及遇到的问题 侯晓东: 1.完成任务项:具体了解了微信小程序的开发流程,然后大致规划了我们项目的进度和完成节点:汇总组员的进度,写博客:画 ...

  3. just_sort

    ★实验任务 给定两个序列 a b,序列 a 原先是一个单调递增的正数序列,但是由于某些 原因,使得序列乱序了,并且一些数丢失了(用 0 表示).经过数据恢复后,找 到了正数序列 b ,且序列 a 中 ...

  4. vue 实战报错解决方案

    最近做项目,遇到一个问题 列表滚动,上拉加载功能 采用了better-scroll 插件,将better-scroll 封装成组件,采用父组件传递值给子组件的方式,子组件 采用$emit 方式 通知父 ...

  5. Mongodb compass 介绍

    参考官方文档:https://docs.mongodb.com/compass/current/install/#install-on-red-hat-enterprise-linux-rhel Mo ...

  6. [2017BUAA软工]第1次个人作业

    软工第1次个人作业 一.快速看完整部教材,列出你不懂的5-10个问题,发布在你的个人博客上. 1.文中提到"积累问题领域的知识和经验(例如:对医疗或金融行业的了解)."然而我们如何 ...

  7. [转帖]SSD的工作原理、GC和TRIM、写入放大以及性能评测

    SSD的工作原理.GC和TRIM.写入放大以及性能评测 https://blog.csdn.net/scaleqiao/article/details/50511279   SSD的物理结构和工作原理 ...

  8. [OS] 内核态和用户态的区别

    http://blog.csdn.net/fatsandwich/article/details/2131707# http://jakielong.iteye.com/blog/771663 当一个 ...

  9. Ubuntu 12.04下LAMP环境搭建实录

    方法一: 用tasksel可以方便安装dns server, lamp, kubuntu desktop, ubuntu desktop, xubuntu之类的软件包.这个软件在ubuntu serv ...

  10. php 关于文件的一些封装好的函数

    <?php //Bytes/Kb/MB/GB/TB/EB /** * 转换字节大小 * @param number $size * @return number */ function tran ...