[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
**/




目录
相关文章
|
7月前
|
人工智能 BI
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
【每日一题Day354】LC2316统计无向图中无法互相到达点对数 | 并查集
61 0
|
7月前
|
算法 测试技术 C#
【状态压缩 动态规划 数论】1799. N 次操作后的最大分数和
【状态压缩 动态规划 数论】1799. N 次操作后的最大分数和
|
7月前
PTA-求平方与倒数序列的部分和
求平方与倒数序列的部分和
65 1
|
7月前
【每日一题Day223】LC1130叶值的最小代价生成树 | 贪心 区间dp
【每日一题Day223】LC1130叶值的最小代价生成树 | 贪心 区间dp
51 0
|
Java 测试技术
hdu1231 最大连续子序列【动态规划】
hdu1231 最大连续子序列【动态规划】
43 0
HDU7018.Banzhuan(计算几何+贪心)
HDU7018.Banzhuan(计算几何+贪心)
107 0
HDU7018.Banzhuan(计算几何+贪心)
|
算法 关系型数据库 MySQL
前缀和和差分和dijkstra算法和二分算法和floyd算法
前缀和和差分和dijkstra算法和二分算法和floyd算法
POJ 2689 Prime Distance (埃氏筛 区间筛)
POJ 2689 Prime Distance (埃氏筛 区间筛)
115 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.
152 0
[POJ] John‘s trip | 欧拉回路 | 边序列字典序最小 + 建图
|
C语言
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
HDOJ/HDU Tempter of the Bone(深搜+奇偶性剪枝)
102 0

热门文章

最新文章