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
 
 
代码:
       n个城市,求解max{ A/b } b为次小生成树!  
 //#define LOCAL
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=;
const int inf=0x3f3f3f3f;
struct node
{
int x,y,p;
double dist(const node &cc){
return sqrt((double)(x-cc.x)*(x-cc.x)+(y-cc.y)*(y-cc.y));
}
}sac[maxn]; bool vis[maxn];
bool road[maxn][maxn];
int pre[maxn];
double maxc[maxn][maxn];
double lowcost[maxn];
double map[maxn][maxn];
double res;
void prim(int st,int en){ memset(vis,,sizeof(vis));
memset(road,,sizeof(road));
memset(maxc,,sizeof maxc);
for(int i=;i<en;i++){
lowcost[i]=map[st][i];
pre[i]=st;;
}
vis[st]=;
res=;
for(int i=;i<en;i++)
{
double larger=inf;
int pp=-;
for(int j=;j<en;j++)
{
if(!vis[j]&&larger>lowcost[j])
{
larger=lowcost[j];
pp=j;
}
}
if(-==pp)continue;
road[pp][pre[pp]]=road[pre[pp]][pp]=;
res+=lowcost[pp];
vis[pp]=;
for(int i=;i<en;i++)
{ if(!vis[i]&&lowcost[i]>map[pp][i]){
lowcost[i]=map[pp][i];
pre[i]=pp;
}
//求解生成树的最大边
if(vis[i]&&i!=pp){
maxc[i][pp]=maxc[pp][i]=max(maxc[i][pre[pp]],lowcost[pp]);
}
}
}
return ;
} int main()
{
#ifdef LOCAL
freopen("test.in","r",stdin);
#endif
int tt,nn;
scanf("%d",&tt);
while(tt--){
scanf("%d",&nn);
// memset(map,0,sizeof(map));
for(int i=;i<nn;i++){
scanf("%d%d%d",&sac[i].x,&sac[i].y,&sac[i].p);
map[i][i]=;
for(int j=i-;j>=;--j){
map[i][j]=map[j][i]=sac[i].dist(sac[j]);
}
}
prim(,nn);
double ans=0.0;
for(int i=;i<nn;i++){
for(int j=;j<nn;j++){
if(i!=j)
{
double tol_p=sac[i].p+sac[j].p;
if(road[i][j])
ans=max(tol_p/(res-map[i][j]),ans);
else
ans=max(tol_p/(res-maxc[i][j]),ans);
}
}
}
printf("%.2lf\n",ans);
}
return ;
}

hdu 4081 Qin Shi Huang's National Road System (次小生成树)的更多相关文章

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

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

  3. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

  4. 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: ...

  5. HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形

    题目链接:https://vjudge.net/problem/HDU-4081 Qin Shi Huang's National Road System Time Limit: 2000/1000 ...

  7. hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  8. hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

  9. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

随机推荐

  1. Android 基础

    1. 安卓的平台构建 例如我的手机 内核版本就是ubuntu  为手机硬件提供各种驱动. 架构的简单理解: Application(应用程序层) 我们一般说的应用层的开发就是在这个层次上进行的,当然包 ...

  2. CodeForces 152C Pocket Book

    Time Limit:2000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Prac ...

  3. [HDOJ5289]Assignment(RMQ,二分)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5289 题意:求满足区间内最大值和最小值差为k的区间个数. 预处理出区间的最值,枚举左端点,根据最值的单 ...

  4. [SAP ABAP开发技术总结]结构复用(INCLUDE)

    声明:原创作品,转载时请注明文章来自SAP师太技术博客( 博/客/园www.cnblogs.com):www.cnblogs.com/jiangzhengjun,并以超链接形式标明文章原始出处,否则将 ...

  5. “System.Data.OracleClient.OracleConnection”已过时

    处理办法: 在oracle 安装目录下 找到 Oracle.DataAccess.dll添加引用,然后 using Oracle.DataAccess.Client;其他的都不用动,即可.连接字符串中 ...

  6. 学习笔记day5:inline inline-block block区别

    1. block元素可以包含block元素和inline元素:但inline元素只能包含inline元素.要注意的是这个是个大概的说法,每个特定的元素能包含的元素也是特定的,所以具体到个别元素上,这条 ...

  7. strcpy, memcpy, memset函数

    一. strcpy函数 原型声明:char *strcpy(char* dest, const char *src);   头文件:#include <string.h> 和 #inclu ...

  8. 在beforeAction里redirect无效,Yii2.0.8

    我是在官方GitHub上得到回答,试了一下,确实解决问题了.之前的问题描述: 之前是2.0.3,然后用composer直接升级到2.0.8,就不正常了,以为是我代码的问题,于是再次尝试 用compos ...

  9. Bootstrap文本对齐风格

    在排版中离不开文本的对齐方式.在CSS中常常使用text-align来实现文本的对齐风格的设置.其中主要有四种风格: ☑  左对齐,取值left ☑  居中对齐,取值center ☑  右对齐,取值r ...

  10. HTML的<body>标签详解与HTML常用的控制标记

    一.<body>标签: 用于标记网页的主体,body 元素包含文档的所有内容(比如文本.超链接.图像.表格和列表等等.) 1.body标签中可用的属性: bgcolor="颜色值 ...