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 ;
}

POJ 2728 Desert King的更多相关文章

  1. poj 2728 Desert King (最小比例生成树)

    http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

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

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

  3. POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25310   Accepted: 7022 Desc ...

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

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

  5. POJ 2728 Desert King (01分数规划)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions:29775   Accepted: 8192 Descr ...

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

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  7. POJ 2728 Desert King | 01分数规划

    题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...

  8. 【POJ 2728 Desert King】

    Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...

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

    题目链接:http://poj.org/problem?id=2728 题意: 给你n个点(x,y,z),让你求一棵生成树,使得 k = ∑ |z[i]-z[j]| / ∑ dis(i,j)最小. | ...

随机推荐

  1. android ListView内数据的动态添加与删除

    main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...

  2. Codeforces Burning Midnight Oil

    /* * BurningMidnightOil.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每次至少写多少行代码ret: * 1 ...

  3. [jobdu]二维数组中的查找

    http://ac.jobdu.com/problem.php?pid=1384 基本思路很简单,从最右上角找起. 九度的OJ做得还是不太行啊.必须要int main()才行,这道题时间卡得太紧,用c ...

  4. 快速排序法QuickSort

    /** * * @author Administrator * 功能:交换式排序之快速排序 */ package com.test1; import java.util.Calendar; publi ...

  5. 139. Word Break

    题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...

  6. Android 多屏幕适配

    问题: 测试时,发现应用在不同的显示器上显示效果不同(部分文本不能显示完全),自然想到屏幕适配的问题. 按照思路整理如下: (一) 几个概念 1, Screen size 屏幕的尺寸,即对角线长度(单 ...

  7. Android 设置控件可见与不可见

    通常控件的可见与不可见分为三种情况 第一种    gone         表示不可见并且不占用空间 第二种    visible       表示可见 第三种    invisible    表示不 ...

  8. Oracle Form Developer: Folder FRM-99999 Error 14212

    Question: 做FOLDER文件夹功能,打开FORM错误提示: FRM-99999:出现1412错误.有关该错误的详细信息,请参阅发行说明文件(relnotes) Answer: 原因是FOLD ...

  9. Using innodb_large_prefix to avoid ERROR #1071,Specified key was too long; max key length is 1000 bytes

    Using innodb_large_prefix to avoid ERROR 1071        单列索引限制上面有提到单列索引限制767,起因是256×3-1.这个3是字符最大占用空间(ut ...

  10. cocos2d-x 2.2 wp8 开发手记

    最近有朋友问我有没有搞过  wp8 的cocos2dx开发 回复:额,没有.(感觉超没面子对方是妹子 = = ) 本着帮妹子试试的态度  就开始了 今天工作 第一我印象中wp8 开发必须要用 vs20 ...