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. 高效C++:资源管理

    C++中资源泄漏一直都是老大难的问题,特别是在嵌入式环境中,一点点的资源泄漏,加上长时间的运行们就会导致程序崩溃,这种问题定位非常困难,无规律偶发.解决问题的一种方式是使用特定工具检查内存泄漏,优点是 ...

  2. CppUnit使用和源码解析

    前言 CppUnit是一个开源的单元测试框架,支持Linux和Windows操作系统,在linux上可以直接进行源码编译,得到动态库和静态库,直接链接就可以正常使用,在Windows上可以使用VC直接 ...

  3. Linux中内存、CPU使用情况查看

    1.背景 在实际生产中我们为了保证系统能稳定运行,我们经常要查看当前的CPU和系统使用情况 建议使用top,简单丰富,快捷 2.使用free查看内存使用情况 3.使用 top查看内存.cpu内存占比 ...

  4. js获得url地址携带参数

    function GetQueryString(name) { var reg = new RegExp("(^|&)" + name + "=([^&] ...

  5. 使用 flask 构建我的 wooyun 漏洞知识库

    前言 最近在学 flask,一段时间没看,又忘得差不多了,于是弄这个来巩固一下基础知识 漏洞总共包括了 88820 个, Drops 文章总共有 1235 篇,全来自公开数据,在 Github 上收集 ...

  6. web自动化 -- 消息提示框处理 (alert、confirm、prompt)

    一.前提知识 1.警告消息框(alert) 警告消息框提供了一个"确定"按钮让用户关闭该消息框,并且该消息框是模式对话框,也就是说用户必须先关闭该消息框然后才能继续进行操作. 2. ...

  7. linux cp 拷贝不覆盖源文件

    cp 参数 CP() User Commands CP() NAME cp - copy files and directories SYNOPSIS cp [OPTION]... [-T] SOUR ...

  8. 深入理解Spring AOP 1.0

    本文相关代码(来自官方源码spring-test模块)请参见spring-demysify org.springframework.mylearntest包下. 统称能够实现AOP的语言为AOL,即( ...

  9. python学习笔记1 -- 函数式编程之高阶函数 使用函数作为返回值

    使用函数作为返回值,看起来就很高端有木有,前面了解过函数名本身就是一个变量,就比如abs()函数,abs只是变量名,而abs()才是函数调用,那么我们如果把ads这个变量作为返回值返回会怎么样呢,这就 ...

  10. 图解 JVM 核心知识点(面试版)

    一.基本概念 1.1 OpenJDK 自 1996 年 JDK 1.0 发布以来,Sun 公司在大版本上发行了 JDK 1.1.JDK 1.2.JDK 1.3.JDK 1.4.JDK 5,JDK 6 ...