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分数规划问题,枚举l,然后求一下最小生成树,嗯,还是很裸啦,然后借机学了一下prim,一直只会Kru(╮(╯▽╰)╭)。

 #include <iostream>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#define inf 1000000000
#define eqs 1e-7
const int N = + ;
using namespace std ;
int n ;
struct id
{
int x , y , h ;
} vill[N] ;
double edge[N][N] , cost[N] ;
int near[N] ; double ffabs( double a )
{
if( a < ) return -a ; return a ;
} double dis( int a , int b )
{ return sqrt(1.0 * (vill[a].x - vill[b].x) * (vill[a].x - vill[b].x) + 1.0 * (vill[a].y - vill[b].y) * (vill[a].y - vill[b].y)); } double prim( int sc , double l )
{
double Cost = , len = ;
for( int i = ; i <= n ; ++i )
{
near[i] = sc ;
cost[i] = abs( vill[sc].h - vill[i].h ) - edge[sc][i] * l ;
}
near[sc] = - ;
for( int i = ; i < n ; ++i )
{
double mi = inf ;
int v = - ;
for( int j = ; j <= n ; ++j )
if( near[j] != - && cost[j] < mi )
{
v = j ;
mi = cost[j] ;
}
if( v != - )
{
Cost += abs( vill[near[v]].h - vill[v].h ) ;
len += edge[near[v]][v] ;
near[v] = - ;
for( int j = ; j <= n ; ++j )
{
double tmp = abs( vill[v].h - vill[j].h ) - edge[v][j] * l ;
if( near[j] != - && tmp < cost[j] )
{
cost[j] = tmp ;
near[j] = v ;
}
}
}
}
return Cost / len ;
} void Init( )
{ for( int x = ; x <= n ; ++x )
scanf( "%d%d%d" , &vill[x].x , &vill[x].y , &vill[x].h ) ;
for( int x = ; x <= n ; ++x )
for( int y = ; y <= n ; ++y )
edge[x][y] = dis( x , y ) ;
} void Solve( )
{
double ans = , tmp ;
while( )
{
tmp = prim( , ans ) ;
if( fabs( ans - tmp ) < eqs ) break ;
// printf( "%.3lf\n" , tmp ) ;
ans = tmp ;
}
printf( "%.3f\n" , tmp ) ;
} int main( )
{
while( ~scanf( "%d" , &n ) && n )
{
Init( ) ;
Solve( ) ;
}
return ;
}

最新文章

  1. 【转】AspNetPager分页控件用法
  2. undefined symbol libiconv_open 完全解决方案
  3. [转]通过继承ConfigurationSection,在web.config中增加自定义配置
  4. php如何实现页面回退的两种方法
  5. Careercup - Facebook面试题 - 4907555595747328
  6. Delphi XE5 android openurl(转)
  7. Mac 下纯lua(一)
  8. 英文版Ubuntu安装Fcitx输入法
  9. Visual Studio 2010 单元测试之一---普通单元测试
  10. zabbix3.2.3安装部署
  11. accp8.0转换教材第1章多线程理解与练习
  12. 使用nginx 的反向代理 给 kibana加上basic的身份认证
  13. There were X failed login attempts since the last successful login
  14. diff和patch命令(1)
  15. 【公告】关于8.8MIP组件审核平台故障的说明
  16. Navicat 连接远程服务器mysql 长时间不操作会连接很久
  17. laravel使用redis队列实践(只需6步,超详细,超简单)
  18. el-date-picker 快捷日期简单计算
  19. last与lastb命令 读取的日志文件
  20. SpringMVC中的自定义参数绑定案例

热门文章

  1. ASP.NET 弹出对话框和页面之间传递值的经验总结
  2. shell脚本ssh自动登陆服务器
  3. 乱序双发射 和 GHB的分支预测
  4. [转贴]使用CryptoAPI解析X509证书和P12证书
  5. Luogu_1565_牛宫_(最大子矩阵)
  6. Oracle MySQL Server 安全漏洞
  7. [ECNU 1624] 求交集多边形面积
  8. wcf 多个节点名出错
  9. HTML5,CSS3 与 Javascript 制作视频播放器
  10. [c#美味] Guid ToString 格式知多少?