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. 核心概念 —— 服务容器

    1.简介 Laravel 服务容器是一个用于管理类依赖和执行依赖注入的强大工具.依赖注入听上去很花哨,其实质是通过构造函数或者某些情况下通过 set 方法将类依赖注入到类中. 让我们看一个简单的例子: ...

  2. ActiveMQ 的安装

    1. 在 http://activemq.apache.org/ 下载 ActiveMQ.Windows 系统选择下载 apache-activemq-x.x.x-bin.zip,Unix/Linux ...

  3. Slickflow.NET 开源工作流引擎基础介绍(一) -- 引擎基本服务接口API介绍

    1. 工作流术语图示                                              图1 流程图形的BPMN图形元素表示 1) 流程模型定义说明流程(Process):是企 ...

  4. 参数请求post, get , delete中的基本使用(2)

    UTF-8数字编码 /// <summary> /// 参数的Url请求 /// </summary> /// <returns></returns> ...

  5. IIS部署网站局域网内无法访问

    今天在局域网发布一个网站时遇到了个问题,在本机上可以访问,但局域网内其他机子访问此IP地址时无法显示,这个问题以前也遇到过,现在总结一下处理方法 检查两个方面: IIS网站身份验证 在IIS中选择要发 ...

  6. C标准库函数实现之strstr(转)

    看下Linux下的实现: char *strstr(const char *s1, const char *s2) { size_t l1, l2; l2 = strlen(s2); if (!l2) ...

  7. iPad accessory communication through UART

    We manufacture a new accessory for iPad/iPhone which should transfer commands to the iPad. We like t ...

  8. js前端防止默认表单提交

    代码如下: document.forms[0].onsubmit=function(){return false;};

  9. OC2-重写

    // // Dog.h // OC2-重写 // // Created by qianfeng on 15/6/17. // Copyright (c) 2015年 qianfeng. All rig ...

  10. Android开发之如何保证Service不被杀掉(前台服务)

    序言 最近项目要实现这样一个效果:运行后,要有一个service始终保持在后台运行,不管用户作出什么操作,都要保证service不被kill.参考了现今各种定制版的系统和安全厂商牛虻软件,如何能保证自 ...