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个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- gcc、g++
http://hi.baidu.com/zhangcunli8499/item/257e187360b48b2bd6a89cc6 g++ src/*.cpp -I include/ -I includ ...
- 使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins)
使用 jQuery UI Widget Factory 编写有状态的插件(Stateful Plugins) Note 这一章节的内容是基于 Scott Gonzalez 一篇博客 Building ...
- spring 中事务的PROPAGATION_REQUIRED,Readonly的解释
一.事务传播行为种类 Spring在TransactionDefinition接口中规定了7种类型的事务传播行为, 它们规定了事务方法和事务方法发生嵌套调用时事务如何进行传播 ...
- 【java】 java 集合类UML图
- 在C#中保存Bouncy Castle生成的密钥对
在用Bouncy Castle的C#版API产生公钥和私钥 中产生了一对密钥对,可以用bouncy caslte提供的API进行保存 公钥方面的3个类,具体代码根据命名空间自行查看其源代码: Org. ...
- 【jmeter】浅说 think time
接口每天被5000个人调用,同时在线500人,每天要被调用50000次. 过了没多久测试完成写了一份报告发给项目经理: 并发 | 响应时间 | 应用服务器cpu |数据库服务器cpu |TPS | ...
- 【shell】变量的配置文件
(1)/etc/profile 登录时,会执行.全局(公有)配置,不管是哪个用户,登录时都会读取该文件.(2)/ect/bashrc Ubuntu没有此文件,与之对应的是/ect/bash.b ...
- sqlite 下载的 ZIP 包的区别
https://system.data.sqlite.org/index.html/doc/trunk/www/downloads.wiki sqlite-netFx20-binary-bundle- ...
- wcf stream 不知道长度的情况下,读取stream
http://bbs.csdn.net/topics/360163784 string filepath = @"http://ww4.sinaimg.cn/thumbnail/6741e0 ...
- (转)WEB第三方打印控件[ASP.NET常用工具]
本文转载自:http://blog.csdn.net/chz_cslg/article/details/25415347 在B/S模式开发中,打印是个很大的困扰.无论是采用页面直接输出或者引用WORD ...