POJ2728 Desert King


Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.

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

There 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

For 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


已经确定了一个根,再选择一种优美的结构,不就是树吗(结构是树的时候代价最小),那么这道题就变成一道最优比率生成树,这个题目j就是要求Min(∑升降机成本/∑通道长度)" role="presentation">Min(∑升降机成本/∑通道长度)Min(∑升降机成本/∑通道长度),套一个板子就好啦


#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
const int N=1010;
const double eps=1e-5;
const double INF=1e16;
int n;
double f[N][N],dis[N][N],x[N],y[N],z[N],minv[N];
int vis[N];
double getdis(int a,int b){
return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
int check(double x){
memset(vis,0,sizeof(vis));
double sum=0;vis[1]=1;
for(int i=1;i<=n;i++)minv[i]=f[1][i]-x*dis[1][i];
for(int i=2;i<=n;i++){
double tmp=INF;int k=-1;
for(int j=2;j<=n;j++)
if(!vis[j]&&minv[j]<tmp)
k=j,tmp=minv[j];
if(k==-1)break;
vis[k]=1;
sum+=tmp;
for(int j=2;j<=n;j++)
if(!vis[j]&&f[k][j]-x*dis[k][j]<minv[j])
minv[j]=f[k][j]-x*dis[k][j];
}
return sum>=0;
}
int main(){
while(1){
scanf("%d",&n);
if(!n)break;
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++){
dis[i][j]=dis[j][i]=getdis(i,j);
f[i][j]=f[j][i]=abs(z[i]-z[j]);
}
double l=0.0,r=100.0;
while(r-l>=eps){
double mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
printf("%.3lf\n",r);
}
return 0;
}

POJ2728 Desert King 【最优比率生成树】的更多相关文章

  1. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

  2. POJ2728 Desert King 最优比率生成树

    题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...

  3. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  4. 【POJ2728】Desert King 最优比率生成树

    题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...

  5. Desert King(最优比率生成树)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22717   Accepted: 6374 Desc ...

  6. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  7. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  8. POJ 2728 Desert King (最优比率树)

    题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...

  9. poj-2728Desert King(最优比率生成树)

    David the Great has just become the king of a desert country. To win the respect of his people, he d ...

  10. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

随机推荐

  1. C#API函数

    API函数是构筑Windows应用程序的基石,是Windows编程的必备利器.每一种Windows应用程序开发工具都提供了间接或直接调用了Windows API函数的方法,或者是调用Windows A ...

  2. javascript 关于节点

    重复使用对像可以用 var a,b; with(document){ a = getElementById('aID') b = getElementById('bID') } 关于节点访问: par ...

  3. jquery tmpl模板

    之前用模板渲染都是用angular,无意间发现了jquery tmpl这种轻量级,其文档在这里 官方解释对该插件的说明:将匹配的第一个元素作为模板,render指定的数据,签名如下: .tmpl([d ...

  4. 【转】R语言知识体系概览

    摘要:R语言的知识体系并非语法这么简单,如果都不了R的全貌,何谈学好R语言呢.本文将展示介绍R语言的知识体系结构,并告诉读者如何才能高效地学习R语言. 最近遇到很多的程序员都想转行到数据分析,于是就开 ...

  5. html合并单元格

    在合并的首位置加上colspan或者rowspan属性即可 code: <html>   <body>   <h4>横跨两列的单元格:</h4> < ...

  6. 026——VUE中事件修饰符之使用$event与$prevent修饰符操作表单

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. Java复习7.输入输出流

    Java复习7.输入输出流 20131005 前言: Java中涉及数据的读写,都是基于流的,这一块的知识相当重要,而且在Java中的数据,char字符是16bit的,所以存在字节流和字符流的区别.如 ...

  8. php 请求url获取状态码

    function get_http_code($url) { $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); //设置URL c ...

  9. Windows下安装tomcat

    一.Tomcat下载与安装: 1.直接到官网下载Tomcat安装程序包:http://tomcat.apache.org/ 2.下载下来后是个压缩包,如:apache-tomcat-8.0.26,解压 ...

  10. MayBatis与Spring的整合

    1. 步骤 第一步:导入相关的jar包(spring和mybatis) mybatis-spring-*.jar spring-jdbc-*.jar spring-tx-*.jar 第二步:sprin ...