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 ...
随机推荐
- python SMTP 发送邮件 阿里企业邮箱、163邮箱 及535错误
class SendEmail(object): def __init__(self, type, to_addr): self.to_addr = to_addr self.sys_date = t ...
- PHP关联查询
article文章表: aid title content uid 1 文章1 文章1正文内容... 1 2 文章2 文章2正文内容... 1 3 文章3 文章3正文内容... 2 4 文章4 文章4 ...
- 神烦之float
另外一篇文章 : css float 一 历史 Float的设计初衷仅仅是:文字环绕效果(向word中的文字环绕效果) 二 特性 1.包裹性:块级元素如果不设置float,它默认会撑满整个屏幕,而如果 ...
- 安装mysql zip5.7版--安裝
一直以来都习惯了使用MySQL安装文件(.exe),今天下载了一个.zip版本的MySQL,安装过程中遇到了一些问题,如下: 1.在MySQL官网上(http://dev.mysql.com/down ...
- codeforces431C
k-Tree CodeForces - 431C Quite recently a creative student Lesha had a lecture on trees. After the l ...
- hdu-2087(kmp)
题意:模板题,在第一个串中有几个第二个串 解题思路:板子题,拿来练手的: 代码: #include<iostream> #include<algorithm> #include ...
- MongoDB数据模型设计
MongoDB的数据模式是一种灵活模式,其集合并不限制文档结构.这种灵活性让对象和数据库文档之间的映射变得很容易,即使数据记录之间有很大的变化,每个文档也可以很好的映射到各条不同的记录.但在实际使用中 ...
- shiro注解和标签
Controller中注解: @RequiresAuthentication @RequiresGuest @RequiresPermissions("account:create" ...
- 如何保证 spring-boot 和 spring-cloud版本一致
spring-boot 版本 和 spring-cloud版本是一一对应的,很多错误都是由于版本不一致导致的.很多百度的东西太老了, 版本一升级就会出错. spring的jar包依赖关系是最难的,但聪 ...
- NODE&NPM
Awesome npm packages 更新版本: Mac/Linux:npm install -g n && n stable (默认安装目录为:usr/local/local/n ...