Desert King POJ - 2728(最优比率生成树)

2024-04-11 15:08

本文主要是介绍Desert King POJ - 2728(最优比率生成树),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

题目:

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

题意:

给你一个数字N,代表在沙漠中有N个村庄;后面有N组数据,每个数据三个数字X,Y,Z代表村庄在位置(X,Y),且村庄的海拔是Z;

国王想要给每一个村庄供水,没修建一条路的花费是两个村庄之间的海拔之差;

问你想要使每一个村庄的都连接起来,建成的路的平均每公里的花费最小;

思路:

其实这道题是让你求出来  ai / bi  所有和的最小值;

那么假设 ai / bi >=x,那么ai - x*bi >= 0;

这道题可以使用最优比率生成树算法,其中用二分法来枚举  合适的 x 的值;

令F(x)=ai - x*bi,那么这就可以看成一个一元一次的方程式:F(x)=A - x*B,即F(x)=  - B *x + A;

还是看这个图,用二分来枚举f(x) ;

因为是要求最小生成树,所以在每一次的二分判断中要跑一遍prim算法来判断现在的最小生成树的值是否小于0;

如果小于0,那么就是二分区间大了,缩小区间;

如果大于0,那么就是二分区间小了,扩大区间;

代码如下:

#include<math.h>
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std;const double inf=1e18;
const int maxn=1e3+10;int n;
double cost[maxn][maxn];
double dis[maxn][maxn];
double x[maxn],y[maxn],z[maxn];
int vis[maxn];double get_dis(int a,int b)///计算两点之间的距离;
{return sqrt((x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]));
}int check(double x)
{memset(vis,0,sizeof vis);double sum=0,lowcost[maxn];vis[1]=1;for(int i=1; i<=n; i++)///计算最小的每一单位距离的花费;lowcost[i]=cost[1][i]-x*dis[1][i];///prim算法;for(int i=2; i<=n; i++)///最小生成树,n-1条边;{double temp=inf;int k=-1;///记录是否还有最小的边;for(int j=2; j<=n; j++){if(!vis[j]&&lowcost[j]<temp){k=j;temp=lowcost[j];}}if(k==-1)break;vis[k]=1;sum+=temp;for(int j=2; j<=n; j++)///松弛;{if(!vis[j]&&cost[k][j]-x*dis[k][j]<lowcost[j])lowcost[j]=cost[k][j]-x*dis[k][j];}}if(sum>=0)return 1;elsereturn 0;
}int main()
{while(~scanf("%d",&n)&&n){for(int i=1; i<=n; i++){scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);}for(int i=1; i<=n; i++)///存储图的数据;{for(int j=i+1; j<=n; j++){dis[i][j]=dis[j][i]=get_dis(i,j);cost[i][j]=cost[j][i]=fabs(z[i]-z[j]);}}///二分枚举;double l=0.0,r=100.0;while(r-l>=1e-5){double mid=(l+r)/2;if(check(mid))l=mid;///扩大区间;elser=mid;///缩小区间;}printf("%.3f\n",l);///注意输出格式;}return 0;
}

 

这篇关于Desert King POJ - 2728(最优比率生成树)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/894417

相关文章

AI一键生成 PPT

AI一键生成 PPT 操作步骤 作为一名打工人,是不是经常需要制作各种PPT来分享我的生活和想法。但是,你们知道,有时候灵感来了,时间却不够用了!😩直到我发现了Kimi AI——一个能够自动生成PPT的神奇助手!🌟 什么是Kimi? 一款月之暗面科技有限公司开发的AI办公工具,帮助用户快速生成高质量的演示文稿。 无论你是职场人士、学生还是教师,Kimi都能够为你的办公文

pdfmake生成pdf的使用

实际项目中有时会有根据填写的表单数据或者其他格式的数据,将数据自动填充到pdf文件中根据固定模板生成pdf文件的需求 文章目录 利用pdfmake生成pdf文件1.下载安装pdfmake第三方包2.封装生成pdf文件的共用配置3.生成pdf文件的文件模板内容4.调用方法生成pdf 利用pdfmake生成pdf文件 1.下载安装pdfmake第三方包 npm i pdfma

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M

poj 3259 uva 558 Wormholes(bellman最短路负权回路判断)

poj 3259: 题意:John的农场里n块地,m条路连接两块地,w个虫洞,虫洞是一条单向路,不但会把你传送到目的地,而且时间会倒退Ts。 任务是求你会不会在从某块地出发后又回来,看到了离开之前的自己。 判断树中是否存在负权回路就ok了。 bellman代码: #include<stdio.h>const int MaxN = 501;//农场数const int

poj 1258 Agri-Net(最小生成树模板代码)

感觉用这题来当模板更适合。 题意就是给你邻接矩阵求最小生成树啦。~ prim代码:效率很高。172k...0ms。 #include<stdio.h>#include<algorithm>using namespace std;const int MaxN = 101;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int n

poj 1287 Networking(prim or kruscal最小生成树)

题意给你点与点间距离,求最小生成树。 注意点是,两点之间可能有不同的路,输入的时候选择最小的,和之前有道最短路WA的题目类似。 prim代码: #include<stdio.h>const int MaxN = 51;const int INF = 0x3f3f3f3f;int g[MaxN][MaxN];int P;int prim(){bool vis[MaxN];

poj 2349 Arctic Network uva 10369(prim or kruscal最小生成树)

题目很麻烦,因为不熟悉最小生成树的算法调试了好久。 感觉网上的题目解释都没说得很清楚,不适合新手。自己写一个。 题意:给你点的坐标,然后两点间可以有两种方式来通信:第一种是卫星通信,第二种是无线电通信。 卫星通信:任何两个有卫星频道的点间都可以直接建立连接,与点间的距离无关; 无线电通信:两个点之间的距离不能超过D,无线电收发器的功率越大,D越大,越昂贵。 计算无线电收发器D

poj 1502 MPI Maelstrom(单源最短路dijkstra)

题目真是长得头疼,好多生词,给跪。 没啥好说的,英语大水逼。 借助字典尝试翻译了一下,水逼直译求不喷 Description: BIT他们的超级计算机最近交货了。(定语秀了一堆词汇那就省略吧再见) Valentine McKee的研究顾问Jack Swigert,要她来测试一下这个系统。 Valentine告诉Swigert:“因为阿波罗是一个分布式共享内存的机器,所以它的内存访问