POJ 2728 Desert King (01分数规划)
| Time Limit: 3000MS | Memory Limit: 65536K | |
| Total Submissions:29775 | Accepted: 8192 |
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 思路
在这个题里面,用到了我上一篇博客里面说到的,01分数规划算法,在写一遍复习这个算法吧,就是用一个估计值,乘上物品的权值,在于物品的价值作比较,这个比较的过程,不止一个节点的比较,所以要将物品的权值和价值累和,在进行比较,这里的权值和价值都没有再乘或除里面,所以这里可以将其累和。如果比较的结果,是价值大了,就调小x,否则,调大x; 除此之外,我要喷一下POJ,说好的数据范围1-1000,我的数组开了1024,tle了一晚上,之后改成了1066,就过了。。过了、、、过了。。。 代码
#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#define inf 1000000000
using namespace std;
int x[],y[],h[];
bool book[];
int n,t;
long double d[][],dis[],z[][],w[][];;
long double Abs(long double x){return (x>=)?x:-x;}
bool prim(double x)
{
for(int i=;i<=n;i++){
dis[i]=inf;
book[i]=false;
for(int j=i+;j<=n;j++){
d[i][j]=d[j][i]=z[i][j]-x*w[i][j];
}
}
dis[]=;
dis[]=inf;
long double ans=;
for(int j=;j<=n;j++){
int t=;
for(int i=;i<=n;i++){
if(!book[i]&&dis[t]>dis[i]){t=i;}
}
ans+=dis[t];book[t]=true;
for(int i=;i<=n;i++){
if(!book[i]&&d[t][i]<dis[i]){
dis[i]=d[t][i];
}
} }
return ans>;
} int main()
{
while(scanf("%d",&n)!=EOF&&n){
for(int i=;i<=n;i++){
scanf("%d%d%d",&x[i],&y[i],&h[i]);
}
for(int i=;i<=n;i++){
for(int j=;j<=n;j++){
w[i][j]=sqrt((long double)((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])));
z[i][j]=Abs(h[i]-h[j]);
}
}
long double l=,r=;
while(r-l>1e-){
double mid=(l+r)/;
if(prim(mid)){l=mid;}
else r=mid;
}
printf("%.3f\n",(double)l);
}
}
POJ 2728 Desert King (01分数规划)的更多相关文章
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- POJ 2728 Desert King | 01分数规划
题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...
- poj2728 Desert King——01分数规划
题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...
- 【POJ2728】Desert King - 01分数规划
Description David the Great has just become the king of a desert country. To win the respect of his ...
- poj2728 Desert King --- 01分数规划 二分水果。。
这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...
- 【POJ2728】Desert King(分数规划)
[POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
随机推荐
- Java ME之Android开发从入门到精通
1. 搭建Android开发环境 方式一:使用ADT插件安装 ADT插件的下载与安装,ADT插件获取网址:http://www.androiddevtools.cn/ 下载好的ADT插件如图所示: 在 ...
- 转 My97日历控件常用功能记录
My97相信大家都不陌生,应该是我所见过的最强大的一个日历控件了,最近的项目中也比较多地用到了此控件,而且项目中经常会有不同时间范围的需求,在此列出一些比较常用的日期范围格式的设置,尽管在My97的官 ...
- CS新建排版
1.拉菜单栏barmanage,去掉不要的头部和尾部 ,选择控件bar属性optionsbar 全部为false,防止菜单拖动. 2.拉一个panelcontrol属性dock 设置顶部,在拉一个p ...
- Python读取excel中的图片
作为Java程序员,Java自然是最主要的编程语言.但是Java适合完成大型项目,对于平时工作中小的工作任务,需要快速完成,易于修改和调试,使用Java显得很繁琐,需要进行类的设计,打成jar包,出现 ...
- 为何CPU散片这么便宜?盒装CPU值得买吗
当玩家选择装一台PC电脑的时候,他会有个怎样的思考过程?第一个要决定的通常是选什么样的处理器,因为处理器的选择可以决定整套平台的预算及性能水平,想玩游戏的话现在4核8线程处理器是入门标准了,高点的则会 ...
- js中session操作
// 保存数据到sessionStorage sessionStorage.setItem('key', 'value'); // 从sessionStorage获取数据 var data = s ...
- Codeforces Round #437 Div. 1
A:显然构造一组只包含1和2面值的数据即可. #include<iostream> #include<cstdio> #include<cmath> #includ ...
- veu——引入iconfont图标
我这里是阿里的iconfont图标,如何下载请看下面这个博文 https://www.cnblogs.com/wangyang0210/articles/9248324.html 创建文件夹 在ass ...
- 【XSY2528】道路建设 LCT 可持久化线段树
题目描述 给你一个\(n\)个点\(m\)条边图,\(q\)个询问,每次问你边权在\([l,r]\)之间的边组成的最小生成树(森林)的边权和.强制在线. \(n,m,q\leq 100000\) 题解 ...
- 【Luogu3732】[HAOI2017]供给侧改革(Trie树)
[Luogu3732][HAOI2017]供给侧改革(Trie树) 题面 洛谷 给定一个纯随机的\(01\)串,每次询问\([L,R]\)之间所有后缀两两之间的\(LCP\)的最大值. 题解 一个暴力 ...