最优比率生成树 poj2728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 28407 | Accepted: 7863 |
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
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
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
#include<bits/stdc++.h>
using namespace std;
#define inf 0x3f3f3f3f
int N;
double eps=1e-;
double x[],y[],z[];
double dis(int a,int b){
return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}
bool vis[];
double d[];
double l[][],c[][];
const double INF=;
bool prime(double x){
memset(vis,,sizeof(vis));
vis[]=;
for(int i=;i<=N;++i) d[i]=c[][i]-x*l[][i];
double res=,minn=INF;
int u;
for(int i=;i<N;++i){minn=INF;
for(int j=;j<=N;++j)
if(!vis[j] && d[j]<minn){ minn=d[j]; u=j;} if(minn==INF) break;
vis[u]=;
res+=minn;
for(int j=;j<=N;++j)
if(!vis[j] && d[j]>c[u][j]-x*l[u][j])
d[j]=c[u][j]-x*l[u][j];
}
return res<=;
} int main()
{
while(cin>>N){int i,j;
if(!N) break;
for(i=;i<=N;++i) l[i][i]=c[i][i]=;
for(i=;i<=N;++i){
scanf("%lf%lf%lf",x+i,y+i,z+i);
for(j=;j<i;++j){
c[i][j]=c[j][i]=fabs(z[i]-z[j]);
l[i][j]=l[j][i]=dis(i,j);
}
}
double l=,r=;
while(fabs(l-r)>=eps){
double mid=(l+r)/;
if(prime(mid))r=mid;
else l=mid;
}
printf("%.3f\n",l);
}
return ;
}
最优比率生成树 poj2728的更多相关文章
- POJ2728 Desert King 【最优比率生成树】
POJ2728 Desert King Description David the Great has just become the king of a desert country. To win ...
- 【最优比率生成树】poj2728 Desert King
最优比率生成树教程见http://blog.csdn.net/sdj222555/article/details/7490797 个人觉得很明白易懂,但他写的代码略囧. 模板题,但是必须Prim,不能 ...
- [POJ2728] Desert King 解题报告(最优比率生成树)
题目描述: David the Great has just become the king of a desert country. To win the respect of his people ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
- [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环
01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- [USACO]地震 (二分答案+最优比率生成树详解)
题面:[USACO 2001 OPEN]地震 题目描述: 一场地震把约翰家的牧场摧毁了, 坚强的约翰决心重建家园. 约翰已经重建了N个牧场,现在他希望能修建一些道路把它们连接起来.研究地形之后,约翰发 ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
随机推荐
- android 带RadioButton的Dialog
package com.example.dialog3; import android.os.Bundle;import android.app.Activity;import android.app ...
- java并发 —— Lock
java并发 -- Lock 关于java并发中的锁知识,少不了 Lock.本文转载自:Java并发编程:Lock. 从Java 5之后,在java.util.concurrent.locks包下提供 ...
- 基于TSUNG对MQTT进行压力测试-基础概念温习
[单台Broker压测结果]请移步另一篇博客:http://www.cnblogs.com/lingyejun/p/7941271.html 一.TCP报头部中的SYN.FIN.ACK: ACK : ...
- Jenkins系统+独立部署系统
原文出自:http://os.51cto.com/art/201601/504846.htm 有了Jenkins,为什么还需要一个独立的部署系统? 现在已经有Jenkins,它自身提供了丰富的部署插件 ...
- ionic 配置打包环境
配置java环境就不说了,太简单 下载AndroidSdkAndroid SDK Tools翻过墙的朋友可以去Google Android的官网上下载:http://developer.android ...
- cdoj1638 红藕香残玉簟秋,轻解罗裳,独上兰舟。
地址:http://acm.uestc.edu.cn/#/problem/show/1638 题目: 红藕香残玉簟秋,轻解罗裳,独上兰舟. Time Limit: 4000/2000MS (Java/ ...
- spark关于join后有重复列的问题(org.apache.spark.sql.AnalysisException: Reference '*' is ambiguous)
问题 datafrme提供了强大的JOIN操作,但是在操作的时候,经常发现会碰到重复列的问题.在你不注意的时候,去用相关列做其他操作的时候,就会出现问题! 假如这两个字段同时存在,那么就会报错,如下: ...
- Python基本知识 os.path.join与split() 函数
Python中有join和os.path.join()两个函数,具体作用如下: join:连接字符串数组.将字符串.元组.列表中的元素以指定的字符(分隔符)连接生成一个新的字符串os.path.joi ...
- ThreadLocal的设计理念与作用
转自:http://www.iteye.com/topic/103804 首先,ThreadLocal 不是用来解决共享对象的多线程访问问题的,一般情况下,通过ThreadLocal.set() 到线 ...
- centos安装xdebug 和 phpstorm+Xdebug断点调试PHP
转载地址:http://www.2cto.com/os/201304/206058.html CentOS下安装xdebug 在CentOS 6.x 的系统中,是集成xdebug 的, y ...