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.

题目大意

平面上给出$n$个点,两两之间都有连边,一条边有两个权值:距离和高度差,求一个生成树使得$\frac{\sum dist_i}{\sum height_i}$最大

思路

『POJ2976』一样,判断时改成prim就可以了

/************************************************
*Author : lrj124
*Created Time : 2018.10.01.20:38
*Mail : 1584634848@qq.com
*Problem : poj2728
************************************************/
#include <cstdio>
#include <cmath>
using namespace std;
const int maxn = 1000 + 10;
double a[maxn][maxn],b[maxn][maxn],tmp[maxn][maxn],Min[maxn];
struct Node { int x,y,z; } p[maxn];
bool vis[maxn];
int n,e[maxn];
inline bool prim(double x) {
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++) tmp[i][j] = a[i][j]-x*b[i][j];
for (int i = 0;i <= n;i++) {
vis[i] = false;
Min[i] = 1000000000;
}
Min[1] = 0;
e[1] = 0;
double ans = 0;
for (int i = 1;i <= n;i++) {
int minnum = 0;
for (int j = 1;j <= n;j++)
if (Min[minnum] > Min[j] && !vis[j]) minnum = j;
vis[minnum] = true;
ans += tmp[e[minnum]][minnum];
for (int j = 1;j <= n;j++)
if (tmp[minnum][j] < Min[j] && !vis[j]) {
Min[j] = tmp[minnum][j];
e[j] = minnum;
}
}
return ans <= 0;
}
int main() {
//freopen("poj2728.in","r",stdin);
//freopen("poj2728.out","w",stdout);
while (scanf("%d",&n) , n) {
for (int i = 1;i <= n;i++) scanf("%d%d%d",&p[i].x,&p[i].y,&p[i].z);
for (int i = 1;i <= n;i++)
for (int j = 1;j <= n;j++) {
a[i][j] = fabs(p[i].z-p[j].z);
b[i][j] = sqrt((p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y));
}
double l = 0,r = 1000000000;
while (r-l >= 1e-6) {
double mid = (l+r)/2;
if (prim(mid)) r = mid;
else l = mid;
}
printf("%.3f\n",l);
}
return 0;
}

【POJ2728】Desert King - 01分数规划的更多相关文章

  1. poj2728 Desert King——01分数规划

    题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...

  2. poj2728 Desert King --- 01分数规划 二分水果。。

    这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...

  3. POJ 2728 Desert King (01分数规划)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 Descr ...

  4. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

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

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

  6. POJ 2728 Desert King | 01分数规划

    题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...

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

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

  8. Desert King (poj 2728 最优比率生成树 0-1分数规划)

    Language: Default Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22113   A ...

  9. Desert King(01分数规划问题)(最优斜率生成树)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:33847   Accepted: 9208 Descr ...

随机推荐

  1. Win7安装Python失败 提示Setup failed

    一.安装报错 如图所示,双击Python安装包后进行安装显示Setup failed 安装失败: 二.错误排除 1.首先查看自己的计算机是否已经安装了 Win7 Service Pack 1大补丁,没 ...

  2. jspang 做个那个pos系统--学习笔记

    /为什么不能使用Object.assign() //使用Object.assign之后数据会发生改变,但是试图没有跟新 <template> <div class="pos ...

  3. 用windbg查看dmp文件,定位bug位置

    windbg + .dmp + .pdb + 源代码,可以看到是哪个代码崩溃的 设置符号文件所在路径 File->Symbol File Path... 在输入框中填入.pdb文件所在的文件夹路 ...

  4. C++与正则表达式入门

    什么是正则表达式? 正则表达式是一组由字母和符号组成的特殊文本, 当你想要判断许多字符串是否符合某个特定格式:当你想在一大段文本中查找出所有的日期和时间:当你想要修改大量日志中所有的时间格式,在这些情 ...

  5. Python环境那点儿事(MAC篇)

    Python环境那点儿事(MAC篇) 解释器版本选择:(Python是解释型语言,相应的选择的就是解释器) 前言: 不管你是什么原因翻看此篇文章,强行安利一篇< 2018 Python官方年度报 ...

  6. npm ERR! Unexpected end of JSON input while

    rm -rf node_modules package-lock.json and npm cache clean --force solved it

  7. Oracle数据库出现[23000][2291] ORA-02291: integrity constraint (SIMTH.SYS_C005306) violated异常

    参考链接 这个异常发生在往中间表中插入数据时,这时出现异常是因为关联的某个表没有插入数据,所以给没有插入数据的关联表插入数据,再给中间表插入数据此时异常就会解决.

  8. HTML5+CSS3前端入门教程---从0开始通过一个商城实例手把手教你学习PC端和移动端页面开发第5章CSS盒子模型

    本教程案例在线演示 有路网PC端 有路网移动端 教程配套源码资源 教程配套源码资源 div div 可定义文档中的分区(division). div 标签可以把网页分割为独立的.不同的部分. 可以看成 ...

  9. 定义 WSGI 接口

    # WSGI服务器调用 def application(environ,start_response): start_response('200 OK',[('Content-Type','text/ ...

  10. PHP 类型比较

    PHP 类型比较 虽然 PHP 是弱类型语言,但也需要明白变量类型及它们的意义,因为我们经常需要对 PHP 变量进行比较,包含松散和严格比较. 松散比较:使用两个等号 == 比较,只比较值,不比较类型 ...