POJ 2728 Desert King
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
还是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的更多相关文章
- poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissio ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25310 Accepted: 7022 Desc ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
- POJ 2728 Desert King | 01分数规划
题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...
- 【POJ 2728 Desert King】
Time Limit: 3000MSMemory Limit: 65536K Total Submissions: 27109Accepted: 7527 Description David the ...
- POJ 2728 Desert King:最优比率生成树
题目链接:http://poj.org/problem?id=2728 题意: 给你n个点(x,y,z),让你求一棵生成树,使得 k = ∑ |z[i]-z[j]| / ∑ dis(i,j)最小. | ...
随机推荐
- android ListView内数据的动态添加与删除
main.xml 文件: <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns ...
- Codeforces Burning Midnight Oil
/* * BurningMidnightOil.cpp * * Created on: 2013-10-12 * Author: wangzhu */ /** * 每次至少写多少行代码ret: * 1 ...
- [jobdu]二维数组中的查找
http://ac.jobdu.com/problem.php?pid=1384 基本思路很简单,从最右上角找起. 九度的OJ做得还是不太行啊.必须要int main()才行,这道题时间卡得太紧,用c ...
- 快速排序法QuickSort
/** * * @author Administrator * 功能:交换式排序之快速排序 */ package com.test1; import java.util.Calendar; publi ...
- 139. Word Break
题目: Given a string s and a dictionary of words dict, determine if s can be segmented into a space-se ...
- Android 多屏幕适配
问题: 测试时,发现应用在不同的显示器上显示效果不同(部分文本不能显示完全),自然想到屏幕适配的问题. 按照思路整理如下: (一) 几个概念 1, Screen size 屏幕的尺寸,即对角线长度(单 ...
- Android 设置控件可见与不可见
通常控件的可见与不可见分为三种情况 第一种 gone 表示不可见并且不占用空间 第二种 visible 表示可见 第三种 invisible 表示不 ...
- Oracle Form Developer: Folder FRM-99999 Error 14212
Question: 做FOLDER文件夹功能,打开FORM错误提示: FRM-99999:出现1412错误.有关该错误的详细信息,请参阅发行说明文件(relnotes) Answer: 原因是FOLD ...
- 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 ...
- cocos2d-x 2.2 wp8 开发手记
最近有朋友问我有没有搞过 wp8 的cocos2dx开发 回复:额,没有.(感觉超没面子对方是妹子 = = ) 本着帮妹子试试的态度 就开始了 今天工作 第一我印象中wp8 开发必须要用 vs20 ...