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 ;
}
最新文章
- 【转】AspNetPager分页控件用法
- undefined symbol libiconv_open 完全解决方案
- [转]通过继承ConfigurationSection,在web.config中增加自定义配置
- php如何实现页面回退的两种方法
- Careercup - Facebook面试题 - 4907555595747328
- Delphi XE5 android openurl(转)
- Mac 下纯lua(一)
- 英文版Ubuntu安装Fcitx输入法
- Visual Studio 2010 单元测试之一---普通单元测试
- zabbix3.2.3安装部署
- accp8.0转换教材第1章多线程理解与练习
- 使用nginx 的反向代理 给 kibana加上basic的身份认证
- There were X failed login attempts since the last successful login
- diff和patch命令(1)
- 【公告】关于8.8MIP组件审核平台故障的说明
- Navicat 连接远程服务器mysql 长时间不操作会连接很久
- laravel使用redis队列实践(只需6步,超详细,超简单)
- el-date-picker 快捷日期简单计算
- last与lastb命令 读取的日志文件
- SpringMVC中的自定义参数绑定案例
热门文章
- ASP.NET 弹出对话框和页面之间传递值的经验总结
- shell脚本ssh自动登陆服务器
- 乱序双发射 和 GHB的分支预测
- [转贴]使用CryptoAPI解析X509证书和P12证书
- Luogu_1565_牛宫_(最大子矩阵)
- Oracle MySQL Server 安全漏洞
- [ECNU 1624] 求交集多边形面积
- wcf 多个节点名出错
- HTML5,CSS3 与 Javascript 制作视频播放器
- [c#美味] Guid ToString 格式知多少?