【最小生成树】UVA1494Qin Shi Huang's National Road System秦始皇修路
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秦始皇修路的更多相关文章
- UVALive 5713 Qin Shi Huang's National Road System秦始皇修路(MST,最小瓶颈路)
题意: 秦始皇要在n个城市之间修路,而徐福声可以用法术位秦始皇免费修1条路,每个城市还有人口数,现要求徐福声所修之路的两城市的人口数之和A尽量大,而使n个城市互通需要修的路长B尽量短,从而使得A/B最 ...
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- 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(最小生成树+bfs)
题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- 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 ...
- 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 次小生成树变种
Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3 ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- 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 ...
随机推荐
- 使用jdk8 stream 统计单词数
在我的SpringBoot2.0不容错过的新特性 WebFlux响应式编程里面,有同学问如何使用stream统计单词数.这是个好例子,也很典型,在这里补上. 下面的例子实现了从一个文本文件读取(英文) ...
- JavaScript脚本放在哪里
在HTML body部分中的JavaScripts会在页面加载的时候被执行. 在HTML head部分中的JavaScripts会在被调用的时候才执行. ----------------------- ...
- 2MySQL Server 系统架构
2.2MySQL Server 系统架构 总的来说,MySQL 可以看成是二层架构,第一层我们通常叫做SQL Layer,在MySQL 数据库系统处理底层数据之前的所有工作都是在这一层完成的,包括权限 ...
- 教你一步步发布一个开源库到 JCenter
今天想来分享下,如何一步步自己发布一个开源库到 JCenter 这方面的博客网上已经特别多了,所以本篇并不打算仅仅只是记录流程步骤而已,而是尽可能讲清楚,为什么需要有这个步骤,让大伙知其然的同时还知其 ...
- 在SQL Server 2008 Management Studio中修改表字段顺序
有时我们可能需要为一个已存在的数据库表添加字段,并且想让这个字段默认排的靠前一些,这时就需要为表字段重新进行排序,默认情况下在Management Studio中调整顺序并保存时会提示"不允 ...
- 新盘进行LVM的划分
echo "- - -" > /sys/class/scsi_host/host2/scan LVM是 Logical Volume Manager(逻辑卷管理)的简写,它由 ...
- C#程序自动更新软件版本号
最近因为服务器程序管理多,所以在查看服务器程序的时候,只能通过EXE的编译时间来判断服务器程序版本时间,费神伤身啊 现在想了一个方式,在目录下新增一个version文件,里面写上年月日,并且只是在程序 ...
- How Microservices are Transforming Python Development
https://blog.appdynamics.com/engineering/how-microservices-are-transforming-python-development/ Summ ...
- IEEE发布2017年编程语言排行榜:Python高居首位
https://news.cnblogs.com/n/574248 编者按:本文由微信公众号“机器之心”(ID:almosthuman2014)编译,机器之心专注生产 AI 领域专业性内容.本文作者: ...
- 关于django migrations的使用
django 1.8之后推出的migrations机制使django的数据模式管理更方便容易,现在简单谈谈他的机制和一些问题的解决方法: 1.谈谈机制:migrations机制有两个指令,第一个是ma ...