【最小生成树】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 ...
随机推荐
- xml与object 之间的ORM
xml映射为object对象,同时object对象,以xml来表示: public class Tools { private static XmlNodeList SelectNodes(strin ...
- Eclipse如何提高开发效率
Ctrl+Shift+J 反向增量查找(和上条相同,只不过是从后往前查) Ctrl+Shift+F4 关闭所有打开的Editer Ctrl+Shift+X 把当前选中的文本全部变为小写 Ctrl+Sh ...
- 手动编译Flume
1.源码下载: 我用的是1.6版,因为加了kafka-sink,下载地址 http://www.apache.org/dyn/closer.cgi/flume/1.6.0/apache-flume-1 ...
- Fiddler - 工具配置及在ios抓取不了https的解决方法
一.首先,官网下载最新版fiddler工具: https://www.telerik.com/fiddler 二.打开fiddler,点击Tools - Options 我电脑上的各项配置如下图(也可 ...
- GPU计算的十大质疑—GPU计算再思考
http://blog.csdn.NET/babyfacer/article/details/6902985 原文链接:http://www.hpcwire.com/hpcwire/2011-06-0 ...
- 移动App开发基本技术面
1.UI布局 1.1.熟悉系统布局基本机制和使用方法 2.界面效果 2.1.熟悉系统提供的所有界面组件 2.2.熟悉各种功能界面效果的实现途径 2,3.动画等特殊UI效果的实现机制 3.网络请求 3. ...
- spring cloud 入门系列五:使用Feign 实现声明式服务调用
一.Spring Cloud Feign概念引入通过前面的随笔,我们了解如何通过Spring Cloud ribbon进行负责均衡,如何通过Spring Cloud Hystrix进行服务断路保护,两 ...
- HashMap在高并发下如果没有处理线程安全会有怎样的安全隐患,具体表现是什么
Hashmap在并发环境下,可能出现的问题: 1.多线程put时可能会导致get无限循环,具体表现为CPU使用率100%: 原因:在向HashMap put元素时,会检查HashMap的容量是否足够, ...
- Scala编程入门---面向对象编程之对象
对象 Object,相当于class单个实例,通常在里面放一些静态的filed或method 第一次调用object方法时候,就会执行object的constructor,也就是Object中不在me ...
- POI实现Excel导入导出
我们知道要创建一张excel你得知道excel由什么组成,比如说sheet也就是一个工作表格,例如一行,一个单元格,单元格格式,单元格内容格式…这些都对应着poi里面的一个类. 一个excel表格: ...