[Nowcoder / POJ2728] 最优比率生成树

2023-11-17

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 2 \leq N \leq 1000 2N1000),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 0x,y<10000, 0 ≤ z < 10000000 0 \leq z \lt 10000000 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 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
**/

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

[Nowcoder / POJ2728] 最优比率生成树 的相关文章

  • CS从头配置电脑清单(软件篇)

    CS从头配置电脑清单 软件篇 假设你电脑丢了 重新搞了一台 怎么从头配置 迅速可以进行高效产出 假设你是Linux ubuntu系统 安装zoom 安装slack 进行其他设备的信息发送 自己给自己发 项目交流 安装截图软件 推荐flame

随机推荐

  • 【Java】QueryWrapper方法解释

    继承自 AbstractWrapper 自身的内部属性 entity 也用于生成 where 条件 及 LambdaQueryWrapper 可以通过 new QueryWrapper lambda 方法获取 queryWrapper lt
  • 加解密

    目录 一 加密基础知识 1 加密函数 密钥 反函数 2 加密 解密 3 对称加密 4 非对称加密 公钥私钥 二 非对称加密 1 大素数分解问题类 1 RSA 2 Rabin 3 Pollard s rho 素数分解算法 2 离散对数问题类
  • 论文笔记-深度估计(5)Unsupervised Monocular Depth Estimation with Left-Right Consistency

    ECCV2016 Unsupervised Monocular Depth Estimation with Left Right Consistency 本文采用无监督学习 没有ground truth 的方法来估计深度 基本思路是匹配好左
  • Graph Neural Network-Based Anomaly Detection in Multivariate Time Series 代码配置及解析

    可以在GPU上跑通的代码 含数据集 我已经放到了以下链接 链接 https pan baidu com s 1gM4KTbRNHzfbGEGgvEjXAw 提取码 e7wu 在服务器上跑 先创建一个虚拟环境 conda create n G
  • 算法——B树,B-树,B+树,B*树全面解析笔记

    算法 B树 B 树 B 树 B 树全面解析笔记 https www cnblogs com lianzhilei p 11250589 html http blog codinglabs org articles theory of mys
  • 【QT】混合UI设计

    虽然利用Designer和代码的设计方式都可以开发GUI 但是毫无疑问的是最有效的开发方式是利用两者进行混合开发 下面这个实验例子来自 QT5 9 C 开发指南 我做了小部分修改 最终效果是这样 图标导入 这次我们要开发的是一个有工具栏 菜
  • 哈希表——哈希表的概念,哈希表的实现(闭散列,开散列)详细解析

    作者 努力学习的少年 个人简介 双非大二 一个正在自学c 和linux操作系统 写博客是总结知识 方便复习 目标 进大厂 如果你觉得文章可以的话 麻烦你给我点个赞和关注 感谢你的关注 种一颗树最好是10年前 其次是现在 目录 哈希概念 哈希
  • 实战怎么用u盘重装系统

    当电脑系统出现故障问题无法进系统的情况下 我们可以通过制作u盘启动盘进pe系统进行修复或者重装系统解决 不过很多网友不知道怎么用u盘重装系统 今天小编就给大家分享一个简单易操作的u盘重装系统教程 具体的步骤如下 1 先在一台可用的电脑上下载
  • nodejs服务后台持续运行三种方法

    网上看到的 用了第二种方式OK的 自己备份保存下 一 利用 forever forever是一个nodejs守护进程 完全由命令行操控 forever会监控nodejs服务 并在服务挂掉后进行重启 1 安装 forever npm inst
  • 微信小程序之内嵌网页(webview)

    微信小程序提供了新的开放能力 它终于开放了在小程序中内嵌HTML页面的功能 从微信小程序基础库1 6 4开始 我们就可以在小程序内放置一个
  • 蓝桥杯大赛— —每日一题(6、走方格)

    走方格 题目描述 在平面上有一些二维的点阵 这些点的编号就像二维数组的编号一样 从上到下依次为第 1 至第 n 行 从左到右依次为第 1 至第 m 列 每一个点可以用行号和列号来表示 现在有个人站在第 1 行第 1 列 要走到第 n 行第
  • Redis可以代替MySQL作为数据库吗

    Redis可以代替MySQL作为数据库吗 当使用Redis作为数据库时 以下是一些基本的代码示例 1 连接到Redis服务器 2 存储和获取数据 3 列表操作 4 有序集合操作 6 键过期和删除 Redis作为数据库时 下面是一些更复杂的代
  • 如何基于G6进行双树流转绘制?

    1 背景 业务背景 CRM系统随着各业务条线对线索精细化分配的诉求逐渐增加 各个条线的流向规则会越来越复杂 各个条线甚至整个CRM的线索流转规则急需一种树形的可视化的图来表达 技术背景 在开发之前考虑了三种方案 原生canvas fabri
  • STM32 同个定时器 采用2个通道输入捕获

    工作中遇到 做点总结 之前看CSDN 找到一种写法 就是 把中断中的 CAPTURE VAL 的值 变成 date1 date2 去保存 但是我的写法不成功 一位大佬帮忙改成功了 总结我的写法错误之处 主函数区别 我把date1 清0 放后
  • java longlong_java Long long

    在Java中执行如下的代码 long number 26012402244 编译的时候会出现一个错误 提示 过大的整数 32322355744 如果在Eclipse中书写上面的代码 提示的是 The literal 26012402244
  • DrawerLayout与FragmentTabHost结合模仿oschina主界面

    1 DrawerLayout实现侧滑菜单 drawerlayout是官方出的侧滑菜单控件 使用起来非常方便 将它当作LinearLayout一样的布局控件 完成布局xml文件
  • 安全(六种核心安全机制-加密、密钥、签名与证书)

    安全要解决什么问题 你都会的密码术 安全机制之对称加密 安全机制之非对称加密 安全机制之密钥交换 安全机制之消息摘要 安全机制之电子签名 安全机制之证书与PKI 一 在典型的场景中 安全主要用于解决4类需求 1 保密 Security Co
  • Load balancer does not have available server for client问题

    Load balancer does not have available server for client问题 是因为消费端没有调用成功服务端 下面四步是必备的 可以检查一番 1 写nacos发现的启动类注解 SpringBootApp
  • statsmodels 笔记:seasonal_decompose 时间序列分解

    1 使用方法 statsmodels tsa seasonal seasonal decompose x model additive filt None period None two sided True extrapolate tre
  • [Nowcoder / POJ2728] 最优比率生成树

    Nowcoder链接 POJ链接 题目描述 David the Great has just become the king of a desert country To win the respect of his people he d