[Nowcoder / POJ2728] 最优比率生成树 | 二分 + prim

简介: 有n个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的cost/dis

Nowcoder链接

POJ链接


题目描述


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 ),which is the number of villages. Each of the following N lines contains three integers, x, y and z (0 ≤ x , y < 10000, 0z<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个点,其中,每个点给出位置坐标( x , y ) 以及高度z ,两点之间的距离为两点之间的欧几里得距离

两点之间建立一条路的代价为两点之间的高度差,问将n 个点联通的情况下,求出最大的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
**/




目录
相关文章
|
5月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
26 0
|
5月前
【每日一题Day223】LC1130叶值的最小代价生成树 | 贪心 区间dp
【每日一题Day223】LC1130叶值的最小代价生成树 | 贪心 区间dp
25 0
|
12月前
51nod 1711 平均数(二分 + 树状数组好题)
51nod 1711 平均数(二分 + 树状数组好题)
76 0
|
算法 关系型数据库 MySQL
前缀和和差分和dijkstra算法和二分算法和floyd算法
前缀和和差分和dijkstra算法和二分算法和floyd算法
|
算法
Dijkstra算法最小堆优化
Dijkstra算法最小堆优化
108 0
Dijkstra算法最小堆优化
AcWing 3797. 最大化最短路(排序优化+最短路)
AcWing 3797. 最大化最短路(排序优化+最短路)
58 0
|
机器学习/深度学习
[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图
Description Little Johnny has got a new car. He decided to drive around the town to visit his friends. Johnny wanted to visit all his friends, but there was many of them. In each street he had one friend. He started thinking how to make his trip as short as possible.
121 0
[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图
|
人工智能 Prometheus Cloud Native
牛客第五场 B Graph最小异或生成树
这道题涉及到最小异或生成树,要理解这个首先要明白 01字典树 关于01字典树呢,先来一道板子题hdu4825 ==》 不方便跳转的同学们可以看下面的题 Problem Description Zeus 和 Prometheus 做了一个游戏,Prometheus 给 Zeus 一个集合,集合中包含了N个正整数,随后 Prometheus 将向 Zeus 发起M次询问,每次询问中包含一个正整数 S ,之后 Zeus 需要在集合当中找出一个正整数 K ,使得 K 与 S 的异或结果最大。Prometheus 为了让 Zeus 看到人类的伟大,随即同意 Zeus 可以向人类求助。
101 0
牛客第五场 B Graph最小异或生成树
|
存储 算法 C++
对最小生成树的认识及模板题
一周前对 树 这个词从新认识了一下,之前一直有听说过,但是很显然,仅仅是听说。。
对最小生成树的认识及模板题