Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1287    Accepted Submission(s): 475

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

Input

The
first line contains an integer t meaning that there are t test cases(t <=
10).
For each test case:
The first line is an integer n meaning that there are n cities(2 < n <=
1000).
Then n lines follow. Each line contains three integers X, Y and P ( 0 <= X,
Y <= 1000, 0 < P < 100000). (X, Y) is the coordinate of a city and P
is the population of that city.
It is guaranteed that each city has a distinct location.

Output

For
each test case, print a line indicating the above mentioned maximum ratio A/B.
The result should be rounded to 2 digits after decimal point.

Sample
Input

2

4

1 1 20

1 2 30

200 2 80

200 1 100

3

1 1 20

1 2 30

2 2 40

Sample
Output

65.00

70.00

Source

2011 Asia
Beijing Regional Contest

【思路】

最小生成树+边交换。

题目中要求:两城市P之和为A,其他城市路径长度为B,有A/B最小。

简单的想可以求出最小生成树之后一次枚举n条边使徐福同学修路然后求一遍MST,时间为O(NMlogM)。

类比于求次小生成树,我们可以做一遍MST得到总权值tot,预处理出maxcost[][]为两点之间在MST上的最长边。枚举两点ij使徐福修maxcost代表的边(这种情况一定对应着删边后的生成树总权值最小),此时有A=P[i]+P[j],有B=tot-maxcost[i][j],比较得ans。时间为O(N^2)。

【代码】

 #include<cstdio>
#include<cmath>
#include<vector>
#include<cstring>
#include<algorithm>
using namespace std; const int maxn = +;
struct Edge{
int v,next;
double w;
}e[maxn*maxn];
int en,front[maxn];
inline void AddEdge(int u,int v,double w) {
en++; e[en].v=v; e[en].w=w; e[en].next=front[u]; front[u]=en;
} int n,m;
int x[maxn],y[maxn],p[maxn];
double maxcost[maxn][maxn]; struct Edge_Krus{
int u,v;
double w;
bool operator<(const Edge_Krus& rhs) const{
return w<rhs.w;
}
}edges[maxn*maxn];
int f[maxn];
inline int find(int x) {
return x==f[x]? x:f[x]=find(f[x]);
}
inline double dist(int i,int j) {
return sqrt((double)(x[i]-x[j])*(x[i]-x[j])+(double)(y[i]-y[j])*(y[i]-y[j]));
}
double Kruskal()
{
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
edges[m++]=(Edge_Krus) {i,j,dist(i,j)};
for(int i=;i<=n;i++) f[i]=i;
sort(edges,edges+m);
int cnt=;
double res=;
for(int i=;i<m;i++) {
int x=find(edges[i].u),y=find(edges[i].v);
if(x!=y) {
f[x]=y;
res += edges[i].w;
AddEdge(edges[i].u,edges[i].v,edges[i].w);
AddEdge(edges[i].v,edges[i].u,edges[i].w);
if(++cnt==n-) break;
}
}
return res;
} vector<int> nodes;
void dfs(int u,int fa,double facost) {
for(int i=;i<nodes.size();i++) {
int x=nodes[i];
maxcost[x][u]=maxcost[u][x]=max(maxcost[x][fa],facost);
}
nodes.push_back(u);
for(int i=front[u];i>=;i=e[i].next) {
int v=e[i].v;
if(v!=fa) dfs(v,u,e[i].w);
}
} int main() {
int T;
scanf("%d",&T);
while(T--)
{
en=-; m=;
memset(front,-,sizeof(front)); scanf("%d",&n);
for(int i=;i<=n;i++) scanf("%d%d%d",&x[i],&y[i],&p[i]); double tot=Kruskal(); nodes.clear();
memset(maxcost,,sizeof(maxcost));
dfs(,-,); double ans=;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
ans=max(ans,(double)(p[i]+p[j])/(tot-maxcost[i][j]));
printf("%.2lf\n",ans);
}
return ;
}

UValive 5713 Qin 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. UVALive 5713 Qin Shi Huang's National Road System(次小生成树)

    题意:对于已知的网络构建道路,使城市两两之间能够互相到达.其中一条道路是可以免费修建的,问需要修建的总长度B与免费修建的道路所连接的两城市的人口之和A的比值A/B最大是多少. 因为是求A/B的最大值, ...

  3. uvalive 5731 Qin Shi Huang’s National Road System

    题意: 秦始皇要修路使得所有的城市连起来,并且花费最少:有一个人,叫徐福,他可以修一条魔法路,不花费任何的钱与劳动力. 秦始皇想让修路的费用最少,但是徐福想要受益的人最多,所以他们经过协商,决定让 A ...

  4. LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST

    LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

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

  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. Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)

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

  9. [hdu P4081] Qin Shi Huang’s National Road System

    [hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Li ...

随机推荐

  1. Android基础问题汇总

    一.android:gravity 和android:layout_gravity的区别: android;gravity是自己的内容相对于自己的控件的位置,而android:layout_gravi ...

  2. .NET 操作PDF文档以及PDF文件打印摸索总结

    关于生成 PDF 的操作,相信大家的在实际的工作过程中难免会碰到.以前我们通过生成 word 文档来进行文件的打印,但是由于太过依赖 office 软件,因此尝试能不能使用 PDF 进行文件打印. 在 ...

  3. 在mipsel-linux平台上的编译应用SQLite-3.5.9

    sqlite 第一个Alpha版本诞生于2000年5月,是实现了SQL 92标准的一个大子集的嵌入式数据库,其以在一个库中组合了数据库引擎和接口,能将所有数据存储于单个文件中.官方测试表明sqlite ...

  4. 如何改app图标名称

    InfoPlist.strings文件里写上:       CFBundleDisplayName="中文名字";

  5. 菜鸟日记之JSP1

                             JSP全名为Java Server Pages,中文名叫java服务器页面,其根本是一个简化的Servlet设计,它 是由Sun Microsyste ...

  6. Linux进程和进程边界

    1. 进程和线程 2. 手机操作系统的发展 3. 进程的地址空间边界 4. 进程边界的安全围栏: Crash的不可扩延性 5. 进程边界的安全围栏: 全局数据和服务的不可访问性 http://www. ...

  7. asp.net中的<%%>形式的详细用法实例讲解

    asp.net中的代码分离模式我们肯定都不陌生,C#(或者其它语言)写的代码一般不会和设计语言HTML混在一起,但是有的时候也避免不了,这时就会在UI页面里用<%%>来绑定显示.绑定变量数 ...

  8. Linux下安装MySQLdb

    在Linux下使用Python访问MySQL的方法之一是使用MySQLdb module,下面将介绍在Linux下如何安装MySQLdb的过程. (1)下载MySQLdb 从SourceForge.n ...

  9. iOS:Swift界面实例1, 简单界面

    Apple推出了基于Objective-C的新语言Swift. 通过实例, 我们可以很好的感受这门新语言 注意事项: 在XCode6_Beta中, 如果有中文, IDE的自动补全功能就会失效, 所以开 ...

  10. Xcode-程序开发设计-01UIKit 框架

    CGRect中的前缀:CoreGraphics.frameworksUIView中的前缀:User InterFace 属于UIKit的框架NS前缀,NS是NextStep 对象方法:辞去第1响应者- ...