Desert King 最小比率生成树 (好题)
Description
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
Output
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 最小比率生成树 (好题)的更多相关文章
- poj 2728 Desert King(最小比率生成树,迭代法)
引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求 ...
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- POJ2728 最小比率生成树/0-1分数规划/二分/迭代(迭代不会)
用01分数规划 + prime + 二分 竟然2950MS惊险的过了QAQ 前提是在TLE了好几次下过的 = = 题目意思:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一 ...
- poj2728 最小比率生成树——01分数规划
题目大意: 有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水, 只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差, 现在要求方案使得费用与距离的比值最小,很显然 ...
- 【bzoj2429】[HAOI2006]聪明的猴子(图论--最小瓶颈生成树 模版题)
题意:有M只猴子,他们的最大跳跃距离为Ai.树林中有N棵树露出了水面,给出了它们的坐标.问有多少只猴子能在这个地区露出水面的所有树冠上觅食. 解法:由于要尽量多的猴子能到达所有树冠,便用Kruskal ...
- poj2728(最小比率生成树)
poj2728 题意 给出 n 个点的坐标和它的高度,求一颗生成树使得树上所连边的两点高度差之和除以距离之和最小. 分析 01分数规划+最小生成树. 对于所有的边,在求最小生成树过程中有选或不选的问题 ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...
- Desert King
poj2728:http://poj.org/problem?id=2728 题意:给你n的点,每一个点会有一个坐标(x,y),然后还有一个z值,现在上你求一棵生成树,是的这棵生成树的所有边的费用/所 ...
随机推荐
- TW实习日记:第11、12天
这两天其实都在做一件事,项目组组长丢了个需求下来,要求完成一个百度地图api的页面.原本以为和之前写微信接口的类似,没想到这次问题这么多.并且在写代码的时候和组长交流不畅导致心情也很差,深刻的反思了一 ...
- JSP页面无法使用EL导致"java.sql.SQLException: No suitable driver found for ${snapshot}"的问题
使用JSTL来连接mysql,这个问题折腾了半天,老以为是Mysql驱动的问题,还好最后偶然发现了是EL表达式识别不了,报错: javax.servlet.ServletException: java ...
- ubuntu server guide 学习笔记
1. 软件包 1.1. dpkg dpkg -l dpkg -l | grep apache2 dpkg -L ufw dpkg -S /etc/host.conf dpkg -i zip_3.0-4 ...
- Map Reduce Application(Partitioninig/Binning)
Map Reduce Application(Partitioninig/Group data by a defined key) Assuming we want to group data by ...
- 以太坊开发(二)使用Ganache CLI在私有链上搭建智能合约
以太坊开发(二)使用Ganache CLI在私有链上搭建智能合约 在上一篇文章中,我们使用Truffle自带的客户端Truffle Develop,在私有链上搭建并运行了官方提供的WebPack智能合 ...
- Linux下安装paramiko
paramiko是用python语言写的一个模块,遵循SSH2协议,支持以加密和认证的方式,进行远程服务器的连接. 由于使用的是python这样的能够跨平台运行的语言,所以所有python支持的平台, ...
- vue移动音乐app开发学习(一):环境搭建
本系列文章是为了记录学习中的知识点,便于后期自己观看.如果有需要的同学请登录慕课网,找到Vue 2.0 高级实战-开发移动端音乐WebApp进行观看,传送门. 一:使用vue-cli脚手架搭建: 1: ...
- Java学习个人备忘录之数组
数组 概念:同一种类型数据的集合,其实数组就是一个容器. 数组的好处:可以自动给数组中的元素从0开始编号,方便操作这些元素. 格式1:元素类型[] 数组名 = new 元素类型[元素个数]; 格式2: ...
- 《梦断代码Dreaming In Code》阅读笔记(二)
这段时间一口气读了5章,所以想着现在一块写阅读笔记. 在阅读的这段时间,我一直是晚上进行阅读,很多时候都是读完一看已经一个小时了,效果还不错.闲话不表,说说阅读心得. 关于底层语言或是低级语言,我之前 ...
- JavaScript初探系列之Ajax应用
一 什么是Ajax Ajax是(Asynchronous JavaScript And XML)是异步的JavaScript和xml.也就是异步请求更新技术.Ajax是一种对现有技术的一种新的应用,不 ...