HDU 4081 Qin Shi Huang's National Road System(最小生成树/次小生成树)
题目链接:传送门
题意:
有n坐城市,知道每坐城市的坐标和人口。如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大。全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i以外全部路的花费的和(路径i的花费为0).
分析:
先求一棵最小生成树,然后枚举每一条最小生成树上的边,删掉后变成两个生成树。然后找两个集合中点权最大的两
个连接起来。这两个点中必定有权值最大的那个点。所以直接从权值最大的点開始dfs。
为了使A/B的值最大,则A尽可能大,B尽可能小。所以B中的边一定是MST上去掉一条边后的剩余全部边。首先用O(N^2)算出
MST,然后依次枚举。删去MST上的每一条边。MST变成两棵树T1和T2,然后在剩余的边(即不在MST上的边),以及这条删
去的边中找到该边的两点的权值和最大以及可以连接T1和T2的边。A=删去边后的替换边的两点的权值和,B=删去该边后的MST
的值。求A/B最大。
则A尽可能大,A各自是T1和T2中最大的两个点,则全部点中权值最大的点一定在A中。由此在MST上从权值
最大的点作为root。開始dfs。递归求出子树中的每一个最大的点以及求出A/B的比值,求出最大。
分析转载自:传送门
我的理解。首先非常明显我们是须要求出最小生成树的,然后我们能够枚举边(u,v)中的边,非常明显枚举的边都会
与原来MST中的边形成一个环,由于这个边不在MST中,那么这个边的权值一定是大于MST中连接U,V的边的,因此
我们在这个环里去掉的应该是权值最大的边。
代码例如以下:
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <vector>
using namespace std; const int maxn = 1e3+10; const int inf = 1e9+10; struct point{
int x,y;
}a[maxn]; int head[maxn],par[maxn],peo[maxn]; bool vis[maxn]; int ip,mmax;
double ans ,mst; struct tree{
int u,v;
double w;
tree(){}
tree(int _u,int _v,double _w):u(_u),v(_v),w(_w){}
bool operator < (const struct tree &tmp)const{
return w<tmp.w;
}
}mp[maxn*maxn]; struct nod{
int to,next;
double w;
}edge[maxn*2]; double calu(point a,point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
} void add(int u,int v,double w){
edge[ip].to=v;
edge[ip].w=w;
edge[ip].next=head[u];
head[u]=ip++;
} int find_par(int x){
if(x!=par[x]) return par[x]=find_par(par[x]);
return par[x];
} bool Union(int x,int y){
x=find_par(x);
y=find_par(y);
if(x!=y){
par[x]=y;
return true;
}
return false;
} void init(){
for(int i=0;i<maxn;i++) par[i]=i;
ip=mmax=0;
ans=mst=0;
memset(head,-1,sizeof(head));
memset(vis,0,sizeof(vis));
} int dfs(int root){
vis[root]=1;
int peo_max=peo[root];
for(int i=head[root];i!=-1;i=edge[i].next){
int v=edge[i].to;
if(!vis[v]){
int tmp = dfs(v);
peo_max=max(peo_max,tmp);
ans=max(ans,(tmp+mmax)/(mst-edge[i].w));
}
}
return peo_max;
} int main(){
int t,n,root;
scanf("%d",&t);
while(t--){
init();
scanf("%d",&n);
for(int i=0;i<n;i++){
scanf("%d%d%d",&a[i].x,&a[i].y,&peo[i]);
if(peo[i]>mmax){
mmax=peo[i];
root=i;
}
}
int cnt = 0;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
mp[cnt++]=tree(i,j,calu(a[i],a[j]));
}
}
sort(mp,mp+cnt);
for(int i=0;i<cnt;i++){
if(Union(mp[i].u,mp[i].v)){
mst+=mp[i].w;
add(mp[i].u,mp[i].v,mp[i].w);
add(mp[i].v,mp[i].u,mp[i].w);
}
}
dfs(root);
printf("%.2lf\n",ans);
}
return 0;
}
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 Lim ...
- HDU4081 Qin Shi Huang's National Road System【prim最小生成树+枚举】
先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.因为结果是A/B.A的变化会引起B的变化,两个制约.无法直接贪心出最大的A/B. ...
- 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 (次小生成树)
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 树的基本性质 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】
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)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
随机推荐
- C# IEqualityComparer类型参数写法
最近在使用Union.Except时,由于默认的对比不太好使,所以需要自定义对比器,下面附上代码. class MaterialListComparer : IEqualityComparer< ...
- arm GIC介绍之四【转】
转自:https://blog.csdn.net/sunsissy/article/details/73882718 GIC是ARM体系中重要的组件,在认识到GIC的组成和功能之后,了解到IRQ的大致 ...
- poj1996
多项式计算,有点像母函数,注意最后要判断最高次项是否为0. #include <cstdio> #include <cstring> using namespace std; ...
- 在阿里云申请Symantec免费SSL证书操作流程
2016年阿里云与国内证书颁发机构天威诚信推出了基于Symantec(赛门铁克)的免费SSL证书,有需要免费SSL证书产品的可以前往阿里云进行申请. 申请地址:阿里云云盾证书服务—Symantec免费 ...
- HDR 视频编码
前转换以及后转换:-f xxx.cfg (以及注意工作目录以及路径名/和//) HEVC视频编码:-c xxx.cfg -c xx.cfg(视频的配置文件)
- SpringMVC介绍及参数绑定
本节内容: SpringMVC介绍 入门程序 SpringMVC架构 SpringMVC整合MyBatis 参数绑定 SpringMVC和Struts2的区别 一.SpringMVC介绍 1. 什么是 ...
- ReentrantLock和AQS
AQS(AbstractQueuedSynchronizer)是JDK1.5提供的一个用来构建锁和同步工具的框架,子类包括常用的ReentrantLock.CountDownLatch.Semapho ...
- APIO2018酱油记
苟比主席树太难了学不会 还是把APIO几天的过程记下来吧...免得忘了 DAY -5 去CTSC的人都走了,机房好冷清...只有我.PSB.yasar.Chlience四个人 CSTC辣么难又辣么贵, ...
- 【BZOJ】3683: Falsita
题解 这道题维护方法比较简单,也有点奇妙 我们可以很容易求出经过所有点的路径条数,和初始时分子的大小 然后单点修改的时候,相当于给当前点\(v\)加上\(delta * (siz[v] - 1)\) ...
- notepad++添加Compare插件
背景 两个文本文件内容要进行比较的时候就会用到比较的功能,notepad++绝对是不错的选择 x64版notepad++安装Compare插件 度说点击插件然后选择 "Plugin Mana ...