hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)
题目链接: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)的更多相关文章
- 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 ...
- 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 ...
- HDU 4081 Qin Shi Huang's National Road System [次小生成树]
题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...
- 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: ...
- 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 ...
- 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 ...
- 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 ...
- 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 ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- Intent 使用详解
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android Intent 是一个消息传递对象,主要用于组建之间的通讯,例如:启动Activit ...
- 1关于如何用Navicat连接到xampp安装的mysql
打开xampp,打开config打开my.ini. 检索bind-address,后面的数字是你的IP地址 检索 port,后面的数字是端口号 打开Navicat新建连接,选中mysql 连接名随便起 ...
- Java ActionListenner类的一些理解
Java的ActionListenner事实上我去年年这个时候大概就已经接触到了,也学会了比较简单的使用.但却始终不能理解ActionListenner的一系列的运行是怎么维持这么一个联系的? 我产生 ...
- A human being,who loves football and music
---title: aboutdate: 2019-08-09 20:52:27---[A human being,who loves football and music.](https://eel ...
- [原创实践]RedHat Enterprise Linux 5 安装GCC和redis
Redis的安装需要使用GCC,Red Hat Enterprise 5默认是不安装gcc的,需要自己手动安装. 1:查看系统中是否有gcc gcc -v 查看本机linux版本 lsb_releas ...
- WebService1
一.什么是WebService(来源百度百科) Web service是一个平台独立的,低耦合的,自包含的.基于可编程的web的应用程序,可使用开放的XML(标准通用标记语言下的一个子集)标准来描述. ...
- p2p 打洞专场(转)
就像1000个人眼中有1000个哈姆雷特一样,每个人眼中的区块链也是不一样的!作为技术人员眼中的区块链就是将各种技术的融合,包括密码学,p2p网络,分布式共识机制以及博弈论等.我们今天就来讨论一下区块 ...
- SpringBoot:Mybatis + Druid 数据访问
西部开源-秦疆老师:基于SpringBoot 2.1.7 的博客教程 秦老师交流Q群号: 664386224 未授权禁止转载!编辑不易 , 转发请注明出处!防君子不防小人,共勉! 简介 对于数据访问层 ...
- 不可错过的几款GitHub开源项目
工作之余或者周末感觉无聊?不知道干什么?想继续提高技术,但是不知道做什么的同学,看过来,不妨利用闲暇时间来撸几个 GitHub 上还不错的开源项目,本文推荐的开源项目比较适合新手.及对MVP设计模式不 ...
- javaScript基础-01 javascript语法结构
一.基础 字符集 JavaScript程序是用Unicode字符集编写的. .区分大小写 .空格.换行符和格式控制符 .Unicode转义序列 .标准化 二.类型.值和变量 JavaScript的数据 ...