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

https://blog.csdn.net/guozizheng001/article/details/51044710
我看这个博客学的讲的特别好
 #include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <ctype.h>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <iostream>
using namespace std;
#define bug printf("******\n");
const int maxn = 1e6 + ;
#define rtl rt<<1
#define rtr rt<<1|1
const double eps=1e-;
int n,vis[];
double mp[][],p[];
struct node {
double x,y,z;
}a[];
double cost(double mid,int i,int j){
return mid*mp[i][j]-1.0*(abs(a[i].z-a[j].z));
}
int pri(double mid) {
for (int i= ;i<=n ;i++) vis[i]=,p[i]=-;
p[]=;
double ret=;
for (int i= ;i<n ;i++ ) {
int idx=-;
double maxx=-110000000.0;
for (int j= ;j<n ;j++){
if (vis[j]) continue;
if (p[j]>maxx) {
maxx=p[j];
idx=j;
}
}
if (idx==-) break;
vis[idx]=;
ret+=maxx;
for (int j= ;j<n ;j++) {
if (vis[j]) continue;
p[j]=max(p[j],cost(mid,idx,j));
}
}
if (ret>eps) return ;
return ;
}
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].z);
for (int i= ;i<n ;i++)
for (int j=i ;j<n ;j++)
mp[i][j]=mp[j][i]=sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)+(a[i].y-a[j].y)*(a[i].y-a[j].y));
double high=,low=,mid;
int m=;
while(m--){
mid=(low+high)/;
if (pri(mid)) high=mid;
else low=mid;
}
printf("%.3f\n",low);
}
return ;
}

Desert King 最小比率生成树 (好题)的更多相关文章

  1. poj 2728 Desert King(最小比率生成树,迭代法)

    引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求 ...

  2. poj 2728 Desert King (最小比例生成树)

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

  3. POJ2728 最小比率生成树/0-1分数规划/二分/迭代(迭代不会)

    用01分数规划 + prime + 二分 竟然2950MS惊险的过了QAQ 前提是在TLE了好几次下过的 = = 题目意思:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一 ...

  4. poj2728 最小比率生成树——01分数规划

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

  5. 【bzoj2429】[HAOI2006]聪明的猴子(图论--最小瓶颈生成树 模版题)

    题意:有M只猴子,他们的最大跳跃距离为Ai.树林中有N棵树露出了水面,给出了它们的坐标.问有多少只猴子能在这个地区露出水面的所有树冠上觅食. 解法:由于要尽量多的猴子能到达所有树冠,便用Kruskal ...

  6. poj2728(最小比率生成树)

    poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...

  7. poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)

    题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...

  8. 【POJ2728】Desert King(分数规划)

    [POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...

  9. Desert King

    poj2728:http://poj.org/problem?id=2728 题意:给你n的点,每一个点会有一个坐标(x,y),然后还有一个z值,现在上你求一棵生成树,是的这棵生成树的所有边的费用/所 ...

随机推荐

  1. 孤荷凌寒自学python第八十一天学习爬取图片1

    孤荷凌寒自学python第八十一天学习爬取图片1 (完整学习过程屏幕记录视频地址在文末) 通过前面十天的学习,我已经基本了解了通过requests模块来与网站服务器进行交互的方法,也知道了Beauti ...

  2. 《Effective C++》读书笔记 被你忽略的关于构造析构赋值

    如果程序员没有定义,那么编译器会默认隐式为你创建一个copy构造函数,一个copy赋值操作符,一个析构函数.另外如果你没有声明任何构造函数,编译器会为你声明一个default构造函数. 但是只有当这些 ...

  3. Office 365 E3功能

    本文简要总结了Office 365E3的功能

  4. [2018 ACL Long] 对话系统

    [NLG - E2E - knowledge guide generation] 1. Knowledge Diffusion for Neural Dialogue Generation ( ‎Ci ...

  5. 【转】Expressions versus statements in JavaScript

    原文地址:http://www.2ality.com/2012/09/expressions-vs-statements.html Update 2012-09-21: New in Sect. 4: ...

  6. activiti工作流已办和待办查询sql

    最近项目中遇到一个问题,需要activiti的工作流表和业务表关联分页查询,然而我对于工作流的查询并不太熟悉,所以学习并总结如下. 想看看activiti到底怎么查询的待认领和待办.已办的查询sql, ...

  7. 福大软工1816:Alpha(10/10)

    Alpha 冲刺 (10/10) 队名:第三视角 组长博客链接 本次作业链接 团队部分 团队燃尽图 工作情况汇报 张扬(组长) 过去两天完成了哪些任务: 文字/口头描述: 1.和愈明.韫月一起对接 2 ...

  8. Jmeter系列-webdriver插件

    1.下载地址    JMeterPlugins-WebDriver-1.1.2 2.将JMeterPlugins-WebDriver-1.1.2\lib\ext中的*.jar拷贝到D:\apache- ...

  9. bpf移植到3.10

    bpf_common.h中显示的是/usr/src/linux-headersXXXX/include/uapi/linux 竟然会识别系统的挂载选项:

  10. perf 是怎么计算调用栈的时间的?

    在我真个malloc的执行过程中共调用了8次的syswrite的系统调用,其中有两次来自于__lib_write, 两次来自于__memmove_avx_unaligned,然后__memmove_a ...