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

 
【题解】:

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

这题是很显然的最优比例生成树,不能用贪心求出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 ;
}

poj 2728 Desert King (最小比例生成树)的更多相关文章

  1. poj 2728 Desert King(最小比率生成树,迭代法)

    引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求 ...

  2. POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 25310   Accepted: 7022 Desc ...

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

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

  4. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

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

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

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

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

  7. POJ 2728 Desert King (01分数规划)

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

  8. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

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

    #include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #i ...

随机推荐

  1. MySQL之经典语句

    数据库的创建:(例如创建名为ConstructionDB的数据库) --创建SelfStudy数据库 CREATE DATABASE ConstructionDB ON PRIMARY --创建主数据 ...

  2. VS2012生成不依赖运行时不依赖MFC的MFC程序

    转载请注明来源:http://www.cnblogs.com/xuesongshu/ 1.新建MFC或者Win32工程,全部使用默认设置 2.设置工程属性,展开配置属性,转到:常规~MFC的使用,修改 ...

  3. VScript 函数调用的两种分类:Sub过程和Function过程

    来源:http://soft.zdnet.com.cn/software_zone/2007/0925/523318.shtml 在 VBScript 中,过程被分为两类:Sub 过程和 Functi ...

  4. EF查询生成的SQL

    在EF 4和EF 3.5 SP1中,我们可以使用ToTraceString()方法得到EF查询所生成的SQL. using (var context = new TestDBEntities()) { ...

  5. Codewars编辑题--今天升到了7段

    今天做的题目是:写一个函数toWeirdCase(),对给定的一个字符串string进行偶数位(包括0)变成大写的操作,字符串string分为单个单词的字符串和多个单词组成的句子.效果应该是这个样子滴 ...

  6. 常用DOM笔记

    1,获取元素方法: (1),获取单个,返回一个元素 element.getElementById()//最快,实时 element.querySelector() (2)获取多个,返回一组 eleme ...

  7. 巧用Systemtap注入延迟模拟IO设备抖动

    原创文章,转载请注明: 转载自系统技术非业余研究 本文链接地址: 巧用Systemtap注入延迟模拟IO设备抖动 当我们的IO密集型的应用怀疑设备的IO抖动,比如说一段时间的wait时间过长导致性能或 ...

  8. 构造函数继承关键apply call

    主要我是要解决一下几个问题: 1.        apply和call的区别在哪里 2.        apply的其他巧妙用法(一般在什么情况下可以使用apply) 我首先从网上查到关于apply和 ...

  9. ngx_cdecl

    ngx_cdecl 作为跨平台用,现在理解有限,以后补充 _cdecl 是C Declaration的缩写(declaration,声明),表示C语言默认的函数调用方法:所有参数从右到左依次入栈,这些 ...

  10. CSS居中的方法整合--水平居中

    原文 CSS的居中问题,是一个老生常谈的问题,各种居中方法层出不穷.是水平居中还是垂直居中?是block还是inline? 居中对象是一个还是多个?长度宽度是否确定?等等各种因素确定. 这里就从这些方 ...