poj-2728Desert King(最优比率生成树)
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 题意:在这么一个图中求一棵生成树,这棵树点权和边权之比最大是多少?
#include<cstdio>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<queue>
#define N 1007
#define eps 0.000001
using namespace std; int n;
double dis[N][N],h[N][N],line[N],p[N][N];
bool vis[N];
struct Node
{
double x,y,h;
}a[N]; double get_dis(int x,int y)
{return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));}
/*struct cmp
{
bool operator()(int x,int y)
{return line[x]>line[y];}
};*/
double prim(double num)
{
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
p[i][j]=h[i][j]-dis[i][j]*num;
//priority_queue<int,vector<int>,cmp>q;
//while(!q.empty()) q.pop();
memset(vis,,sizeof(vis));
memset(line,,sizeof(line));
line[]=;
//for (int i=1;i<=n;i++) q.push(i);
for (int i=;i<=n;i++)
{
int u=;
for (int j=;j<=n;j++)
if (!vis[j]&&line[j]<line[u]) u=j;
vis[u]=;
for (int j=;j<=n;j++)
if (!vis[j]) line[j]=min(line[j],p[u][j]);
}
double sum=;
for (int i=;i<=n;i++)
sum+=line[i];
return sum;
}
int main()
{
while(~scanf("%d",&n)&&n)
{
for (int i=;i<=n;i++)
scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].h);
for (int i=;i<=n;i++)
for (int j=;j<=n;j++)
{
dis[i][j]=get_dis(i,j);
h[i][j]=fabs(a[i].h-a[j].h);
}
double l=0.0,r=100.0;
while(r-l>=eps)
{
double mid=(l+r)*1.0/;
if (prim(mid)>=) l=mid;
else r=mid;
}
printf("%.3f\n",l);
}
}
poj-2728Desert King(最优比率生成树)的更多相关文章
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- 【POJ2728】Desert King 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
- POJ 2728(最优比率生成树+01规划)
Dese ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
随机推荐
- 解决flexpaper读取本地文件问题,JAVA+TOMCAT
flexpaper是不可以用绝对路径的调用本地或项目外的swf文件的,这个我们就有两种方法,一种是把swf和项目放在同一个目录下面,通过相对路径来获取 另一种是在运行项目的tomcat的server. ...
- Clown without borders 2017/1/9
原文 Taking laughter to those who need it most "When will you all return again?"the Croatian ...
- Adding other views to UIButton
Q: I want to add some views to UIButton, for example multiple UILabels, UIImages etc. One I add thos ...
- kubernetes添加不了google apt-key
转自icepoint的博客 key来源 我的百度云盘 密码:v3wo 下载kube_apt_key.gpg到本地,上传到服务器后执行下面的命令 apt-get update && ap ...
- Springboot邮箱接口(使用个人邮箱发送邮件)
近期项目使用邮件验证,这里使用个人邮箱进行测试,记录开发笔记 SpringBoot自带短信接口 maven pom.xml 引入: <dependency> <groupId> ...
- WinForm各种关闭
Appication.Exit(); Environment.Exit(); System.Threading.Thread.CurrentThread.Abort(); Process.GetCur ...
- Mac 电源管理
在安装BatteryManager后,可以删除NullPowerMananger,AppleIntelPowerMananger, AppleIntelPowerClientMananger三个kex ...
- (11)zabbix item types监控类型
1. 什么是item types item types是由zabbix提供的各种类型的检查器(这样翻译很奇怪),大致就是Zabbix agent, Simple checks, SNMP, Zabbi ...
- 四:SQL语句介绍
前言:介绍SQL语句及其大致的分类 一:SQL语句介绍(Structured SQL Lanage) 结构化的查询语言 是一种特殊的编程语言 是一种数据库查询和程序设计语言 用于存取数据及查询.更新和 ...
- perl学习之子程序
一.定义子程序即执行一个特殊任务的一段分离的代码,它可以使减少重复代码且使程序易读.PERL中,子程序可以出现在程序的任何地方.定义方法为:sub subroutine{statements;}二.调 ...