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/ ...
随机推荐
- 手机访问本地php项目遇到的问题及解决
做html5的本地应用要调试后台,学了下php 按照和连j2ee的时候一样,电脑发射wifi,ipconfig..等等 发现tomcat的可以访问,apache的不能访问,搜索好久,没找到解答, j ...
- Codeforces Round #304 (Div. 2) E. Soldier and Traveling 最大流
题目链接: http://codeforces.com/problemset/problem/546/E E. Soldier and Traveling time limit per test1 s ...
- SQL语句中 chinese_prc_CS_AI_WS 以及replace用法
Select * from [DBData].[dbo].[T_Student] where Name='lilei' 查询结果如下: 结论:由查询结果可知 SQL Server ...
- ADO.NET使用using关闭数据库连接
using (SqlConnection conn = new SqlConnection(source)) { // open the connoction conn.Open(); // Do s ...
- vue 选项卡(转载)
!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <meta http-e ...
- lr基本的概念
并发用户数量:与服务器进行交互的在线用户数量 请求响应时间:从client端发出请求到得到响应的整个时间,它一般包括网络相应时间+server的响应时间 事务请求响应时间:完成这个事务所用的时 ...
- [转帖] Linux buffer 和 cache相关内容
Linux中Buffer/Cache清理 Lentil2018年9月6日 Linux中的buff/cache可以被手动释放,释放缓存的代码如下: https://lentil1016.cn/linux ...
- httpstat的简单使用
httpstat 应该是一个 python 封装后的 curl 工具能够展现 一些客户端连接网站的时间消耗,最近在看tls 感觉挺有用处的 简单学习一下 1. centos7 安装python 和 p ...
- HTML之绝对路径与相对路径
路径指文件存放的位置,在网页中利用路径可以引用文件,插入图像.视频等.表示路径的方法有两种:相对路径,绝对路径.以下讨论均是在HTML环境下进行. 相对路径 相对路径是指目标相对于当前文件的路径,网页 ...
- TypeError: to_categorical() got an unexpected keyword argument 'nb_classes'
在学习莫烦教程中keras教程时,报错:TypeError: to_categorical() got an unexpected keyword argument 'nb_classes',代码如下 ...