POJ2728 Desert King —— 最优比率生成树 二分法
题目链接:http://poj.org/problem?id=2728
Time Limit: 3000MS | Memory Limit: 65536K | |
Total Submissions: 26878 | Accepted: 7459 |
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
Output
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 <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
#define ms(a,b) memset((a),(b),sizeof((a)))
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e3+; int n;
double x[MAXN], y[MAXN], z[MAXN];
double dis[MAXN][MAXN], height[MAXN][MAXN], d[MAXN][MAXN];
//dis为两点间的距离, height为两点间的高度差。d[i][j] = height[i][j] - L*dis[i][j],作为图的边权。 bool vis[MAXN];
double cost[MAXN];
double prim()
{
ms(vis, );
for(int i = ; i<=n; i++)
cost[i] = d[][i];
vis[] = true; double sum = ;
for(int i = ; i<=n-; i++)
{
int k;
double minn = INF;
for(int j = ; j<=n; j++)
if(!vis[j] && minn>cost[j])
minn = cost[k=j]; vis[k] = true;
sum += cost[k]; //加上边权
for(int j = ; j<=n; j++)
if(!vis[j])
cost[j] = min(cost[j], d[k][j]);
}
return sum;
} bool test(double L)
{
for(int i = ; i<=n; i++)
for(int j = ; j<=n; j++)
d[i][j] = height[i][j]-L*dis[i][j]; return prim()>=;
} int main()
{
while(scanf("%d", &n) && n)
{
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 = ; j<=n; j++)
{
dis[i][j] = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
height[i][j] = fabs(z[i]-z[j]);
} double l = , r = 100.0;
while(l+EPS<=r)
{
double mid = (l+r)/;
if(test(mid))
l = mid + EPS;
else
r = mid - EPS;
}
printf("%.3f\n", r);
}
}
POJ2728 Desert King —— 最优比率生成树 二分法的更多相关文章
- POJ2728 Desert King 最优比率生成树
题目 http://poj.org/problem?id=2728 关键词:0/1分数规划,参数搜索,二分法,dinkelbach 参考资料:http://hi.baidu.com/zzningxp/ ...
- POJ 2728 Desert King 最优比率生成树
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 20978 Accepted: 5898 [Des ...
- 【POJ2728】Desert King 最优比率生成树
题目大意:给定一个 N 个点的无向完全图,边有两个不同性质的边权,求该无向图的一棵最优比例生成树,使得性质为 A 的边权和比性质为 B 的边权和最小. 题解:要求的答案可以看成是 0-1 分数规划问题 ...
- Desert King(最优比率生成树)
Desert King Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 22717 Accepted: 6374 Desc ...
- POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)
题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...
- POJ 2728 Desert King(最优比率生成树, 01分数规划)
题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...
- POJ 2728 Desert King (最优比率树)
题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...
- poj-2728Desert King(最优比率生成树)
David the Great has just become the king of a desert country. To win the respect of his people, he d ...
- POJ 2728 Desert King (最优比例生成树)
POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...
随机推荐
- 【HDOJ5978】To begin or not to begin(概率)
题意:有k个黑球和1个红球,两个轮流抽,抽到红球算赢,问先手赢的概率大还是后手大还是相等 k<=1e5 思路:手算前几项概率 大胆猜想 #include<cstdio> #inclu ...
- C++ assert 的一点说明
断言(ASSERT)的用法 转载自http://www.cnblogs.com/moondark/archive/2012/03/12/2392315.html 我一直以为assert仅仅是个报错函数 ...
- 533. Lonely Pixel II
Given a picture consisting of black and white pixels, and a positive integer N, find the number of b ...
- BZOJ 3675 [Apio2014]序列分割 (斜率优化DP)
题目链接 BZOJ 3675 首先最后的答案和分割的顺序是无关的, 那么就可以考虑DP了. 设$f[i][j]$为做了$i$次分割,考虑前$j$个数之后的最优答案. 那么$f[i][j] = max( ...
- Cocoa开发中, 如何用全局变量
比如在tabbar的开发中,可以某个页面的数据需要在back到此页面后依然有效. 可以用 appDelegate 这个对象来创建你的全局数据 这个内建的对象,在 APP 启动时产生,在 APP 退出时 ...
- Fragment 生命周期怎么来的?
前言 Fragment对于 Android 开发人员来说一点都不陌生,由于差点儿不论什么一款 app 都大量使用 Fragment,所以 Fragment 的生命周期相信对于大家来说应该都非常清晰.但 ...
- [Unit Testing] Test async function with Jasmine
Most of time, when we want to test function call inside a promise, we can do: it('Should be async', ...
- ZT:150条毒鸡汤
1.照照镜子吧,还要什么段子? 2.多年过去,再回忆高考,其实本质上没有考到好与坏的说法,重要的是年轻人在一起,做份试题,然后决定去哪座城市做代购. 3.真正努力过的人,就会明白天赋的重要性. 4.转 ...
- lua 异常 错误处理 pcall
lua 错误处理 匿名函数 if pcall(function () local s=object.data[1]['type'] end) then return object.data[1]['t ...
- mysql 安装与启动
1.下载mysql installer 2.安装 一直点next,直到finish. 3.安装时的配置 安装完后,选择立即开始配置. 选择standard configuration 勾选安装mysq ...