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了!!!! 咳咳 最优比率生成 ...
随机推荐
- WinServer2008配置任务计划
window server 2008下配置任务计划 打开window servers 2008下任务计划配置工具 点击“开始”-->“管理工具”-->“任务计划程序”,打开任务计划配置工具 ...
- (转) Hibernate检索方式概述
http://blog.csdn.net/yerenyuan_pku/article/details/70554816 Hibernate检索方式概述 我们在对数据库的操作中,最常用的是select, ...
- listcontrol 加combobox
之前写过一篇(list Control实现单元格编辑)文章,那篇文章不是很完善执行的时候有时会出错,这篇文章经过完善后还加入了Combo Box功能! 这里我就只是晒一下我的代码; 头文件: // L ...
- 怎么用最短时间高效而踏实地学习Python?
之所以写这篇文章,在标题里已经表达得很清楚了.做技术的人都知道,时间就是金钱不是一句空话,同一个技术,你比别人早学会半年,那你就能比别人多拿半年的钱.所以有时候别人去培训我也不怎么拦着,为什么?因为培 ...
- Ansible实现zabbix服务器agent端批量部署
项目需求:由于搭建zabbix,需要每台服务器都需要安装监控端(agent)正常的的操作是一台一台去安装,这样确实有点浪费时间,这里为大家准备了一款开源 的自动化运维工具Ansible,相信大家也很熟 ...
- pandas.DataFrame.rank
原文:https://www.cnblogs.com/sunbigdata/p/7874581.html pandas.DataFrame.rank DataFrame.rank(axis=0 ...
- Python编码格式导致的csv读取错误
Python编码格式导致的csv读取错误(pandas.read_csv) 本文记录python小白我今天遇到的这两个问题(csv.reader和pandas.csv_read): pandas模块“ ...
- Django - 模版语言循环字典
1.可以对传入字典参数做循环显示 views.py中代码: urls.py中代码: html中代码: 在模版语言中,可以对字典进行类似python中的操作(keys,values,items),需要注 ...
- C++ Primer(第4版)-学习笔记-第1部分:基本语言
第1章 快速入门 每个C++程序都包含一个或多个函数,而且必须有一个命名为main. main函数是唯一被操作系统显式调用的函数,main函数的返回值必须是int或者void(无返回值) 函数体是函 ...
- Linux判断
#字符串比较if [ "$1" == "判断条件" ] then echo "$1" elif [ "$1" == &q ...