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)最小. | ...
随机推荐
- call()和apply()的区别
var a = function(a,b){ console.log(a+b); }, b = { c:5, d:3 }; a.call(b,1,2); a.apply(b,[1,2]); a.cal ...
- java多线程下载和断点续传
java多线程下载和断点续传,示例代码只实现了多线程,断点只做了介绍.但是实际测试结果不是很理想,不知道是哪里出了问题.所以贴上来请高手修正. [Java]代码 import java.io.File ...
- codeforces 390E Inna and Large Sweet Matrix
本题的主要算法就是区间更新和区间求和: 可以用线段树和树状数组来做: 感觉线段树写的太麻烦了,看到官方题解上说可以用树状数组做,觉得很神奇,以前用过的树状数组都是单点维护,区间求和的: 其实树状数组还 ...
- Unity3D研究院之在MAC上脚本XlsxWriter写入Excel .xlsx格式
原地址:http://www.xuanyusong.com/archives/3011 以前找了很久可以跨平台支持读写Excel的工具,我也试了很多种DLL.可在Windows上各个完美支持,可是在M ...
- var a=[]; 和 var a=new Array(); 的区别,为什么前者效率高
因为 JSON格式的语法是引擎直接解释的.而new Array 则需要调用Array的构造器.还有就是1.当你需要将一个数字转化为字符串时可以这样定义:var s=""+1; 这样 ...
- android fragment嵌套fragment出现的问题:no activity
package com.example.fragmentNavigation2.fragment; import android.content.Context; import android.os. ...
- 服务器部署_linuix下 一台nginx 多域名之二
第二个需求: 1. 一台服务器部署了三个应用web1~web3对应三个域名:www.web1.com.www.web2.com.www.web3.com ,方法参照我另外一篇文章 2. 如果用非www ...
- Windows server 2008下开启telnet功能
今天在windows server 2008 R2下使用telnet 来测试端口是否可以连接,结果发现如下错误:
- 判断微信内置浏览器的UserAgent
要区分用户是通过"微信内置浏览器"还是"原生浏览器"打开的WebApp, 可以通过navigator.userAgent来进行判断. 以下是对各种平台上微信内置 ...
- 【HDOJ】5564 Clarke and digits
DP+快速矩阵幂.注意base矩阵的初始化,不难. /* 5564 */ #include <iostream> #include <string> #include < ...