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 ...
随机推荐
- loadrunner 事务、同步点和思考时间
事务 在LoadRunner里,我们定义事务主要是为了度量服务器的性能.每个事务度量服务器响应指定的Vuser请求所有的时间,这些请求可以是简单任务,也可以是复杂任务. 要度量事务,需要插入Vuser ...
- 今日头条移动app广告激活数据API对接完整Java代码实现供大家参考》》》项目随记
这是自毕业后的第一篇博客,希望自己今后能养成写博客的一个好习惯.最近公司为了加速APP推广,采取在外部平台(如:今日头条)进行广告投放的方式,进行用户引流.因此我们需要对广告的激活数据进行一个检测,跟 ...
- 【数学建模】day08-数理统计III
2. 回归分析 回归分析与曲线拟合区分. 曲线拟合是,根据得到的若干有关变量的一组数据,寻找因变量与(一个或几个)自变量之间的一个函数,使这个函数对那组数据拟合得好.通常,函数的形式可以由经验.先验知 ...
- BZOJ3730震波——动态点分治+线段树(点分树套线段树)
题目描述 在一片土地上有N个城市,通过N-1条无向边互相连接,形成一棵树的结构,相邻两个城市的距离为1,其中第i个城市的价值为value[i].不幸的是,这片土地常常发生地震,并且随着时代的发展,城市 ...
- 基准对象object中的基础类型----字典 (六)
object有如下子类: CLASSES object basestring str unicode buffer bytearray classmethod complex dict enumera ...
- L - Vases and Flowers HDU - 4614 线段树+二分
题意 给出一排空花瓶 有两种操作 1是 从A花瓶开始放F朵花 如果当前瓶有花就跳过前往下一个 直到花用完或者 瓶子到了最后一个为止 输出 成功放花的第一个和最后一个 如果没有输出 can not. ...
- 基于FPGA的VGA接口使用
前言 什么是VGA? VGA(视频图形阵列)是IBM公司制定的一种视频数据传输标准. 接口信号主要有5个:R(Red),G(Green),B(Blue),HS(Horizontal synchroni ...
- verilog parameter 位宽问题
前言 一直以为parameter 的位宽是无限的,其实不然. 流程: 仿真一下就知道啦: 用处: 精准控制位宽理论上会占用更少的内存,其他好像并没有什么卵用,注意不要越界,我这里系统默认32bit位宽 ...
- 【XSY2719】prime 莫比乌斯反演
题目描述 设\(f(i)\)为\(i\)的不同的质因子个数,求\(\sum_{i=1}^n2^{f(i)}\) \(n\leq{10}^{12}\) 题解 考虑\(2^{f(i)}\)的意义:有\(f ...
- Win10报错0x800f0906
在安装适用于Linux的Windows子系统(Beta)的时候,有选中开发人员模式这一步设置->更新和安全->针对开发人员->选中开发人员模式 如果报错0x800f0906 那是因为 ...