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条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...
随机推荐
- 【Python】HackBack(获取暴力破解服务器密码的IP来源)
1.前言 又在0x00sec上翻到好东东. https://0x00sec.org/t/python-hackback-updated/882 帖子里的脚本会得到那些暴力服务器密码失败的IP和用户名, ...
- git 入门常用命令(转)
Git工作流程:D:\projects\Setup2\Setup2\Setup2\Express\SingleImage\DiskImages\DISK1 git clone工作开始之初,可通过git ...
- Jmeter之逻辑控制器(Logic Controller)【转】
Jmeter之逻辑控制器(Logic Controller) 前言: 1. Jmeter官网对逻辑控制器的解释是:“Logic Controllers determine the order in w ...
- 用原生js实现ajax、jsonp
转载: http://www.cnblogs.com/yangheng/p/6065910.html 一.原生js实现ajax $.ajax({ url: '', type: 'post', data ...
- dede列表页读取当前栏目名称
list或者arclist之内使用[field:typename/]之外使用{dede:field name='typename'/}
- Excel学习笔记:sumif、sumifs、countifs函数使用方法
本篇纯粹记录一下sumif.sumifs.countifs函数的使用方法. 一.sumif函数 按条件求和 使用格式:=sumif(条件区域,求和条件,实际求和区域) =SUMIF($B$7:$B$1 ...
- py-faster-rcnn在windows下安装
准备好vs2013+anaconda2+好点的显卡(比如GTX970) 先改造caffe-rfcn以支持cudnn5,得到caffe-rfcn-cudnn5:替换个别代码文件和代码:改.props 再 ...
- PS 切图、抠图
PS切图https://www.cnblogs.com/xiongmanli/p/6079172.html https://tech.youzan.com/cut-boy/?hmsr=toutiao. ...
- 005.Zabbix-Agent客户端安装
一 安装Zabbix-Agent [root@imxhy ~]# rpm -ivh http://repo.zabbix.com/zabbix/3.2/rhel/7/x86_64/zabbix-rel ...
- Java中static块执行时机
Java中static块执行时机 演示例子 在使用static进行初始化的操作,怎么也执行不了!代码如下: public class StaticDemo { public static final ...