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 ...
随机推荐
- sysdig安装和使用介绍
安装步骤1)安装资源库rpm --import https://s3.amazonaws.com/download.draios.com/DRAIOS-GPG-KEY.publiccurl -s -o ...
- shiro 配置拦截规则之后css和js等失效
使用shiro作为平台的权限管理工具,shiro的配置文件如下: package com.ros.config; import java.util.LinkedHashMap;import java. ...
- Java异常处理:如何写出“正确”但被编译器认为有语法错误的程序
文章的标题看似自相矛盾,然而我在"正确"二字上打了引号.我们来看一个例子,关于Java异常处理(Exception Handling)的一些知识点. 看下面这段程序.方法pleas ...
- rf统计条数
js模式 直接引用关键字模式
- 利用java自带的base64实现加密、解密
package com.stone.util; import java.io.UnsupportedEncodingException; import sun.misc.*; public class ...
- MYSQL 注射精华
前言鄙人今天心血来潮突然想写篇文章,鄙人从来没写过文章,如果有错误的地方请多多指教.本文需要有基础的SQL语句知识才可以更好的理解.建议想学习的人多去了解一下SQL语句和编程语言,知己知彼才能百战百胜 ...
- C# Excel常用控件总结
参考:https://blog.csdn.net/waterstar50/article/details/80590355 1.ClosedXML2.EPPlus 教程:http://www.cnbl ...
- vue input 判断
//输入框 判断 //全局异常提示信息 //b 1:失去焦点验证错误提示 2:得到焦点关闭错误提示 //i 来区分是验证那个input框 check:function (t,b) { var that ...
- 使用iptables缓解DDOS及CC攻击
使用iptables缓解DDOS及CC攻击 LINUX 追马 7个月前 (02-09) 465浏览 0评论 缓解DDOS攻击 防止SYN攻击,轻量级预防 iptables -N syn-flo ...
- 哪些 Python 库让你相见恨晚?
知乎用户,A European Swallow. 苇叶.Aran He.jerry等人赞同 补充三个有助于自动化日常工作的: sh:sh 1.08 — sh v1.08 documentation可以 ...