最优比率生成树 poj2728
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions: 28407 | Accepted: 7863 |
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
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
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.
Sample Input
4
0 0 0
0 1 1
1 1 2
1 0 3
0
Sample Output
1.000
Source
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int N;
double eps=1e-;
double x[],y[],z[];
double dis(int a,int b){
return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
bool vis[];
double d[];
double l[][],c[][];
const double INF=;
bool prime(double x){
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<=N;++i) d[i]=c[][i]-x*l[][i];
double res=,minn=INF;
int u;
for(int i=;i<N;++i){minn=INF;
for(int j=;j<=N;++j)
if(!vis[j] && d[j]<minn){ minn=d[j]; u=j;} if(minn==INF) break;
vis[u]=;
res+=minn;
for(int j=;j<=N;++j)
if(!vis[j] && d[j]>c[u][j]-x*l[u][j])
d[j]=c[u][j]-x*l[u][j];
}
return res<=;
} int main()
{
while(cin>>N){int i,j;
if(!N) break;
for(i=;i<=N;++i) l[i][i]=c[i][i]=;
for(i=;i<=N;++i){
scanf("%lf%lf%lf",x+i,y+i,z+i);
for(j=;j<i;++j){
c[i][j]=c[j][i]=fabs(z[i]-z[j]);
l[i][j]=l[j][i]=dis(i,j);
}
}
double l=,r=;
while(fabs(l-r)>=eps){
double mid=(l+r)/;
if(prime(mid))r=mid;
else l=mid;
}
printf("%.3f\n",l);
}
return ;
}
最优比率生成树 poj2728的更多相关文章
- POJ2728 Desert King 【最优比率生成树】
POJ2728 Desert King Description David the Great has just become the king of a desert country. To win ...
- 【最优比率生成树】poj2728 Desert King
最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能 ...
- [POJ2728] Desert King 解题报告(最优比率生成树)
题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
- [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环
01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- [USACO]地震 (二分答案+最优比率生成树详解)
题面:[USACO 2001 OPEN]地震 题目描述: 一场地震把约翰家的牧场摧毁了, 坚强的约翰决心重建家园. 约翰已经重建了N个牧场,现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发 ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
随机推荐
- 转:在0~N(不包括N)范围内随机生成一个长度为M(M <= N)且内容不重复的数组
1. 最朴素暴力的做法. void cal1() { , j = , num = ; int result[M]; result[] = rand() % N; //第一个肯定不重复, 直接加进去 ; ...
- 无线路由MAC地址过滤安全可靠性讨论
无线路由MAC地址过滤安全可靠性讨论/如何实现,真的有效吗,如何防范 [内容导航] 什么是MAC地址过滤 突破MAC地址过滤步骤 捕获的无线客户端MAC地址 更改MAC地址来伪造身份 在W ...
- exec-maven-plugin配置及使用
背景: 如果你想在项maven生命周期内,运行一段java代码,或者一段独立的程序,或者说我们所指的预执行,初始化某些值,生成某些不能预先生成的文件.那么这样我们就可以使用exec-maven-plu ...
- hust1010 The Minimum Length
地址:http://acm.hust.edu.cn/problem/show/1010 题目: 1010 - The Minimum Length Time Limit: 1s Memory Limi ...
- 虚拟机Linux系统忘记密码的情况下,修改root或其他用户密码
使用场景 linux管理员忘记root密码,需要进行找回操作. 注意事项:本文基于centos7环境进行操作,由于centos的版本是有差异的,继续之前请确定好版本. 步骤 一.重启系统,在开机过程中 ...
- Qt 编码问题QTextCodec
在学习计算机语言的时候, 关于字体编码问题, 一直是大家开始学习新语言比较头痛的问题, 在这边总结一下关于Qt图形框架开发的编码问题. 一般在Window开发环境里,是GBK编码,在Linux开发环境 ...
- 谷歌技术"三宝"之MapReduce(转)
原文地址:http://blog.csdn.net/opennaive/article/details/7514146 江湖传说永流传:谷歌技术有"三宝",GFS.MapReduc ...
- Spark高级数据分析· 2数据分析
wget https://archive.ics.uci.edu/ml/machine-learning-databases/00210/donation.zip 数据清洗 cd /Users/eri ...
- maven常见指令和插件
总结自:https://www.cnblogs.com/ysocean/p/7416307.html#_label1及 https://blog.csdn.net/zhaojianting/artic ...
- 20145333 《Java程序设计》第5周学习总结
20145333 <Java程序设计>第5周学习总结 教材学习内容总结 语法与继承架构 使用try.catch Java中所有错误都会被包装成对象,可以尝试(try)执行程序并捕捉(cat ...