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. scratch编程——画笔模块画各种同心图案

    我们今天是要用画笔来画出不同的同心图案,在画之前,我们先来了解一下画笔模块: 1.画笔模块的用法 画笔模块的用法就是在舞台上留下不同颜色粗细的线条,它的默认是情况是抬笔,我们在使用的时候要让角色移动到 ...

  2. 数据湖应用解析:Spark on Elasticsearch一致性问题

    摘要:脏数据对数据计算的正确性带来了很严重的影响.因此,我们需要探索一种方法,能够实现Spark写入Elasticsearch数据的可靠性与正确性. 概述 Spark与Elasticsearch(es ...

  3. 《java常用设计模式之----单例模式》

    一.简介 单例模式(Singleton Pattern)是 Java 中最简单的设计模式之一.这种类型的设计模式属于创建型模式,它提供了一种创建对象的最佳方式. 这种模式涉及到一个单一的类,该类负责创 ...

  4. 题解 洛谷 P4569 【[BJWC2011]禁忌】

    考虑用\(AC\)自动机来解决本题这样的多字符串匹配问题. 要最大化魔法分割后得到的禁忌串数目,最优情况肯定为在一个串中每个禁忌串的右端点进行分割.对应到\(AC\)自动机上,就是匹配到一个禁忌串后, ...

  5. vue学习(二) 三个指令v-cloak v-text v-html

    //style <style> [v-cloak]{ display:none } </style> //html <div id="app"> ...

  6. 如何消灭飞机的“黑色十分钟”,AI来帮忙

    近年来,“AI的应用和落地”逐渐成了具化的关键词,它和很多事物很多行业结合在一起,形成了奇妙的“化学反应”.例如,在日常生活中,AI可以推送我们喜欢的新闻或视频,可以在拍照的时候识别场景提升照片的美感 ...

  7. Spring葵花宝典

    一 Spring简介 Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架 为了解决企业应用开发的复杂性而创建 二 Spring功能 1. 方便解耦 简化开发 Spring就是一 ...

  8. 0.9循环=lim(n趋于无穷大)(1-1/10的n次方),所以这是一个极限问题

    0.9循环=lim(n趋于无穷大)(1-1/10的n次方),所以这是一个极限问题 因为lim(...)(1-1/10的n次方)=1 这意味着维尔斯特拉斯发明极限定义之前,这个等号是不成立的,因为没有极 ...

  9. Java中的锁机制

    1.在Java中锁的分类 其实就是按照锁的特性分类的 公平锁,非公平锁 可重入锁 独享锁,共享锁 互斥锁,读写锁 乐观锁,悲观锁 分段锁 偏向锁,轻量级锁,重量级锁 自旋锁 相关资料:思维导图 使用场 ...

  10. 什么是PHP 面向对象

    PHP 面向对象 在面向对象的程序设计(英语:Object-oriented programming,缩写:OOP)中,对象是一个由信息及对信息进行处理的描述所组成的整体,是对现实世界的抽象. 在现实 ...