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/32768 K (Java/Others) Total Submission(s): 3843 Accepted Submission(s): 1336
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
代码:
n个城市,求解max{ A/b } b为次小生成树!
1 //#define LOCAL
2 #include<cstdio>
3 #include<cstdlib>
4 #include<cstring>
5 #include<cmath>
6 #include<iostream>
7 #include<algorithm>
8 using namespace std;
9 const int maxn=1005;
10 const int inf=0x3f3f3f3f;
11 struct node
12 {
13 int x,y,p;
14 double dist(const node &cc){
15 return sqrt((double)(x-cc.x)*(x-cc.x)+(y-cc.y)*(y-cc.y));
16 }
17 }sac[maxn];
18
19 bool vis[maxn];
20 bool road[maxn][maxn];
21 int pre[maxn];
22 double maxc[maxn][maxn];
23 double lowcost[maxn];
24 double map[maxn][maxn];
25 double res;
26 void prim(int st,int en){
27
28 memset(vis,0,sizeof(vis));
29 memset(road,0,sizeof(road));
30 memset(maxc,0,sizeof maxc);
31 for(int i=0;i<en;i++){
32 lowcost[i]=map[st][i];
33 pre[i]=st;;
34 }
35 vis[st]=1;
36 res=0;
37 for(int i=0;i<en;i++)
38 {
39 double larger=inf;
40 int pp=-1;
41 for(int j=0;j<en;j++)
42 {
43 if(!vis[j]&&larger>lowcost[j])
44 {
45 larger=lowcost[j];
46 pp=j;
47 }
48 }
49 if(-1==pp)continue;
50 road[pp][pre[pp]]=road[pre[pp]][pp]=1;
51 res+=lowcost[pp];
52 vis[pp]=1;
53 for(int i=0;i<en;i++)
54 {
55
56 if(!vis[i]&&lowcost[i]>map[pp][i]){
57 lowcost[i]=map[pp][i];
58 pre[i]=pp;
59 }
60 //求解生成树的最大边
61 if(vis[i]&&i!=pp){
62 maxc[i][pp]=maxc[pp][i]=max(maxc[i][pre[pp]],lowcost[pp]);
63 }
64 }
65 }
66 return ;
67 }
68
69 int main()
70 {
71 #ifdef LOCAL
72 freopen("test.in","r",stdin);
73 #endif
74 int tt,nn;
75 scanf("%d",&tt);
76 while(tt--){
77 scanf("%d",&nn);
78 // memset(map,0,sizeof(map));
79 for(int i=0;i<nn;i++){
80 scanf("%d%d%d",&sac[i].x,&sac[i].y,&sac[i].p);
81 map[i][i]=0;
82 for(int j=i-1;j>=0;--j){
83 map[i][j]=map[j][i]=sac[i].dist(sac[j]);
84 }
85 }
86 prim(0,nn);
87 double ans=0.0;
88 for(int i=0;i<nn;i++){
89 for(int j=0;j<nn;j++){
90 if(i!=j)
91 {
92 double tol_p=sac[i].p+sac[j].p;
93 if(road[i][j])
94 ans=max(tol_p/(res-map[i][j]),ans);
95 else
96 ans=max(tol_p/(res-maxc[i][j]),ans);
97 }
98 }
99 }
100 printf("%.2lfn",ans);
101 }
102 return 0;
103 }
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法
- 直播软件开发如何使用FFMPEG推流并保存在本地
- react-router学习笔记
- 尤大 3 天前发在 GitHub 上的 vue-lit 是啥?
- BFE.dev前端刷题 23. 实现一个sum()方法
- 彻底深刻理解js原型链之prototype,proto以及constructor(一)
- SAP Spartacus取cart的HTTP请求
- 记一次Netty连接池FixedChannelPool连接未释放问题的排查总结
- 神经网络中的蒸馏技术,从Softmax开始说起
- NullInjectorError R3InjectorError(AppModule)[StoreFeatureModule]
- Angular module加载的原理研究
- NullInjectorError No provider for EffectsRootModule!
- Web 中文字体性能优化实践
- Java反射到底慢在哪?不看后悔
- win10 edge 打开闪退问题
- dotnet Open XML 如何判断一份 Office 文档是否被加密