POJ2728 Desert King 最优比例生成树
来源:互联网 发布:初中编程学哪个 编辑:程序博客网 时间:2024/05/16 13:54
Desert King
题目:
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.
题意:
这是一道最优比例生成树的题目,大致意思就是有N个村庄,给出每个村庄的坐标和海拔,,benifit为两点之间的距离,cost为两点的高度差,现在要求一棵树使得 ∑cost / ∑benift 最小.
一个最小生成树,可以表示为 ∑benifit[i]*x[i]>=minn.
benifit[i]为边权值,x[i]为是否添加该边,minn为最小生成树。
对于该题目来说,有(∑cost[i] * x[i])/ (∑benifit[i] * x[i])>=minn;移项得x[i] * (∑cost[i]-∑benifit[i] * minn)>=0;
显然就是转化为一个边权值为cost[i]-benifit[i] * minn的最小生成树问题了,因为minn未知所有用二分查找可以使变形后的最小生成树结果为0的minn值即可。
代码:
#include <iostream>#include <stdio.h>#include <algorithm>#include <string.h>#include <stdlib.h>#include <cmath>#define N 1000using namespace std;struct node{ double x,y,z;};node maps[1000+20];int n;double grap[N+20][N+20];bool vis[N+20];double val[N+20];double judge(double mid){ memset(vis,0,sizeof(vis)); for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { double ax,ay,az; ax=fabs(maps[i].x-maps[j].x); ay=fabs(maps[i].y-maps[j].y); az=fabs(maps[i].z-maps[j].z); grap[j][i]=grap[i][j]=az-sqrt(ay*ay+ax*ax)*mid; } } for(int i=1;i<=n;i++) val[i]=grap[1][i]; vis[1]=true; double sum=0.0; int T=n-1; while(T--) { double minn=99999999999.9; int index=0; for(int j=1;j<=n;j++) { if(vis[j]==false&&minn>val[j]) { minn=val[j]; index=j; } } sum+=minn; vis[index]=true; for(int j=1;j<=n;j++) { if(vis[j]==false&&grap[index][j]<val[j]) val[j]=grap[index][j]; } } return sum;}int main(){ while(scanf("%d",&n),n!=0) { for(int i=1;i<=n;i++) scanf("%lf%lf%lf",&maps[i].x,&maps[i].y,&maps[i].z); double left=0,right=100.0,mid; while(right-left>1e-6) { mid=(left+right)/2; double temp=judge(mid); if(temp<=0) right=mid; else left=mid; } printf("%.3lf\n",mid); } return 0;}
假期在家宛如咸鱼orz
- poj2728 Desert King,最优比例生成树
- POJ2728 Desert King 最优比例生成树
- poj2728 Desert King 【最优比例生成树 01分数规划】
- POJ2728 Desert King 最优比率生成树
- POJ2728 Desert King 最优比率生成树
- 【POJ2728】Desert King-最优比率生成树
- 【分数规划-最小比例生成树】POJ2728[Desert King]题解
- poj2728--Desert King(最优比率生成树)
- poj2728 Desert King(最优比率生成树)
- POJ2728-Desert King(最优比率生成树)
- poj2728 Desert King(最优比率生成树,二分答案+prim)
- 【分数规划】【最优比率环 & 最优比率生成树】poj3621 Sightseeing Cows && poj2728 Desert King
- poj 2728 Desert King(最优比例生成树)
- POJ 2728 Desert King(最优比例生成树)
- poj 2728 Desert King(最优比例生成树)
- poj 2728 Desert King(最优比例生成树)
- poj 2728 Desert King(最优比例生成树)
- poj 2728 Desert King(最优比例生成树)
- HTML引用资源相对路径
- poj 3185 高斯消元 枚举变元
- jsp中el表达式无效的解决办法
- 数组的应用深入
- POJ 1655 Balancing Act(简单树型dp)
- POJ2728 Desert King 最优比例生成树
- 指针的简单应用
- android2.6.1——知晓当前活动
- 指针的深入
- 结构体初识
- Java JDBC 之 PreparedStatement
- C#中的异常捕获机制(try catch finally)
- H2 DataBase简单使用
- 组合_____Game( hdu 5725 2016多校第一场)