[UVA1494] Qin Shi Huang's National Road System
题目
题解
从今天起我要改邪归正,好好刷题准备联赛!
这是一道经典的最小生成树题目。
枚举每一条边作为道士要修的路,求出包含这条边的最小生成树。
先求出原图的最小生成树。
如果要删的边在最小生成树上,那仍是原来那个最小生成树。
如果不在,便要把这条边加进去。类似次小生成树,删除原最小生成树中这两点间唯一路径上边权最大的边,并把这条边加进去。
我们要预处理最小生成树上两点间路径上边权最大的边,设它的边权为 \(f[u][v]\)
在求最小生成树的同时 借助父节点更新某一节点到已有生成树中其它节点的 \(f\) 值,复杂度 \(O(n^2)\)
代码
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
using namespace std;
const int N = 1005;
typedef double db;
typedef pair<double,int> P;
int w[N],vis[N];
db mp[N][N],a[N],b[N];
int n;
priority_queue< P, vector<P>, greater<P> > que;
db d[N],f[N][N],S;
int fa[N];
void prim(){
d[1]=0; vis[1]=1;
for(int i=2;i<=n;i++)
d[i]=mp[1][i],fa[i]=1,que.push(P(d[i],i));
while(!que.empty()){
int u=que.top().second;
que.pop();
if(vis[u]) continue;
for(int i=1;i<=n;i++)
if(vis[i]) {
if(i==fa[u]) f[i][u]=f[u][i]=mp[u][i];
else f[i][u]=f[u][i]=max(mp[u][fa[u]],f[fa[u]][i]);
}
vis[u]=1; d[u]=0;
S+=mp[u][fa[u]];
for(int v=1;v<=n;v++){
if(v==u) continue;
if(d[v]>mp[u][v])
fa[v]=u,d[v]=mp[u][v],que.push(P(d[v],v));
}
}
for(int i=1;i<=n;i++) vis[i]=0;
}
int main()
{
int T;
scanf("%d",&T);
while(T--){
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf%d",&a[i],&b[i],&w[i]);
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
mp[i][j]=mp[j][i]=sqrt((b[i]-b[j])*(b[i]-b[j])+(a[i]-a[j])*(a[i]-a[j]));
S=0;
prim();
db ans=0.0;
for(int i=1;i<n;i++)
for(int j=i+1;j<=n;j++)
ans=max(ans,(w[i]*1.0+w[j]*1.0)/(S-f[i][j]));
printf("%.2lf\n",ans);
}
return 0;
}
[UVA1494] 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 ...
- UValive 5713 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 次小生成树变种
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- [hdu P4081] Qin Shi Huang’s National Road System
[hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- HDU4081: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 ...
随机推荐
- ASP.NET MVC 实现页落网资源分享网站+充值管理+后台管理(10)之素材管理
源码下载地址:http://www.yealuo.com/Sccnn/Detail?KeyValue=c891ffae-7441-4afb-9a75-c5fe000e3d1c 素材管理模块也是我们这个 ...
- springBoot中使用使用junit测试文件上传,以及文件下载接口编写
本篇文章将介绍如何使junit在springBoot中测试文件的上传,首先先阅读如何在springBoot中进行接口测试. 文件上传操作测试代码 import org.junit.Before; im ...
- maven仓库总结,maven私服搭建,批量mvn eclipse:eclipse
配置pom.xml依赖包时在这里找包的描述: http://search.maven.org/#browse 以java为根目录. mvn archtype:generate -DgroupId=zt ...
- 2019-8-31-C#-如何写-DEBUG-输出
title author date CreateTime categories C# 如何写 DEBUG 输出 lindexi 2019-08-31 16:55:58 +0800 2018-2-13 ...
- springboot多对一关系映射
原文:https://blog.csdn.net/h993438890/article/details/89146483 spring boot项目的创建省略创建两张表t_user 字段 主键id,u ...
- 2019-8-31-dotnet-core-隐藏控制台
title author date CreateTime categories dotnet core 隐藏控制台 lindexi 2019-08-31 16:55:58 +0800 2019-2-1 ...
- dotnet core 使用 PowerShell 脚本
本文告诉大家如何在 dotnet core 通过 Host PowerShell 的方法使用 PowerShell 脚本 本文提供的方法需要在 dotnet core 2.1 和以上的版本,对于 do ...
- CITRIX ADC配置SSL卸载
如上图,将ssl的加密解密放在前端的负载均衡设备上,客户端到VPX的访问都是加密的,VPX到后端的服务器都是http的 Step1:上传证书到VPX,如下图: Step2:创建SSL的虚拟服务器并且绑 ...
- FreeNOS学习2——操作系统是如何启动的
The System Boot Process Explained:https://www.webopedia.com/DidYouKnow/Hardware_Software/BootProcess ...
- Vijos 1206 CoVH之再破难关 [BFS] [位运算]
1.题意:一个由01组成的4*4的矩阵,可以实现相邻元素交换位置的操作,给出初试状态和目标状态,试求最少操作数的方案: 2.输入输出:输入给出初试矩阵和目标矩阵:要求输出最小操作的次数: 3.分析:输 ...