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. css3-------:before和:after的作用

    1.:before和:after的作用就是在指定的元素内容(而不是元素本身)之前或者之后插入一个包含content属性指定内容的行内元素,最基本的用法如下: <!doctype html> ...

  2. 3 sum closest

    Given an array S of n integers, find three integers in S such that the sum is closest to a given num ...

  3. Activiti初学问题,求解

    <userTask id="writeReportTask" name="Write monthly financial report" > < ...

  4. AOP事务解决方案和分布式事务方案

    http://www.cnblogs.com/jianxuanbing/p/7242254.html http://www.cnblogs.com/jianxuanbing/p/7199457.htm ...

  5. 2010_3_1最新 完整 FFMPEG 编译详解

    在网上看了很多编译详解,都很零散.经过自己的编译,解决一些BUG,在此分享自己的一些经验... 话不多说了!直接上贴. 第一步:准备编译平台. 需要 一个 MinGW 和 一个 MSYS 安装包 以及 ...

  6. postgresql to_char 问题

    select create_time from  xxx; select to_char(create_time,'yyyy-MM-dd HH24:mm:ss') as  create_time fr ...

  7. 多线程中操作UI

    遇到过要在工作线程中去更新UI以让用户知道进度,而在多线程中直接调用UI控件操作是错误的做法. 最后解决方法是将操作UI的代码封装,通过Invoke / BeginInvoke 去委托调用. 代码封装 ...

  8. js判断是否下拉刷新

    if(document.body.scrollTop + window.innerHeight>=document.body.clientHeight-10){ this.loadPointsL ...

  9. C语言下double转char*或者std::string,可以精确转换不含多余的0

    char* GetDoubleStr(double value) { char buf[32]={0};//长度可以自定义 sprintf(buf,"%.8f",value);// ...

  10. Linux内核架构与底层--读书笔记

    linux中管道符"|"的作用 命令格式:命令A|命令B,即命令1的正确输出作为命令B的操作对象(下图应用别人的图片) 1. 例如: ps aux | grep "tes ...