Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions:29775   Accepted: 8192

Description

David the Great has just become the king of a desert country. To win the respect of his people, he decided to build channels all over his country to bring water to every village. Villages which are connected to his capital village will be watered. As the dominate ruler and the symbol of wisdom in the country, he needs to build the channels in a most elegant way.

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

There are several test cases. Each test case starts with a line containing a number N (2 <= N <= 1000), which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 <= x, y < 10000, 0 <= z < 10000000). (x, y) is the position of the village and z is the altitude. The first village is the capital. A test case with N = 0 ends the input, and should not be processed.

Output

For each test case, output one line containing a decimal number, which is the minimum ratio of overall cost of the channels to the total length. This number should be rounded three digits after the decimal point.

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分数规划)的更多相关文章

  1. POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)

    [题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...

  2. POJ 2728 Desert King 01分数规划,最优比率生成树

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

  3. POJ 2728 Desert King | 01分数规划

    题目: http://poj.org/problem?id=2728 题解: 二分比率,然后每条边边权变成w-mid*dis,用prim跑最小生成树就行 #include<cstdio> ...

  4. poj2728 Desert King——01分数规划

    题目:http://poj.org/problem?id=2728 第一道01分数规划题!(其实也蛮简单的) 这题也可以用迭代做(但是不会),这里用了二分: 由于比较裸,不作过多说明了. 代码如下: ...

  5. 【POJ2728】Desert King - 01分数规划

    Description David the Great has just become the king of a desert country. To win the respect of his ...

  6. poj2728 Desert King --- 01分数规划 二分水果。。

    这题数据量较大.普通的求MST是会超时的. d[i]=cost[i]-ans*dis[0][i] 据此二分. 但此题用Dinkelbach迭代更好 #include<cstdio> #in ...

  7. 【POJ2728】Desert King(分数规划)

    [POJ2728]Desert King(分数规划) 题面 vjudge 翻译: 有\(n\)个点,每个点有一个坐标和高度 两点之间的费用是高度之差的绝对值 两点之间的距离就是欧几里得距离 求一棵生成 ...

  8. POJ 2728 Desert King(最优比率生成树 01分数规划)

    http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...

  9. poj 2728 Desert King (最优比率生成树)

    Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS   Memory Limit: 65536K       Descripti ...

随机推荐

  1. Spring Boot 构建电商基础秒杀项目 (十) 交易下单

    SpringBoot构建电商基础秒杀项目 学习笔记 新建表 create table if not exists order_info ( id varchar(32) not null defaul ...

  2. Jenkins+PowerShell持续集成环境搭建(四)常用PowerShell命令

    0. 修改执行策略 Jenkins执行PowerShell脚本,需要修改其执行策略.以管理员身份运行PowerShell,执行以下脚本: Set-ExecutionPolicy Unrestricte ...

  3. 树&图 记录

    A - Lake Counting POJ - 2386 最最最最最基础的dfs 挂这道题为了提高AC率(糖水不等式 B - Paint it really, really dark gray Cod ...

  4. vuex2.0 基本使用(3) --- getter

    有的组件中获取到 store 中的state,  需要对进行加工才能使用,computed 属性中就需要写操作函数,如果有多个组件中都需要进行这个操作,那么在各个组件中都写相同的函数,那就非常麻烦,这 ...

  5. C语言实现字符串逆序输出

    方法一: #include <stdio.h> #include <stdlib.h> #include <string.h> void Reverse(char ...

  6. Nginx+Tomcat 负载均衡集群

    案例分析 通常情况下,一台Tomcat站点由于可能出现单点故障及无法应对多客户复杂多样性的请求等问题,不能单独应用于生产环境下,所以我们需要一套更可靠的解决方案来完善Web站点架构. Nginx是一款 ...

  7. 洛谷P2918 [USACO08NOV]买干草(一道完全背包模板题)

    题目链接 很明显的一道完全背包板子题,做法也很简单,就是要注意 这里你可以买比所需多的干草,只要达到数量就行了 状态转移方程:dp[j]=min(dp[j],dp[j-m[i]]+c[i]) 代码如下 ...

  8. Matplotlib学习---用seaborn画联合分布图(joint plot)

    有时我们不仅需要查看单个变量的分布,同时也需要查看变量之间的联系,这时就需要用到联合分布图. 这里利用Jake Vanderplas所著的<Python数据科学手册>一书中的数据,学习画图 ...

  9. Ikki's Story IV - Panda's Trick POJ - 3207(水2 - sat 在圈内 还是 在圈外)

    题意: 就是一个圈上有n个点,给出m对个点,这m对个点,每一对都有一条边,合理安排这些边在圈内或圈外,能否不相交 解析: 我手残 我手残 我手残 写一下情况 只能是一个在圈外 一个在圈内 即一个1一个 ...

  10. codeforces1096G Lucky Tickets

    题目链接:https://codeforces.com/problemset/problem/1096/G 大意:给出\(k\)个数码\(d_1,d_2,\cdots,d_k\),构造一个由这\(k\ ...