Desert King
Time Limit: 3000MS   Memory Limit: 65536K
Total Submissions: 25310   Accepted: 7022

Description

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

Source

题目链接:POJ 2728

原理与NYOJ那道题相似,只是这次的X集合取值有了更多的限制,即取X的边必须要是一颗生成树。

题目要求的是令$ {\Sigma cost} \over {\Sigma len} $最小,这是显然的,日常生活中肯定是让平均花费越小才越省钱,类比NYOJ的入门题,如何确定${X_i}$的取值呢?不是简单地贪心找最大,而是在最小(最大)生成树中确定它是要找一个最大的比例,而且这题是要找到一个最小的比例,那么我们用最小生成树来做即可。二分的速度没有那种迭代法快,有时候还容易超时……,哦对了这题我用邻接表手写的200W堆和自带的pq都是超时的,不得已去搬了一个邻接矩阵的朴素Prim模版没想到过了,可能是我堆写的丑……

二分代码(2360MS):

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const int M = N * N * 2;
const double eps = 1e-6;
struct info
{
double x, y, z;
} P[N]; bitset<N>vis;
double cost[N][N], len[N][N], lowcost[N], Map[N][N]; inline double getlen(info a, info b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
inline double getcost(info a, info b)
{
return fabs(a.z - b.z);
}
double prim(int n, double k)
{
int i, j;
vis.reset();
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
Map[i][j] = cost[i][j] - k * len[i][j]; for (i = 1; i <= n; ++i)
lowcost[i] = Map[1][i]; double ret = 0;
vis[1] = 1;
lowcost[1] = 1e16; for (i = 1; i < n; ++i)
{
double Min = 1e16;
int pos;
for (j = 1; j <= n; ++j)
{
if (!vis[j] && lowcost[j] < Min)
{
Min = lowcost[j];
pos = j;
}
}
ret += lowcost[pos];
vis[pos] = 1;
for (j = 1; j <= n; ++j)
{
if (!vis[j] && lowcost[j] > Map[pos][j])
lowcost[j] = Map[pos][j];
}
}
return ret;
}
int main(void)
{
int n, i, j;
while (~scanf("%d", &n) && n)
{
for (i = 1; i <= n; ++i)
scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
double Maxk = -1e16;
for (i = 1; i <= n; ++i)
{
for (j = i + 1; j <= n; ++j)
{
double c = getcost(P[i], P[j]);
double l = getlen(P[i], P[j]);
cost[i][j] = cost[j][i] = c;
len[i][j] = len[j][i] = l;
Maxk = max(Maxk, c / l);
}
}
double L = 0, R = Maxk;
double ans = 0;
while (fabs(R - L) >= eps)
{
double mid = (L + R) / 2.0;
if (prim(n, mid) > 0)
{
ans = mid;
L = mid;
}
else
R = mid;
}
printf("%.3f\n", ans);
}
return 0;
}

Dinkelbach迭代法代码:

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <cstdlib>
#include <sstream>
#include <numeric>
#include <cstring>
#include <bitset>
#include <string>
#include <deque>
#include <stack>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
#define INF 0x3f3f3f3f
#define LC(x) (x<<1)
#define RC(x) ((x<<1)+1)
#define MID(x,y) ((x+y)>>1)
#define CLR(arr,val) memset(arr,val,sizeof(arr))
#define FAST_IO ios::sync_with_stdio(false);cin.tie(0);
typedef pair<int, int> pii;
typedef long long LL;
const double PI = acos(-1.0);
const int N = 1010;
const double eps = 1e-6;
struct info
{
double x, y, z;
} P[N]; bitset<N>vis;
double cost[N][N], len[N][N], lowcost[N], Map[N][N];
int pre[N]; inline double getlen(info a, info b)
{
return sqrt((a.x - b.x) * (a.x - b.x) + (a.y - b.y) * (a.y - b.y));
}
inline double getcost(info a, info b)
{
return fabs(a.z - b.z);
}
double prim(int n, double k)
{
/**< 用k值重新赋边权值 */
int i, j;
for (i = 1; i <= n; ++i)
for (j = 1; j <= n; ++j)
Map[i][j] = cost[i][j] - k * len[i][j]; /**< 必要的初始化 */
vis.reset();
for (i = 1; i <= n; ++i)
{
lowcost[i] = Map[1][i];
pre[i] = 1;
}
vis[1] = 1;
double sumcost = 0, sumlen = 0; /**< Prim过程 */
for (i = 1; i < n; ++i)
{
double Min = 1e19;
int pos = 0;
for (j = 1; j <= n; ++j)
{
if (!vis[j] && lowcost[j] < Min)
{
pos = j;
Min = lowcost[j];
}
}
vis[pos] = 1;
sumcost += cost[pre[pos]][pos];
sumlen += len[pre[pos]][pos];
for (j = 1; j <= n; ++j)
{
if (!vis[j] && lowcost[j] > Map[pos][j])
{
lowcost[j] = Map[pos][j];
pre[j] = pos;
}
}
}
return sumcost / sumlen;
}
int main(void)
{
int n, i, j;
while (~scanf("%d", &n) && n)
{
for (i = 1; i <= n; ++i)
scanf("%lf%lf%lf", &P[i].x, &P[i].y, &P[i].z);
for (i = 1; i <= n; ++i)
{
for (j = i + 1; j <= n; ++j)
{
double c = getcost(P[i], P[j]);
double l = getlen(P[i], P[j]);
cost[i][j] = cost[j][i] = c;
len[i][j] = len[j][i] = l;
}
}
double ans = 0, temp = 0;
while (1)
{
temp = prim(n, ans);
if (fabs(ans - temp) < eps)
break;
ans = temp;
}
printf("%.3f\n", ans);
}
return 0;
}

POJ 2728 Desert King(最优比例生成树 二分 | Dinkelbach迭代法)的更多相关文章

  1. POJ 2728 Desert King (最优比例生成树)

    POJ2728 无向图中对每条边i 有两个权值wi 和vi 求一个生成树使得 (w1+w2+...wn-1)/(v1+v2+...+vn-1)最小. 采用二分答案mid的思想. 将边的权值改为 wi- ...

  2. POJ 2728 Desert King 最优比率生成树

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 20978   Accepted: 5898 [Des ...

  3. POJ.2728.Desert King(最优比率生成树 Prim 01分数规划 二分/Dinkelbach迭代)

    题目链接 \(Description\) 将n个村庄连成一棵树,村之间的距离为两村的欧几里得距离,村之间的花费为海拔z的差,求花费和与长度和的最小比值 \(Solution\) 二分,假设mid为可行 ...

  4. POJ 2728 Desert King(最优比率生成树, 01分数规划)

    题意: 给定n个村子的坐标(x,y)和高度z, 求出修n-1条路连通所有村子, 并且让 修路花费/修路长度 最少的值 两个村子修一条路, 修路花费 = abs(高度差), 修路长度 = 欧氏距离 分析 ...

  5. POJ 2728 Desert King (最优比率树)

    题意:有n个村庄,村庄在不同坐标和海拔,现在要对所有村庄供水,只要两个村庄之间有一条路即可,建造水管距离为坐标之间的欧几里德距离,费用为海拔之差,现在要求方案使得费用与距离的比值最小,很显然,这个题目 ...

  6. poj 2728 Desert King (最小比例生成树)

    http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissio ...

  7. POJ 2728 Desert King 01分数规划,最优比率生成树

    一个完全图,每两个点之间的cost是海拔差距的绝对值,长度是平面欧式距离, 让你找到一棵生成树,使得树边的的cost的和/距离的和,比例最小 然后就是最优比例生成树,也就是01规划裸题 看这一发:ht ...

  8. Desert King(最优比率生成树)

    Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 22717   Accepted: 6374 Desc ...

  9. POJ2728 Desert King —— 最优比率生成树 二分法

    题目链接:http://poj.org/problem?id=2728 Desert King Time Limit: 3000MS   Memory Limit: 65536K Total Subm ...

随机推荐

  1. SHOI2001 小狗散步

    题目传送门 感觉这题最大的难点是发现它的解法是二分图最大匹配 主人的路线是固定的,对于每一段的路线,我们可以枚举小狗想去的景点,如果时间够,我们就将这段路线的起点和小狗想去的点连起来 这样就形成了一个 ...

  2. Luogu [P2814] 家谱

    题目链接 这个题不难,但是有点小小坑. 首先并查集肯定能看出来. 然后字符串的话,一开始我想用 hash 来处理,但想了想,离散化不好搞,人也太多了,一不小心就hash重了,还是算了. 然后就想到了S ...

  3. 操作系统(2)_进程管理_李善平ppt

    所有程序都有CPU和io这两部分,即使没有用户输入也有输出. CPU最好特别忙,io空闲无所谓. 程序/数据/状态 三个维度来看进程. 等待的资源可能是io资源或者通信资源(别的进程的答复). 一个进 ...

  4. 在mac下使用python抓取数据

    2015已经过去,这是2016的第一篇博文! 祝大家新年快乐! 但是我还有好多期末考试! 还没开始复习,唉,一把辛酸泪! 最近看了一遍彦祖的文章叫做 iOS程序员如何使用Python写网路爬虫 所以自 ...

  5. XML格式与实体类的转换

    背景 本人头一回写博客,请大家多多关照.通过读取XML文件获取用户管理权限,其中涉及三部分: 1.XML文件的生成: 2.XML文件的读取: 3.XML文件的保存: 如何做 第一步:自己先将XML文件 ...

  6. SpingBoot之配置文件的值注入问题

    我们在这里研究的是以yml配置文件值注入的问题: Person: lastName: 张三 age: 23 boss: false birth: 2018-10-11 maps: {k1: v1,k2 ...

  7. Microsoft .Net framework 4.0出现 安装不成功,错误代码0x80240037 的解决方法

    ,安装Microsoft .Net framework 时出现 解决方法:用QQ管家 之后 以上就解决了

  8. django+xadmin在线教育平台(五)

    3-3 django orm介绍与model设计 上节教程完成后代码(来学习本节前置条件): 对应commit: 留言板前端页面展示.本次内容截止教程3-2结束. 可能现在你还在通过手写sql语句来操 ...

  9. nginx安装与部署

    1:安装工具包 wget.vim和gcc yum install -y wget yum install -y vim-enhanced yum install -y make cmake gcc g ...

  10. 老男孩Python全栈第2期+课件笔记【高清完整92天整套视频教程】

    点击了解更多Python课程>>> 老男孩Python全栈第2期+课件笔记[高清完整92天整套视频教程] 课程目录 ├─day01-python 全栈开发-基础篇 │ 01 pyth ...