POJ 题目2728 Desert King(最优比率生成树)
2015-06-26 16:16
513 查看
Desert King
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
Sample Output
Source
Beijing 2005
题目大意:
给你n个点,每个都有x,y,z坐标,建立一个三维生成树,是高度差之和和水平距离之和比率最小,求这个比率
ac代码
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 21923 | Accepted: 6123 |
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
题目大意:
给你n个点,每个都有x,y,z坐标,建立一个三维生成树,是高度差之和和水平距离之和比率最小,求这个比率
ac代码
#include<stdio.h> #include<string.h> #include<math.h> #define INF 1<<30 struct s { double x,y,z; }ponit[1010]; int n,vis[1010]; double len[1010][1010],high[1010][1010],map[1010][1010]; double hlen(struct s a,struct s b) { return fabs(a.z-b.z); } double dis(struct s a,struct s b) { return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)); } double prim(double mid) { int i,j; for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { map[i][j]=high[i][j]-mid*len[i][j]; } } memset(vis,0,sizeof(vis)); vis[1]=1; double sum=0; for(i=2;i<=n;i++) { int flag=-1; double minn=INF; for(j=1;j<=n;j++) { if(!vis[j]&&map[1][j]<minn) { flag=j; minn=map[1][j]; } } if(flag==-1) break; vis[flag]=1; sum+=map[1][flag]; for(j=1;j<=n;j++) { if(!vis[j]&&map[1][j]>map[flag][j]) { map[1][j]=map[flag][j]; } } } return sum; } int main() { //int n; while(scanf("%d",&n)!=EOF,n) { int i,j; for(i=1;i<=n;i++) scanf("%lf%lf%lf",&ponit[i].x,&ponit[i].y,&ponit[i].z); for(i=1;i<=n;i++) { for(j=1;j<=n;j++) { len[i][j]=len[j][i]=dis(ponit[i],ponit[j]); high[i][j]=high[j][i]=hlen(ponit[i],ponit[j]); } } double a,b,mid; a=0; b=100; while(b>=a) { mid=(a+b)/2; double temp=prim(mid); if(fabs(temp)<=0.00005) break; if(temp>=0.00005) a=mid; else b=mid; } printf("%.3lf\n",mid); } }
相关文章推荐
- 20150610-Python面试
- 《征服C指针》——读书笔记(5)
- 面试问题之:Android中消息系统模型和Handler Looper
- Atlas+Keepalived系列二:管理Atlas
- c++ std::bitset
- 浅析RabbitMQ 集群
- 复杂表达式解释器 c语言复杂指针定义在线解释 正则在线解释
- 如何解决Android SDK中离线文档打开慢的问题
- 移除input type number的箭头
- android倒计时(整理)
- Impersonation option 设置
- Django1.4 python2.7 apache mod_python 安装与部署实例
- 内部类
- listview数据源有数据,但是不显示数据
- 使用ffmpeg实现解码并保存为yuv文件
- Android照片墙完整版,完美结合LruCache和DiskLruCache
- 面试问题之:JSON是什么?
- MooseFS灾备演练实录
- Office Tips 3 - 如何设置电脑屏幕背景色为淡绿色
- opencl+opencv实现sobel算法