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个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- hdoj 3732 Ahui Writes Word (多重背包)
之前在做背包的题目时看到了这道题,一看,大喜,这不是裸裸的01背包吗!! 然后华丽丽的超时,相信很多人也和我一样没有考虑到数据量的大小. 时隔多日,回过头来看这道题,依旧毫无头绪....不过相比之前 ...
- js获得页面get跳转的参数
通过js获得页面跳转参数 页面通过window.location.href或通过window.parent.location.href进行页面跳转,在新的页面如何获得相应的参数呢? window.lo ...
- web渗透---第一天
了解黑客 黑客: 黑客是一个中文词语,皆源自英文hacker,随着灰鸽子的出现, 灰鸽子成为了很多假借黑客名义控制他人电脑的黑客技术,于是 出现了“骇客”与黑客”分家. 黑客:Hacker 骇 ...
- 自定义 EditText 样式
极力推荐文章:欢迎收藏 Android 干货分享 阅读五分钟,每日十点,和您一起终身学习,这里是程序员Android 本篇文章主要介绍 Android 开发中的部分知识点,通过阅读本篇文章,您将收获以 ...
- Oracle 存储过程批量插入数据
oracle 存储过程批量插入大量数据 declare numCount number; userName varchar2(512); email varchar2(512); markCommen ...
- centos开发环境安装
执行 yum install gcc gcc-c++ gcc-g77 flex bison autoconf automake bzip2-devel zlib-devel ncurses-devel ...
- ubuntu安装伪分布式Hadoop3.1.2
作业要求:https://edu.cnblogs.com/campus/gzcc/GZCC-16SE1/homework/3223 本文是基于已经安装好的ubuntu环境上搭建伪分布式hadoop,在 ...
- 关于JVM内存溢出的原因分析及解决方案探讨
前言:JVM中除了程序计数器,其他的区域都有可能会发生内存溢出. 0.什么是内存溢出 当程序需要申请内存的时候,由于没有足够的内存,此时就会抛出OutOfMemoryError,这就是内存溢出. 1. ...
- mongoDB的CRUD的总结
今天开始接触非关系型数据库的mongoDB,现在将自己做的笔记发出来,供大家参考,也便于自己以后忘记了可以查看. 首先,mongoDB,是一种数据库,但是又区别与mysql,sqlserver.orc ...
- vi编辑器基础指令
vi -- 终端中的编辑器 visual interface ssh-- secure shell vim vi improved-----------------------------打开和新建文 ...