题目链接:传送门

题意:

有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&#39;s National Road System(最小生成树/次小生成树)的更多相关文章

  1. HDU 4081 Qin Shi Huang&#39;s National Road System 最小生成树

    点击打开链接题目链接 Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Lim ...

  2. HDU4081 Qin Shi Huang&#39;s National Road System【prim最小生成树+枚举】

    先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.因为结果是A/B.A的变化会引起B的变化,两个制约.无法直接贪心出最大的A/B. ...

  3. 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: ...

  4. 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 ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. 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 ...

  9. hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

随机推荐

  1. keepalived启动不成功,状态一直是inactive(dead) 的解决办法以及keepalived高版本没有rc.d目录,虚拟VIP无法访问问题

    安装配置教程我就不说了,网上很多,这里只给出我遇到的两个坑: 1 rc.d目录 ,kp在1.4版本之后rc.d要去解压之后的源码包里去找,make之后的目录里面没有了,我使用的是2.0.13最新版本, ...

  2. 『Numpy学习指南』Matplotlib绘图

    数据生成: import numpy as np import matplotlib.pyplot as plt func = np.poly1d(np.array([,,,])) func1 = f ...

  3. Convert Expression to Reverse Polish Notation

    Given an expression string array, return the Reverse Polish notation of this expression. (remove the ...

  4. 纠正几个NANDFLASH很容易犯的错误【转】

    转自:https://blog.csdn.net/pengrui18/article/details/32337297 今天在看别人如何根据物理地址计算NANDFLASH的列地址(column add ...

  5. WPF为stackpanel设置滚动条

    最新遇到ItemControl控件增加滚动条功能,找半天还是在StackPanel模板外添加的. <ScrollViewer x:Name="scrolls" Vertica ...

  6. node+webpack环境搭建 vue.js 2.0 基础学习笔记

    npm install -g vue //全局安装vue npm install -g webpack //全局安装webpack npm install -g vue-cli //全局安装vue-c ...

  7. 移动端调试利器之vconsole

    说明 由于移动端项目在手机中调试时不能使用chrome的控制台,而vconsole是对pc端console的改写 使用方法 使用 npm 安装: npm install vconsole 使用webp ...

  8. SqlServer中 SET DATEFIRST更改

    在 SQL Server 中默认情况下,每周的开始都是从周日开始算起的,如果默认星期一呢? 这里有三种方式可以解决这个问题: 一:直接通过 SET DATEFIRST VALUE 来更改重新生成新的 ...

  9. SqlServer自定义函数Function中调用with as

    SET QUOTED_IDENTIFIER ON 标识符可以由双引号分隔,而文字必须由单引号分隔 SET QUOTED_IDENTIFIER OFF 标识符不可加引号,且必须遵守所有 Transact ...

  10. APP性能测试开始之旅

    你是不是也跟我一样在工作中存在着同样的问题,APP版本在上线后不断的会有市场人员或者用户反馈页面加载慢,进入页面loading很久(实际我们设置的加载超时是15秒,15秒内加载出内容则显示,15秒外未 ...