[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim
Posted PushyTao
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim相关的知识,希望对你有一定的参考价值。
题目描述
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.
输入描述:
There are several test cases. Each test case starts with a line containing a number N ( 2 ≤ N ≤ 1000 2 \\leq N \\leq 1000 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 \\leq x, y \\lt 10000 0≤x,y<10000, 0 ≤ z < 10000000 0 \\leq z \\lt 10000000 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.
输出描述:
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.
示例1
输入
4
0 0 0
0 1 1
1 1 2
1 0 3
0
输出
1.000
有
n
n
n个点,其中,每个点给出位置坐标
(
x
,
y
)
(x,y)
(x,y)以及高度
z
z
z,两点之间的距离为两点之间的欧几里得距离
两点之间建立一条路的代价为两点之间的高度差,问将
n
n
n个点联通的情况下,求出最大的
c
o
s
t
/
d
i
s
cost / dis
cost/dis
如果令maxVal = cost / dis 则有 => cost = maxVal * dis现在问题就转化为找到最大的
cost - maxVal * dis
找这个值的时候,我们可以二分答案,即二分maxVal(0,r)///
由于数据范围只有1000所以我们可以先将两两之间的距离以及花费全部求出,在这个过程中,二分的右边界就顺其自然地被求出
然后就可以对maxVal进行二分
二分里面就是一个prim,其中对于一个给定的maxVal,就可以求出任意两点之间的距离
Code:
// Problem: Desert King
// Contest: POJ - Beijing 2005
// URL: http://poj.org/problem?id=2728
// Memory Limit: 65 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)
double cost[1007][1007];
double dis[1007][1007];
int n;
struct node {
double u, v;
double h;
} a[1007];
double calDis(int x, int y) {
double ret = 0;
ret = (a[x].u - a[y].u) * (a[x].u - a[y].u);
ret += (a[x].v - a[y].v) * (a[x].v - a[y].v);
return sqrt(ret);
}
double vis[1007];
double edge[1007];
double dist[1007][1007];
bool check(double mid) {
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
dist[i][j] = dist[j][i] = cost[i][j] - mid * dis[i][j];
}
}
double sum = 0;
int cur = 1;
memset(vis, 0, sizeof vis);
for (int i = 1; i <= n; i++) edge[i] = inf;
edge[1] = 0;
vis[1] = true;
for (int k = 1; k < n; k++) {
double mini = inf;
int t;
for (int i = 1; i <= n; i++) {
if (!vis[i]) {
if (dist[cur][i] < edge[i]) {
edge[i] = dist[cur][i];
}
if (edge[i] < mini) {
mini = edge[i];
t = i;
}
}
}
vis[t] = 1;
sum += mini;
cur = t;
}
if (sum <= 0) return true;
return false;
}
int main() {
while (cin >> n && n) {
for (int i = 1; i <= n; i++) {
// cin >> a[i].u >> a[i].v >> a[i].h;
a[i].u = read, a[i].v = read, a[i].h = read;
}
double l = 0, r = 0, ans = inf;
for (int i = 1; i <= n; i++) {
for (int j = i + 1; j <= n; j++) {
cost[i][j] = cost[j][i] = fabs(a[i].h - a[j].h);
dis[i][j] = dis[j][i] = calDis(i, j);
r = max(r, cost[i][j] / dis[i][j]);
}
}
while (r - l >= 0.000001) {
double mid = (l + r) / 2.0;
if(check(mid)) {
r = mid;
ans = min(ans,mid);
}else l = mid;
}
printf("%.3f\\n", ans);
}
return 0;
}
/**
4
0 0 0
0 1 1
1 1 2
1 0 3
0
1.000
**/
以上是关于[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim的主要内容,如果未能解决你的问题,请参考以下文章