Description

During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China -- they were Qi, Chu, Yan, Han, Zhao, Wei and Qin. Ying Zheng was the king of the kingdom Qin. Through 9 years of wars, he finally conquered all six other kingdoms and became the first emperor of a unified China in 221 BC. That was Qin dynasty -- the first imperial dynasty of China(not to be confused with the Qing Dynasty, the last dynasty of China). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the first emperor " in Chinese.Qin Shi Huang undertook gigantic projects, including the first version of the Great Wall of China, the now famous city-sized mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a story about the road system:

There were n cities in China and Qin Shi Huang wanted them all be connected by n - 1 roads, in order that he could go to every city from the capital city Xianyang. Although Qin Shi Huang was a tyrant, he wanted the total length of all roads to be minimum,so that the road system may not cost too many people's life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could build a road by magic and that magic road would cost no money and no labor. But Xu Fu could only build ONE magic road for Qin Shi Huang. So Qin Shi Huang had to decide where to build the magic road. Qin Shi Huang wanted the total length of all none magic roads to be as small as possible, but Xu Fu wanted the magic road to benefit as many people as possible -- So Qin Shi Huang decided that the value of A/B (the ratio of A to B) must be the maximum, which A is the total population of the two cites connected by the magic road, and B is the total length of none magic roads.

Would you help Qin Shi Huang?

A city can be considered as a point, and a road can be considered as a line segment connecting two points.

Solution

枚举加特效的一条边(u,v),然后通过预处理实现O(1)得到(u,v)上最大边。

白书例题,类似次小生成树的运用。

Code

 #include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
using namespace std;
const int maxn=; int f[maxn][maxn],p[maxn];
int x[maxn],y[maxn],c[maxn];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
struct edge{
int u,v,w;
bool operator<(const edge&a)
const {return w<a.w;}
}g[maxn*maxn];
int head[maxn],e[maxn*],w[maxn*],nxt[maxn*],k;
void adde(int u,int v,int g){
e[++k]=v;w[k]=g;nxt[k]=head[u];head[u]=k;
e[++k]=u;w[k]=g;nxt[k]=head[v];head[v]=k;
}
int dist(int a,int b){
return (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b]);
}
int n,m; int q[maxn],clock;
void dfs(int p,int u){
q[++clock]=u;
for(int i=head[u];i;i=nxt[i]){
int v=e[i];
if(v==p) continue;
for(int j=;j<=clock;j++)
f[v][q[j]]=f[q[j]][v]=max(f[u][q[j]],w[i]);
dfs(u,v);
}
} void clear(){
m=k=clock=;
memset(head,,sizeof(head));
memset(e,,sizeof(e));
memset(w,,sizeof(w));
memset(nxt,,sizeof(nxt));
memset(f,,sizeof(f));
} int main(){
int T;
scanf("%d",&T);
while(T--){
clear();
scanf("%d",&n);
for(int i=;i<=n;i++)
scanf("%d%d%d",&x[i],&y[i],&c[i]),p[i]=i; for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++){
m++;
g[m].u=i,g[m].v=j;
g[m].w=dist(i,j);
}
sort(g+,g+m+); double sum=;
for(int i=;i<=m;i++){
int x=find(g[i].u),y=find(g[i].v);
if(x!=y){
adde(g[i].u,g[i].v,g[i].w);
sum+=sqrt(g[i].w);
p[x]=y;
}
if(k==*(n-)) break;
} dfs(,); double ans=;
for(int u=;u<=n;u++)
for(int v=u+;v<=n;v++){
double ansx=sum;
ansx-=sqrt(f[u][v]);
ansx=(c[u]+c[v])*1.0/ansx;
ans=max(ans,ansx);
}
printf("%.2lf\n",ans);
}
return ;
}

【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路的更多相关文章

  1. UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)

    题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...

  2. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

    Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...

  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(最小生成树+bfs)

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

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

  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 次小生成树变种

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  8. HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏

    Qin Shi Huang's National Road System                                                                 ...

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

随机推荐

  1. 从小故事来谈nginx负载均衡

    负载均衡 负载均衡是任何一个有一定规模的互联网企业都会考虑的问题,负载方式很多,有依靠硬件实现的,也有依靠软件实现负载的. 今天来聊聊使用软件来负载的方式 你可能听过各自负载的方式,比如常见的ngin ...

  2. ## 分享一下Mac(苹果电脑)里面好用的软件!

    该文章主要分享 Mac电脑常用的软件 文章来源于 github小弟调调™的仓库转载 说明 [Open-Source Software][OSS Icon] 表示 开源软件 ,点击进入 开源 仓库: ...

  3. was上的应用程序部分启动的原因

    最近几天为了方便联调,我把两个项目配置到was测试环境上,前几天还好好的,昨天忽然有一个项目反复安装后都呈现部分启动的状态,打开节点一看,偏偏没启动的那个节点就是我需要用的79节点. 这让我很郁闷,硬 ...

  4. CDH安装系统环境准备——系统版本和安装包下载地址指南

    由于Hadoop深受客户欢迎,许多公司都推出了各自版本的Hadoop,也有一些公司则围绕Hadoop开发产品.在Hadoop生态系统中,规模最大.知名度最高的公司则是Cloudera.接下来的日子里, ...

  5. javascript数组特性

    数组是一段线性分配的内存, 它通过整数计算偏移并访问其中的元素. 数组是一种性能出色的数据结构. 1.数组字面量 数组字面量提供了一种非常方便地创建新数组的表示法. 多个用逗号分隔的值的表达式. 数组 ...

  6. Python用pip安装IPython/Jupyter最佳交互环境

    一.Python模块及安装包简介 如果说编程语言是武器,那么Python就是一把双管枪(Python2/Python3),而各种为Python编写的模块和包就是子弹.使用pip来填满我们的武器吧! I ...

  7. Nodejs经验谈

    前言 这里主要说一下之前使用Nodejs开发踩过的坑,只说坑不填坑,那就是赤裸地耍流氓,文中有大量的说明及填坑方法,了解的看官可以直接跳过. PS:说实话,Nodejs的坑确实蛮多的:但是上手简单,扩 ...

  8. scons脚本示例

    import os def list_dir(dir): all_dirs = [] for root, dirs, files in os.walk('./', True): for name in ...

  9. C++笔记019:C++中的const修饰的是一个真正的常量

    原创笔记,转载请注明出处! 点击[关注],关注也是一种美德~ 程序一: 我们知道数组的下标不能为变量,必须是一个确定的值.在C语言中看程序: #define a 10 int main() { //第 ...

  10. SSM博客

    最近在写基于SSM 的博客,用的是spring+springmvc+mybatis搭建的,用的maven+github管理项目,目前完成了登录注册以及用户管理员的增删改查,后续的功能还需要完善,如何建 ...