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. css左侧固定宽度右侧自适应

    左侧固定宽,右侧自适应屏幕宽: 左右两列,等高布局: 左右两列要求有最小高度,例如:200px;(当内容超出200时,会自动以等高的方式增高) 要求不用JS或CSS行为实现: 仔细分析试题要求,要达到 ...

  2. 《转》xcode创建一个工程的多个taget,便于测试和发布多个版本

    背景:很多时候,我们需要在一个工程中创立多个target,也就是说我们希望同一份代码可以创建两个应用,放到模拟器或者真机上,或者是,我们平时有N多人合作开发,当测试的时候,在A这里装了一遍测A写的那块 ...

  3. j2EE经典面试题

    1. hibernate中离线查询去除重复项怎么加条件? dc.setResultTransformer(Criteria.DISTINCT_ROOT_ENTITY); 2. http协议及端口,sm ...

  4. tomcat6 高并发配置 与优化

    server.xml配置 1.  <Connectorport="8080"protocol="HTTP/1.1" 2.  maxThreads=&quo ...

  5. [坑况]——windows升级node最新版本报错【npm install -g n】

    我本来是下载一个vue-cli的,然后技术日新月异,告知我要先把我的node升级到8以上(目前是v6.1.13) 升级就升级,升级就报错 尝试第一种方法,网上最多的一种方法,估计也是成功最多的一种吧( ...

  6. innobackupex 简单使用笔记

    innobackupex 选项介绍 --backup 备份 --apply-log   应用日志 --move-back  --copy-back 恢复 --export 只导出单个表.前提是使用in ...

  7. 无效类字符串:ProgID: Excel.Application

    网上发现的方案是改注册表,其实用不着那么麻烦,找2种excel文件:xlsx和xls,把默认打开方式都换成你机器上有的程序就行,比如WPS Office的WPS 表格

  8. jsoup 使用总结2--高级用法之 :gt(n)

    jsoup 使用总结2--高级用法之 :gt(n) 大部分时候,我们使用jsoup解析网页的时候,都是直接找到某一类元素,或者按某种selector查询:具体使用方法可以参考jsoup官网文档 部分h ...

  9. 接口和抽象类的区别(JDK1.8)

    1.一个类只能进行单继承,但可以实现多个接口. 2.有抽象方法的类一定是抽象类,但是抽象类里面不一定有抽象方法: 接口里面所有的方法的默认修饰符为public abstract,接口里的成员变量默认的 ...

  10. linux基础-系统安装教程篇(centos6.5)

    一.linux系统简介: Linux是一套免费使用和自由传播的类Unix操作系统,是一个基于POSIX和UNIX的多用户.多任务.支持多线程和多CPU的操作系统.它能运行主要的UNIX工具软件.应用程 ...