Desert King
Time Limit: 3000MS   Memory Limit: 65536K
     

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

Source

 
题目大意:构建最优比率生成树
每条边的花费=连接两点的高度差,距离=平面两点间距离(官方名称:欧几里得距离)
最小化 ∑ 每条边的花费/距离
01分数规划+prim
prim构造最小生成树的标准是 w=这条边的花费-这条边的距离*二分的mid
最后判断选用边的w是否大于0
01分数规划就用在这里
然而初学,并没有想到,一直在思考怎么在prim过程中套01规划
再就是判断式子>0,移动下界,否则移动上界
这是固定的,与最终求最大最小值没有关系
受了刚做的一道01规划题影响http://www.cnblogs.com/TheRoadToTheGold/p/6546981.html
#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 (最优比率生成树)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. Scapy之ARP询问

    引言 校园网中,有同学遭受永恒之蓝攻击,但是被杀毒软件查下,并知道了攻击者的ip也是校园网.所以我想看一下,这个ip是PC,还是路由器. 在ip视角,路由器和pc没什么差别. 实现 首先是构造arp报 ...

  2. BETA-6

    前言 我们居然又冲刺了·六 团队代码管理github 站立会议 队名:PMS 530雨勤(组长) 过去两天完成了哪些任务 新方案代码比之前的更简单,但是对场景的要求相应变高了,已经实现,误差感人 代码 ...

  3. 牛客网国庆集训派对Day3题目 2018年

    链接:https://www.nowcoder.com/acm/contest/203/D来源:牛客网 Shopping 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 524288K ...

  4. Unity发布Windows程序遇到的问题

    Unity版本:5.6.2 因为程序中使用了Networking模块,所以在打包发布的时候需要登录Unity的账号,并做设置. 错误信息如下: 解决办法如下: 先登录Unity账号,并在Service ...

  5. Internet History, Technology and Security (Week 3)

    Week 3 History: The Web Makes it Easy to Use Welcome to week 3! This is our fourth and final week of ...

  6. mac下快速安装gearman和php扩展

    1.brew install gearman 用brew安装gearman 2.pecl install gearman 用pecl安装php的gearman扩展 3.ln -s /usr/local ...

  7. robotium学习及整理

    一.                      Robotium 简介 Robotium是一款国外的Android自动化测试框架,主要针对Android平台的应用进行黑盒自动化测试,它提供了模拟各种手 ...

  8. SPOJ NETADMIN_Smart Network Administrator

    给一个图,某些点需要单独以某一种颜色的线连接到1点,问如何安排能够使得整个图颜色最多的一条路颜色最少. 显然,二分枚举然后加以颜色其实就是流量了,相当于对每条边限定一个当前二分的流量值,判断能否满流即 ...

  9. (转)Python中如何理解if __name__ == '__main__'

    摘要 通俗的理解 __name__ == '__main__' :假如你叫李凯.py,在朋友眼中,你是李凯( __name__ == '李凯' ):在你自己眼中,你是你自己( __name__ == ...

  10. springboot整合spring @Cache和Redis

    转载请注明出处:https://www.cnblogs.com/wenjunwei/p/10779450.html spring基于注解的缓存 对于缓存声明,spring的缓存提供了一组java注解: ...