poj-2728Desert King(最优比率生成树)
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
- 题意:在这么一个图中求一棵生成树,这棵树点权和边权之比最大是多少?
- #include<cstdio>
- #include<cstring>
- #include<cmath>
- #include<iostream>
- #include<algorithm>
- #include<queue>
- #define N 1007
- #define eps 0.000001
- using namespace std;
- int n;
- double dis[N][N],h[N][N],line[N],p[N][N];
- bool vis[N];
- struct Node
- {
- double x,y,h;
- }a[N];
- double get_dis(int x,int y)
- {return sqrt((a[x].x-a[y].x)*(a[x].x-a[y].x)+(a[x].y-a[y].y)*(a[x].y-a[y].y));}
- /*struct cmp
- {
- bool operator()(int x,int y)
- {return line[x]>line[y];}
- };*/
- double prim(double num)
- {
- for (int i=;i<=n;i++)
- for (int j=;j<=n;j++)
- p[i][j]=h[i][j]-dis[i][j]*num;
- //priority_queue<int,vector<int>,cmp>q;
- //while(!q.empty()) q.pop();
- memset(vis,,sizeof(vis));
- memset(line,,sizeof(line));
- line[]=;
- //for (int i=1;i<=n;i++) q.push(i);
- for (int i=;i<=n;i++)
- {
- int u=;
- for (int j=;j<=n;j++)
- if (!vis[j]&&line[j]<line[u]) u=j;
- vis[u]=;
- for (int j=;j<=n;j++)
- if (!vis[j]) line[j]=min(line[j],p[u][j]);
- }
- double sum=;
- for (int i=;i<=n;i++)
- sum+=line[i];
- return sum;
- }
- int main()
- {
- while(~scanf("%d",&n)&&n)
- {
- for (int i=;i<=n;i++)
- scanf("%lf%lf%lf",&a[i].x,&a[i].y,&a[i].h);
- for (int i=;i<=n;i++)
- for (int j=;j<=n;j++)
- {
- dis[i][j]=get_dis(i,j);
- h[i][j]=fabs(a[i].h-a[j].h);
- }
- double l=0.0,r=100.0;
- while(r-l>=eps)
- {
- double mid=(l+r)*1.0/;
- if (prim(mid)>=) l=mid;
- else r=mid;
- }
- printf("%.3f\n",l);
- }
- }
poj-2728Desert King(最优比率生成树)的更多相关文章
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- 【POJ2728】Desert King 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS Memory Limit: 65536K Total Subm ...
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
- POJ 2728(最优比率生成树+01规划)
Dese ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
随机推荐
- 修改本地dns域名对应的 ip
C:\Windows\System32\drivers\etc 打开 hosts 文件 在浏览器 访问 http://a.com,就相当于访问 127.0.0.2 这个ip了
- ajax上传文件以及使用中常见问题处理
<script src="/scripts/ajaxfileupload.js"></script> <script src="/scrip ...
- python代理检测
import socket,threading,os,sys,queue,re socket.setdefaulttimeout(5) path=sys.path[0] if os.path.isfi ...
- WPF中引入外部资源
有时候需要在WPF中引入外部资源,比如图片.音频.视频等,所以这个常见的技能还是需要GET到. 第一步:在VS中创建一个WPF窗口程序 第二步:从外部引入资源,这里以引入图片资源为例 1)新建Reso ...
- Electric Motor Manufacturer - Motor Protection: 5 Questions, 5 Answers
I. Selection principle of motor protectorThe Electric Motor Manufacturer stated that the reasonab ...
- Spring框架中的aop操作之一 及aspectjweaver.jar与aopalliance-1.0.jar下载地址 包含beans 注解context 和aop的约束
(aspect oriented programming面向切面编程) 首先在原有的jar包: 需Spring压缩包中的四个核心JAR包 beans .context.core 和expression ...
- linux 用dd命令读写引导区文件
分类: LINUX 备份MBR,linux下使用如下命令: # dd if=/dev/hda of=/root/linux.bin bs=512 count=1 这里注意使用if=/dev/hda备份 ...
- [LUOGU] 1002 过河卒
题目描述 棋盘上A点有一个过河卒,需要走到目标B点.卒行走的规则:可以向下.或者向右.同时在棋盘上C点有一个对方的马,该马所在的点和所有跳跃一步可达的点称为对方马的控制点.因此称之为"马拦过 ...
- html中footer如何一直保持在页底
最近在开发博客过程中,遇到有些body的height是比window的height要低的,然后就出现了footer在页面中间的尴尬样子.那么这种情况怎么解决呢: 首先,写一个footer标签: < ...
- 【转】4w+1h 教你如何做用户画像
记得14年开始做用户画像的时候,对于用户画像完全没有概念,以为是要画一幅幅图画,经过两年多的学习和理解,渐渐的总结出了一些方法和技巧,在这里就通过4个W英文字母开头和1个H英文字母开头的单词和大家分享 ...