最优比率生成树 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分数规划,二分答案即可,题目很简 ...
随机推荐
- 我与前端之间不得不说的三天两夜之jQuery
前端基础之jquery 一 jQuery是什么? [1] jQuery由美国人John Resig创建,至今已吸引了来自世界各地的众多 javascript高手加入其team. [2] jQuery是 ...
- 自动填写IE的网页的输入框的内容
procedure TForm1.PutData; var ShellWindow: IShellWindows; nCount: integer; spDisp: IDispatch; i,j,X: ...
- 【JXL】导出Excel
基本设置 // File file = new File("d:\\writetest.xlsx");// WritableWorkbook wwb = Workbook.crea ...
- 【Maven】在intellij里,体验Maven
1:配置Maven 从官网下载,然后将bin目录添加到系统变量Path中, 在cmd窗口,输入mvn,有执行反馈即配置成功. 配置conf/settings.xml文件,指定仓库中心下载到本地的目录 ...
- README.android
Default (and possibly architecture dependents) HAL modules go here. libhardware.so eventually should ...
- 解读:hadoop压缩格式
Hadoop中用得比较多的4种压缩格式:lzo,gzip,snappy,bzip2.它们的优缺点和应用场景如下: 1). gzip压缩 优点:压缩率比较高,而且压缩/解压速度也比较快:hadoop本身 ...
- ubuntu16.04下安装mysql详细步骤
1.如果要搭建服务器先去购买一个云主机,比如阿里云.京东云.新网等等都有卖.这里推荐使用京东云服务器,因为最近在搞活动.一元可体验两个月(可能现在活动已经过了,但在京东云里有免费领一个月的,学生机也有 ...
- 在Windows下使用Navicat连接Linux下的MySql
Linux下的Mysql安装可以参考菜鸟教程:https://www.runoob.com/mysql/mysql-install.html 安装完成后你会发现用Navicat链接MySql会失败,这 ...
- HA-web-services
一.HA部署 本次实验的程序选型为heartbeat v1 + hearesources.资源有IP和httpd,filesystem 配置HA集群的前提: (1)各节点资源一致,硬件或软件环境一致 ...
- 同时打印多个worksheets
https://support.office.com/en-us/article/print-a-sheet-or-workbook-0f104967-ebce-406f-9c37-d3ab0dc02 ...