【POJ2728】Desert King - 01分数规划
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分数规划的更多相关文章
- poj2728 Desert King——01分数规划
题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...
- poj2728 Desert King --- 01分数规划 二分水果。。
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- POJ 2728 Desert King | 01分数规划
题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...
- Desert King (poj 2728 最优比率生成树 0-1分数规划)
Language: Default Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22113 A ...
- Desert King(01分数规划问题)(最优斜率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:33847 Accepted: 9208 Descr ...
随机推荐
- C++语法小记---面向对象模型(实例的内存分布)
面向对象的模型(内存分布) 对于一个对象而言,成员变量和成员函数是分开存放的 成员函数位于代码段,所有的类对象共有 成员变量为每一个对象独有,位于内存中 类对象在内存中的分布和struct完全相同 对 ...
- css控制div的各种形状
css控制div的各种形状 CSS3的一个非常酷的特性是允许我们创建各种规则和不规则形状的图形,从而可以减少图片的使用. 以前只能在Photoshop等图像编辑软件中制作的复杂图形现在使用CSS3就可 ...
- P4547 [THUWC2017]随机二分图(状压,期望DP)
期望好题. 发现 \(n\) 非常小,应该要想到状压的. 我们可以先只考虑 0 操作. 最难的还是状态: 我们用 \(S\) 表示左部点有哪些点已经有对应点, \(T\) 表示右部点有哪些点已经有对应 ...
- 一个有趣的问题, 你知道SqlDataAdapter中的Fill是怎么实现的吗
一:背景 1. 讲故事 最近因为各方面原因换了一份工作,去了一家主营物联柜的公司,有意思的是物联柜上的终端是用 wpf 写的,代码也算是年久失修,感觉技术债还是蛮重的,前几天在调试一个bug的时候,看 ...
- springcloud ribbon的 @LoadBalanced注解
在使用springcloud ribbon客户端负载均衡的时候,可以给RestTemplate bean 加一个@LoadBalanced注解,就能让这个RestTemplate在请求时拥有客户端负载 ...
- MySQL(一)简介与入门
一.数据库简介 这个博客详细介绍:http://www.cnblogs.com/progor/p/8729798.html 二.MySQL的安装 这个博客详细介绍:https://blog.csdn. ...
- netcore RabbitMQ入门--win10开发环境
安装 1.进入rabbitMQ官网下载安装包 2.点击安装包安装的时候会提示需要先装erlang 点击是会自动跳转到erlang的下载界面如果没有跳转可以直接点击这里下载,根据系统选择下载包 下载完之 ...
- dp入门例题(1)
按摩师问题 https://leetcode-cn.com/problems/the-masseuse-lcci/ (找好状态转移方程) 今天只和昨天的状态相关,依然是分类讨论: 今天不接受预约:或者 ...
- 02_HTML03
学于黑马和传智播客联合做的教学项目 感谢 黑马官网 传智播客官网 微信搜索"艺术行者",关注并回复关键词"软件测试"获取视频和教程资料! b站在线视频 HTML ...
- 在Windows上安装MySQL(转整)
MySQL安装 在Windows上安装MySQL.首先登录MySQL的官网下载安装包. 选择MySQL installer 这里选择第二个安装包下载即可. 下载完成之后就选择安装那个下载到的文件,基本 ...