POJ 2728(最优比率生成树+01规划)
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 25729 | Accepted: 7143 |
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<iostream>
#include<cstdio>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<cstring>
#include<map>
#include<stack>
#include<set>
#include<vector>
#include<algorithm>
#include<string.h>
typedef long long ll;
typedef unsigned long long LL;
using namespace std;
const int INF=0x3f3f3f3f;
const double eps=0.0000000001;
const int N=+;
const int MAX=+;
int vis[N];
double x[N],y[N],z[N];
double w[MAX][MAX],v[MAX][MAX];
double low[N];
int n;
double fun(double a,double b,double c,double d){
double ans=(a-c)*(a-c)+(b-d)*(b-d);
return sqrt(ans);
}
int prime(double x){
double sum=;
memset(vis,,sizeof(vis));
for(int i=;i<n;i++)low[i]=v[][i]-x*w[][i];
vis[]=;
for(int i=;i<n;i++){
int k;
double maxx=INF*1.0;
for(int j=;j<n;j++)if(vis[j]==&&low[j]<maxx){
maxx=low[j];
k=j;
}
if(maxx==1.0*INF)break;
sum=sum+maxx;
vis[k]=;
for(int j=;j<n;j++)
if(vis[j]==&&low[j]>v[k][j]-x*w[k][j])
low[j]=v[k][j]-x*w[k][j];
}
if(sum>)return ;
else
return ;
}
int main(){
while(scanf("%d",&n)!=EOF){
if(n==)break;
double maxx=;
double minn=INF*1.0;
for(int i=;i<n;i++)scanf("%lf%lf%lf",&x[i],&y[i],&z[i]);
for(int i=;i<n;i++)
for(int j=i+;j<n;j++){
double t=fun(x[i],y[i],x[j],y[j]);
w[i][j]=w[j][i]=t;
v[i][j]=v[j][i]=fabs(z[i]-z[j]);
maxx=max(v[i][j],maxx);
minn=min(minn,t);
}
double low=0.0;
double high=maxx/minn;
double ans;
while(low+eps<high){
double mid=(low+high)/;
if(prime(mid)){
ans=mid;
low=mid;
}
else
high=mid;
}
printf("%.3f\n",ans);
}
}
POJ 2728(最优比率生成树+01规划)的更多相关文章
- Desert King (poj 2728 最优比率生成树 0-1分数规划)
Language: Default Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22113 A ...
- poj 2728 最优比率生成树
思路:设sum(cost[i])/sum(dis[i])=r;那么要使r最小,也就是minsum(cost[i]-r*dis[i]);那么就以cost[i]-r*dis[i]为边权重新建边.当求和使得 ...
- POJ 2728 Desert King(最优比率生成树 01分数规划)
http://poj.org/problem?id=2728 题意: 在这么一个图中求一棵生成树,这棵树的单位长度的花费最小是多少? 思路: 最优比率生成树,也就是01分数规划,二分答案即可,题目很简 ...
- poj 2728 最优比例生成树(01分数规划)模板
/* 迭代法 :204Ms */ #include<stdio.h> #include<string.h> #include<math.h> #define N 1 ...
- POJ 2728 Desert King ★(01分数规划介绍 && 应用の最优比率生成树)
[题意]每条路径有一个 cost 和 dist,求图中 sigma(cost) / sigma(dist) 最小的生成树. 标准的最优比率生成树,楼教主当年开场随手1YES然后把别人带错方向的题Orz ...
- poj 2728 Desert King (最优比率生成树)
Desert King http://poj.org/problem?id=2728 Time Limit: 3000MS Memory Limit: 65536K Descripti ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- [转]01分数规划算法 ACM 二分 Dinkelbach 最优比率生成树 最优比率环
01分数规划 前置技能 二分思想最短路算法一些数学脑细胞? 问题模型1 基本01分数规划问题 给定nn个二元组(valuei,costi)(valuei,costi),valueivaluei是选择此 ...
- poj2728 Desert King(最小生成树+01分数规划=最优比率生成树)
题意 n个点完全图,每个边有两个权值,求分数规划要求的东西的最小值. (n<=1000) 题解 心态炸了. 堆优化primT了. 普通的就过了. 我再也不写prim了!!!! 咳咳 最优比率生成 ...
随机推荐
- 更改计算机名后DB2不能启动的解决方法
1.找到以下位置目录下相应的文件db2nodes.cfg C:\Documents and Settings\All Users\Application Data\IBM\DB2\DB2COPY1\D ...
- Mysql 之show status数据详解
状态名 作用域 详细解释 Aborted_clients Global 由于客户端没有正确关闭连接导致客户端终止而中断的连接数 Aborted_connects Global 试图连接到MySQL服务 ...
- Eclipse安装和使用TFS
第一步下载Tfs插件 去微软官网下载https://www.microsoft.com/en-us/download/details.aspx?id=4240 点击 选择下载 随便放置到一个本地或者服 ...
- S-HR之OSF
1):getWorkDayCount ->ArrayList data = (ArrayList) com.kingdee.shr.rpts.ctrlreport.osf.OSFExecutor ...
- ZOJ 3180 Number Game(模拟,倒推)
题目 思路: 先倒推!到最后第二步,然后: 初始状态不一定满足这个状态.所以我们要先从初始状态构造出它出发的三种状态.那这三种状态跟倒推得到的状态比较即可. #include<stdio.h&g ...
- Array.prototype.slice.call()的理解
最近在看廖雪峰的JS课程,浏览器中的操作DOM的那一章,有这样一道题. JavaScript Swift HTML ANSI C CSS DirectX <!-- HTML结构 --> & ...
- =、==、is、id(内容)
= 赋值 == 比较值是否相等 is 比较.比较的是内存地址 id(内容) 测出内存地址
- 如何在docker和宿主机之间复制文件
如何在docker和宿主机之间复制文件 最近在用Docker布署hadoop,要将文件上传到HDFS首先文件得在Docker容器中吧,网上提供的方法差不多有三种 1.用-v挂载主机数据卷到容器内 ...
- 6)STM32使用HAL库实现modbus的简单通讯
1.判断地址.校验 2.读取本机数据并校验打包 3.发送数据包 4.本机数据长度比要读取的长度短怎么办 4.校验错误怎么办
- 赛门铁克扩展验证EV SSL证书
申请EV SSL证书,将接受最严格验证企业域名所有权和企业身份信息,属于最高信任级别扩展验证(EV)的 EV SSL证书,最高达256位自适应加密.Symantec不仅提供先进的SSL加密技术,同 ...