poj 2728 Desert King (最小比例生成树)
http://poj.org/problem?id=2728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 18595 | Accepted: 5245 |
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
Source
题意:给定三维的点,求这样一棵树,使得高度差的和与水平距离的和的比值最小
这题是很显然的最优比例生成树,不能用贪心求出cost/len,再建MST。
注意输出用% .3f 用%.3lf会WA
/**
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 (最小比例生成树)的更多相关文章
- poj 2728 Desert King(最小比率生成树,迭代法)
引用别人的解释: 题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可, 建造水管距离为坐标之间的欧几里德距离(好象是叫欧几里德距离吧),费用为海拔之差 现在要求 ...
- POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 25310 Accepted: 7022 Desc ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- POJ 2728 Desert King 01分数规划,最优比率生成树
一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King (01分数规划)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions:29775 Accepted: 8192 Descr ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
- poj 2728 Desert King(最优比例生成树)
#include <iostream> #include <cstdio> #include <cmath> #include <cstdlib> #i ...
随机推荐
- hdu 4635 强连通度缩点
思路:想用Tarjan算法进行缩点,并记录每个连通分支的点数.缩点完毕过后,找出所有出度或入度为0的连通分量,假设该连通分量的点数为num[i],那么 ans=Max(ans,(n-num-1)*(n ...
- PE制作实录 —— 补充说明
上一篇博文中我提到了定制 PE 合盘的方法,可能还有一些朋友不是很懂,这里补充几点. 要点1: 菜单的排布 U盘启动时的界面,这里叫做主界面,而主界面下有时还会用到子界面,下面是我制作的PE的菜单目录 ...
- java下实现调用oracle的存储过程和函数
在Oracle下创建一个test的账户,然后 1.创建表:STOCK_PRICES --创建表格 CREATE TABLE STOCK_PRICES( RIC VARCHAR() PRIMARY KE ...
- tools安装
1.ruby安装 下载安装包 勾选中间一个 2.sass 安装 转换TB镜像 $ gem sources --remove https://rubygems.org/$ gem sources - ...
- 一个简单的XML与数组之间的转换
xml是网络使用最多的数据交换格式,所以,不掌握怎么操作它,又有蛋疼的了. php中可以操作xml的类/函数很多,个人认为最简单的是SimpleXMLElement这个类,它的使用就跟其名字一样:简单 ...
- union on
UNION 操作符用于合并两个或多个 SELECT 语句的结果集. 请注意,UNION 内部的 SELECT 语句必须拥有相同数量的列.列也必须拥有相似的数据类型.同时,每条 SELECT 语句中的列 ...
- IOS TextField设置大全
//初始化textfield并设置位置及大小 //设置边框样式,只有设置了才会显示边框样式 text.borderStyle = UITextBorderStyleRoundedRect; ...
- 升级NppAstyle中的AstyleLib为最高版本
注:本文撰写时,NppAstyle的最高版本为0.10.02.14(更新于2013-04-08),Astyle的最高版本为2.05.1(更新于2014-12-11). Astyle是一个很好的代码格式 ...
- 模板:strncpy函数
参考链接:http://baike.baidu.com/view/1207711.htm strncpy 是 C语言的函数之一,来自 C语言标准库,定义于 string.h,char *strncpy ...
- 谈谈C语言的数据类型
本文摘要: 本文主要讲述C语言中的数据类型,从基本的数据类型到派生的数据类型,从int ,char ,float double ....到指针,数组,函数,指向指针的指针,指向数组的指针,指向函数的指 ...