poj 2728 Desert King (最优比率生成树)
Time Limit: 3000MS | Memory Limit: 65536K | |
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
Source
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
#define eps 1e-6
using namespace std;
int n;
double l,r,mid,ans,p;
double x[],y[],h[],dis[][],cost[][],minn[],w[][];
bool v[];
bool check(double k)
{
p=;
memset(v,,sizeof(v));
for(int i=;i<=n;i++) minn[i]=w[][i];
minn[]=;v[]=true;
int s=n-;
while(s--)
{
int point;double d=;
for(int i=;i<=n;i++)
if(!v[i]&&minn[i]<d)
{
point=i;d=minn[i];
}
p+=d;v[point]=true;
for(int i=;i<=n;i++)
if(!v[i]&&w[point][i]<minn[i])
minn[i]=w[point][i];
}
return p>=;
}
int main()
{
while(scanf("%d",&n)!=EOF)
{
if(!n) return ;
for(int i=;i<=n;i++) scanf("%lf%lf%lf",&x[i],&y[i],&h[i]);
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
{
dis[i][j]=sqrt(pow(abs(x[i]-x[j]),)+pow(abs(y[i]-y[j]),));
cost[i][j]=abs(h[i]-h[j]);
}
l=,r=;ans=;
while(fabs(l-r)>eps)
{
mid=(l+r)/;
for(int i=;i<n;i++)
for(int j=i+;j<=n;j++)
w[i][j]=w[j][i]=cost[i][j]-mid*dis[i][j];
if(check(mid))
{
ans=mid;
l=mid+eps;
}
else r=mid-eps;
}
printf("%.3lf\n",ans);
} }
poj 2728 Desert King (最优比率生成树)的更多相关文章
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ 2728 Desert King (最优比率树)
题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- 【POJ2728】Desert King 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
随机推荐
- 项目Beta冲刺团队随笔集
博客集如下: Beta冲刺Day1:第一天冲刺记录 Beta冲刺Day2:第二天冲刺记录 Beta冲刺Day3:第三天冲刺记录 Beta冲刺Day4:第四天冲刺记录 Beta冲刺Day5:第五天冲刺记 ...
- 乱码之UTF-8 &GBK
在提交JSP时对于乱码问题,首先我们要搞清楚为什么会出现乱码? 看JSP的头文件:<%@ page contentType="text/html;charset=UTF-8" ...
- 解决在Mac上用pyenv安装python3失败的问题
背景 前段时间在本地Mac系统上要跑一个python3写的压测脚本. Mac默认安装的是python2, 而且很多软件依赖的也是python2. 为了不影响现有系统其它软件, 当时安装了pyenv来实 ...
- vue 中使用better-scroll 遇到的问题
以下是遇到问题以及解决方法 1.使用v-for 循环循环出来的列表,不能滚动. 原因是没有给wrapper 父层 加高度,当子层的高度大于父层的高度,才能滚动 打印scroll 对象,显示如此 竟然相 ...
- 使用mdadm创建磁盘RAID10整列,RAID5出现故障,自动替换硬盘
首先需了解mdadm的参数使用 . 第一步: 先在虚拟机中添加四块硬板 第二步:使用mdadm命令创建RAID10名称为"/dev/md0" -C代表创建操作,v 显示创建过程,- ...
- beta阶段博客合集
第一次博客 第二次博客 第三次博客 第四次博客 第五次博客
- mysql group by分组查询
分组的SQL语句有2个: group by 和分组聚合函数实现 partition by (oracle和postgreSQL中的语句)功能 group by + having 组合赛选数据 注意:h ...
- Linux里的稀疏文件
今天发现一个有意思的现象,文件系统大小只有37GB,上面却有一个900GB的文件!查了下,这个叫“稀疏文件”,我理解类似于VMWare里的瘦硬盘模式吧,先预先划出一块空间,然后往里填数据. [root ...
- 通过ClientDataSet复制表的结构及数据
1. 需要2个ClientDataSet组件: 2. clientDataSet1连接目标表,clientDataSet2连接源表,如果无法直接连接,使用DataSetProvider进行桥接: ...
- SSH框架面试题集锦
Hibernate工作原理及为什么要使用Hibernate? 工作原理: 1.读取并解析配置文件 2.读取并解析映射信息,创建SessionFactory 3.打开Session 4.创建事务Tran ...