HDU 4081 Qin Shi Huang's National Road System [次小生成树]
题意:
秦始皇要建路,一共有n个城市,建n-1条路连接。
给了n个城市的坐标和每个城市的人数。
然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值。
A代表所建的魔法路的连接的城市的市民的人数的和,B 代表n-2条正常路的长度的和。
思路:
这题是次小生成树的变形,所谓次小生成树的核心应该是记录树上节点间的路中最大的边的权重,然后将这条边替换为非最小生成树中的边,然后枚举找到最小值。
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
#include<math.h>
#define INF 0x3f3f3f3f
using namespace std;
struct city{
int x,y,p;
};
city jilu[];
int n,from[];
double dp[][],dis[],total;
bool vis[];
double cal(city a,city b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
void prim(int pos){
vis[pos]=;
for(int i=;i<n;i++){
if(dis[i]>cal(jilu[pos],jilu[i])){
dis[i]=cal(jilu[pos],jilu[i]);
from[i]=pos;
}
}
int next=-;
double mmin=1e32;
for(int i=;i<n;i++){
if(vis[i]==&&mmin>dis[i]){
mmin=dis[i];
next=i;
}
}
if(next>){
for(int i=;i<n;i++){
if(vis[i]){
dp[i][next]=max(dp[i][from[next]],mmin);
}
}
total+=mmin;
prim(next);
}
}
int main()
{
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
for(int i=;i<n;i++){
scanf("%d%d%d",&jilu[i].x,&jilu[i].y,&jilu[i].p);
}
for(int i=;i<n;i++){
dis[i]=1e32;
}
memset(dp,,sizeof(dp));
memset(vis,,sizeof(vis));
total=;
prim();
double ans=-;
for(int i=;i<n;i++){
for(int j=;j<n;j++){
if(i!=j){
ans=max(ans,(jilu[i].p+jilu[j].p)*1.0/(total-max(dp[i][j],dp[j][i])));
}
}
}
printf("%.2lf\n",ans);
}
}
HDU 4081 Qin Shi Huang's National Road System [次小生成树]的更多相关文章
- 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 最小生成树+倍增求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(次小生成树prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- 解决 nginx https反向代理http协议 302重定向localtion到http问题
location /rest { #proxy_redirect off; proxy_set_header Host $host; proxy_set_header X-Real-IP $remot ...
- [MVC] 深入浅出Spring MVC
[MVC] 深入浅出Spring MVC 转:http://4925054.blog.51cto.com/4915054/1176855 Spring MVC主要包括以下要点: 1:由Dispatch ...
- 【svn】 SVN错误:Attempted to lock an already-locked dir
出现这个问题后使用“清理”功能,如果还不行,就直接到上一级目录,再执行“清理”,然后再“更新”. 有时候如果看到某个包里面的文件夹没有SVN的标志,直接用“Ctrl+Delete”手工删除,然后“清理 ...
- PEM文件格式详细解析
PEM文件格式存档 Author:Roson sun sunxiao@tomonline-inc.com Time:2006-4-11 1. 描述: Openssl使用PEM(RFC 1421-14 ...
- visibility和display的区别
大多数人很容易将CSS属性display和visibility混淆,它们看似没有什么不同,其实它们的差别却是很大的. visibility属性用来确定元素是显示还是隐藏的,这用visibility=& ...
- android学习笔记三
GUI==>Graphics User Interface,图形用户界面. android UI 建立在View.ViewGroup基础上,采用组合器设计模式设计View和ViewGoup. V ...
- TextBox(只允许输入字母或者数字)
实现如下: using System;using System.Collections.Generic;using System.Linq;using System.Text;using System ...
- pgsql 9.4修改数据库只读
先进入psql 切换到目标数据库 \c mydb 对于老表 grant usage on schema public to $read_only_user; grant select on all t ...
- [工具开发] Perl 爬虫脚本--从美国国家漏洞数据库抓取实时信息
一.简介 美国国家漏洞数据库收集了操作系统,应用软件的大量漏洞信息,当有新的漏洞出现时,它也会及时发布出来. 由于信息量巨大,用户每次都需要到它的网站进行搜索,比较麻烦.如果能有个工具,每天自动分析它 ...
- grep 与正则表达式
正则表达式只是字符串的一种描述,只有和支持正则表达式的工具相结合才能进行字符串处理.本文以grep为例来讲解正则表达式. grep命令 功能:输入文件的每一行中查找字符串. 基本用法: grep [- ...