首页 技术 正文
技术 2022年11月9日
0 收藏 725 点赞 2,672 浏览 3245 个字

http://poj.org/problem?id=2728

Desert King

Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 18595   Accepted: 5245

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

Source

Beijing 2005 【题解】:

题意:给定三维的点,求这样一棵树,使得高度差的和与水平距离的和的比值最小

这题是很显然的最优比例生成树,不能用贪心求出cost/len,再建MST。

注意输出用% .3f  用%.3lf会WA

【code】:

 /**
Judge Status:Accepted Memory:732K
Time:454MS Language:G++
Code Length:1772B Author:cj
*/ #include <iostream>
#include <stdio.h>
#include <algorithm>
#include <math.h>
#include <string.h> #define N 1010
#define INF 1000000000
//using namespace std; //加了这句居然报CE,什么情况没搞懂 double dis[N];
int pre[N],vis[N];
int n; struct Nod
{
int x,y,z;
}node[N]; double distance(Nod a,Nod b)
{
return sqrt((double)((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)));
} int abs(int x){return x>?x:-x;} double prim(double r)
{
memset(vis,,sizeof(vis));
int i;
for(i=;i<=n;i++)
{
dis[i] = abs(node[].z-node[i].z) - distance(node[],node[i])*r;
pre[i]=;
}
dis[] = ;
vis[] = ;
double cost=,len=;
int j;
for(i=;i<n;i++)
{
double mins = INF;
int k = -;
for(j=;j<=n;j++)
{
if(!vis[j]&&mins>dis[j])
{
mins = dis[j];
k = j;
}
}
if(k==-) break;
vis[k] = ;
cost += abs(node[pre[k]].z-node[k].z);
len += distance(node[pre[k]],node[k]);
for(j=;j<=n;j++)
{
double val = abs(node[k].z-node[j].z) - distance(node[k],node[j])*r;
if(!vis[j]&&dis[j]>val)
{
dis[j] = val;
pre[j] = k;
}
}
}
return cost/len;
} int main()
{
while(~scanf("%d",&n)&&n)
{
int i;
for(i=;i<=n;i++)
{
scanf("%d%d%d",&node[i].x,&node[i].y,&node[i].z);
}
double a=,b=; //初始r为0
while()
{
b = prim(a);
if(fabs(a-b)<1e-) break;
a=b;
}
printf("%.3f\n",b); //printf("%.3lf\n",b); %.3lf居然WA 改 %.3f就AC 神马神马情况
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,488
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,903
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,736
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,487
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:8,127
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:5,289