最优比率生成树 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分数规划,二分答案即可,题目很简 ...
 
随机推荐
- nginx php fastcgi安装
			
CGI全称是“公共网关接口”(Common Gateway Interface),HTTP服务器与你的或其它机器上的程序进行“交谈”的一种工具,其程序须运行在网络服务器上. CGI可以用任何一种语言编 ...
 - 使用 Task 简化异步编程
			
.Net 传统异步编程概述 .NET Framework 提供以下两种执行 I/O 绑定和计算绑定异步操作的标准模式: 异步编程模型 (APM),在该模型中异步操作由一对 Begin/End 方法(如 ...
 - WEB项目异常处理
			
package cn.rest.advice; import javax.servlet.http.HttpServletResponse; import org.slf4j.Logger;impor ...
 - 使用Spring Session和Redis解决分布式Session跨域共享问题
			
http://blog.csdn.net/xlgen157387/article/details/57406162 使用Spring Session和Redis解决分布式Session跨域共享问题
 - lightGallery 一个视屏不播放 解决方法
			
这次使用了lightGallery,感觉还不错.样式比较美观,并且支持响应式. 使用过程中,我遇到了下面的问题: 当我 .picsgallery里面只有一个 .gItem的时候.点击弹出幻灯片,再点击 ...
 - 一幅图秒懂LoadAverage(转载)
			
转自:http://www.habadog.com/2015/02/27/what-is-load-average/ 一幅图秒懂LoadAverage(负载) 一.什么是Load Average? ...
 - Python3.x:获取代理ip以及使用
			
Python3.x:获取代理ip以及使用 python爬虫浏览器伪装 #导入urllib.request模块 import urllib.request #设置请求头 headers=("U ...
 - Spring编码过滤器:解决中文乱码
			
Spring编码过滤器:解决中文乱码 针对问题: 前台JSP页面和JAVA代码中使用了不同的字符集进行编码的时候就会出现表单提交的数据或者上传/下载中文名称文件出现乱码的问题: 解决方案: web.x ...
 - Cisco 交换Vlan配置
			
添加Vlan命令 #添加vlan100 config)#vlan 100 #重命名vlan100 config-vlan)#name vlan100 #返回上一层 config-vlan)#exit ...
 - 20145230熊佳炜《逆向及BOF基础实践》
			
20145230熊佳炜<逆向及BOF基础实践> 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件.该程序同时包含另一个代码片段,getShell,会返回一个可用Shell. ...