题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081

题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点。秦始皇希望这所有n-1条路长度之和最短。然后徐福突然有冒出来,说是他有魔法,可以不用人力、财力就变出其中任意一条路出来。

秦始皇希望徐福能把要修的n-1条路中最长的那条变出来,但是徐福希望能把要求的人力数量最多的那条变出来。对于每条路所需要的人力,是指这条路连接的两个城市的人数之和。

最终,秦始皇给出了一个公式,A/B,A是指要徐福用魔法变出的那条路所需人力, B是指除了徐福变出来的那条之外的所有n-2条路径长度之和,选使得A/B值最大的那条。

题解:就是次小生成树稍微改一下就行,这里只能用prim的次小生成树,由于边太多但是点还是1000。

#include <iostream>
#include <cstring>
#include <cstdio>
#include <cmath>
using namespace std;
struct TnT {
int x , y , p;
}T[1010];
double lowcost[1010] , mmp[1010][1010] , maxpath[1010][1010] , cost[1010];
int pre[1010];
bool vis[1010][1010] , has[1010];
double prim(int n) {
lowcost[1] = 0;
pre[1] = 0;
memset(vis , false , sizeof(vis));
memset(has , false , sizeof(has));
has[1] = true;
for(int i = 2 ; i <= n ; i++) {
lowcost[i] = mmp[1][i];
pre[i] = 1;
}
double sum = 0;
for(int i = 2 ; i <= n ; i++) {
int pos = 0;
double MIN = 10000000000000.0;
for(int j = 1 ; j <= n ; j++) {
if(!has[j] && lowcost[j] < MIN) {
MIN = lowcost[j];
pos = j;
}
}
sum += MIN;
vis[pos][pre[pos]] = vis[pre[pos]][pos] = true;
has[pos] = true;
for(int j = 1 ; j <= n ; j++) {
if(has[j] && j != pos) {
maxpath[pos][j] = maxpath[j][pos] = max(maxpath[j][pre[pos]] , lowcost[pos]);
}
if(!has[j]) {
if(mmp[pos][j] < lowcost[j]) {
lowcost[j] = mmp[pos][j];
pre[j] = pos;
}
}
}
}
return sum;
} double getlen(int a , int b) {
return sqrt((T[a].x - T[b].x) * (T[a].x - T[b].x) + (T[a].y - T[b].y) * (T[a].y - T[b].y));
}
int main() {
int t;
scanf("%d" , &t);
while(t--) {
int n;
scanf("%d" , &n);
for(int i = 1 ; i <= n ; i++) {
int u , v , p;
scanf("%d%d%d" , &u , &v , &p);
T[i].x = u , T[i].y = v , T[i].p = p;
cost[i] = 1.0 * p;
}
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= n ; j++) {
mmp[i][j] = getlen(i , j);
}
}
double sum = prim(n);
double val = 0.0;
for(int i = 1 ; i <= n ; i++) {
for(int j = 1 ; j <= n ; j++) {
if(i == j) continue;
if(!vis[i][j]) {
val = max(val , 1.0 * (cost[i] + cost[j]) / (sum - maxpath[i][j]));
}
else {
val = max(val , 1.0 * (cost[i] + cost[j]) / (sum - mmp[i][j]));
}
}
}
printf("%.2lf\n" , val);
}
return 0;
}

hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)的更多相关文章

  1. HDU 4081 Qin Shi Huang's National Road System 次小生成树变种

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  2. hdu 4081 Qin Shi Huang's National Road System (次小生成树的变形)

    题目:Qin Shi Huang's National Road System Qin Shi Huang's National Road System Time Limit: 2000/1000 M ...

  3. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

  4. HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA

    原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 Qin Shi Huang's National Road System Time Limit: ...

  5. hdu 4081 Qin Shi Huang's National Road System (次小生成树)

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  7. HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形

    题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...

  8. hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  9. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

随机推荐

  1. DesignPattern系列__01SingletonResponsibility

    单一职责原则 单一职责原则:一个类应该只有一个原因引起改变,即一个类应该只负责一个业务逻辑. 问题由来:类T负责t1, t2两个职责,当因为t1j对类T修改的时候,可能导致类T出现问题而影响职责t2. ...

  2. AI and Robot

    Have you ever seen a movie called "Ex Machina"?  I like this movie very much. Artificial i ...

  3. 一文了解:Redis基础类型

    Redis基础类型 Redis特点 开源的,BSD许可高级的key-value存储系统 可以用来存储字符串,哈希结构,链表,集合 安装 windows:https://github.com/micro ...

  4. koa2基于stream(流)进行文件上传和下载

    阅读目录 一:上传文件(包括单个文件或多个文件上传) 二:下载文件 回到顶部 一:上传文件(包括单个文件或多个文件上传) 在之前一篇文章,我们了解到nodejs中的流的概念,也了解到了使用流的优点,具 ...

  5. 【Java例题】7.4 文件题1-学生成绩排序

    4.学生成绩排序.已有一个学生成绩文件,含有多位学生的成绩:读取这个文件中的每位学生的成绩,然后排序:最后将这些排好序的成绩写到另一个文件中. package chapter7; import jav ...

  6. 一文读懂JS中的原型和原型链(图解)

    讲原型的时候,我们应该先要记住以下几个要点,这几个要点是理解原型的关键: 1.所有的引用类型(数组.函数.对象)可以自由扩展属性(除null以外). 2.所有的引用类型都有一个’_ _ proto_ ...

  7. [转载]ActiveMQ实现负载均衡+高可用部署方案

    转载于 http://www.open-open.com/lib/view/open1400126457817.html 一.架构和技术介绍 1.简介 ActiveMQ 是Apache出品,最流行的, ...

  8. javaScript基础-02 javascript表达式和运算符

    一.原始表达式 原始表达式是表达式的最小单位,不再包含其他表达式,包含常量,直接量,关键字和变量. 二.对象和数组的初始化表达式 对象和数组初始化表达式实际上是一个新创建的对象和数组. 三.函数表达式 ...

  9. Mac 安装 homebrew 流程 以及 停在 Updating Homebrew等 常见错误解决方法

    懒人操作顺序:S_01>>>S_02>>>S_03 首先这是homebrew的官网 https://brew.sh/index_zh-cn 安装方法是在终端中输入 ...

  10. 读取某个目录下的所有图片并显示到pictureBox

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...