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): 5023 Accepted Submission(s): 1749
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.
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.
2
4
1 1 20
1 2 30
200 2 80
200 1 100
3
1 1 20
1 2 30
2 2 40
65.00
70.00
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define INF 0x3f3f3f3f*1.0
using namespace std;
double getdistence(int x1,int y1,int x2,int y2){
double xx=sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
return xx;
}
int n;
double kill;
struct Node{
int x,y,p;
}node[1111];
bool visit[1111][1111],vis[1111];
double dis[1111][1111],path[1111][1111];
int pre[1111];
void init(){
memset(visit,0,sizeof(visit));
memset(vis,0,sizeof(vis));
memset(path,0,sizeof(path));
kill=0.0;
for(int i=1;i<=n;i++)
scanf("%d %d %d",&node[i].x,&node[i].y,&node[i].p);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
dis[i][j]=getdistence(node[i].x,node[i].y,node[j].x,node[j].y);
}
void prim(){
double dist[1111];
vis[1]=1;
for(int i=1;i<=n;i++){
dist[i]=dis[1][i];
pre[i]=1;
}
int p,k;
p=-1;
double minn;
for(int i=1;i<n;i++){
minn=INF;
for(int j=1;j<=n;j++){
if(!vis[j]&&minn>dist[j]){
minn=dist[j];
k=j;
}
}
visit[k][pre[k]]=visit[pre[k]][k]=1;
kill+=minn;
vis[k]=1;
for(int j=1;j<=n;j++){
if(!vis[j]&&dist[j]>dis[k][j]){
dist[j]=dis[k][j];
pre[j]=k;
}
if(vis[j]&&j!=k){
path[j][k]=path[k][j]=max(path[j][pre[k]],dist[k]);
}
}
}
}
void solve(){
double ans=0.0;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(j!=i){
if(visit[i][j])
ans=max(ans,(node[i].p+node[j].p)*1.0/(kill-dis[i][j]));
else
ans=max(ans,(node[i].p+node[j].p)*1.0/(kill-path[i][j]));
}
}
}
printf("%.2f\n",ans);
}
int main(){
int t;
scanf("%d",&t);
while(t--){
scanf("%d",&n);
init();
prim();
solve();
}
return 0;
}
HDU 4081 Qin Shi Huang's National Road System 最小生成树的更多相关文章
- HDU 4081 Qin Shi Huang's National Road System(最小生成树/次小生成树)
题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.如今要在全部城市之间修路,保证每一个城市都能相连,而且保证A/B 最大.全部路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i ...
- HDU4081 Qin Shi Huang's National Road System【prim最小生成树+枚举】
先求出最小生成树,然后枚举树上的边,对于每条边"分别"找出这条割边形成的两个块中点权最大的两个 1.因为结果是A/B.A的变化会引起B的变化,两个制约.无法直接贪心出最大的A/B. ...
- 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 次小生成树变种
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 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 树的基本性质 or 次小生成树思想 难度:1
During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...
- 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 ...
- HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】
题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- vue懒加载实现
- java中 flush()方法的作用
flush() 是清空,而不是刷新啊.一般主要用在IO中,即清空缓冲区数据,就是说你用读写流的时候,其实数据是先被读到了内存中,然后用数据写到文件中,当你数据读完的时候不代表你的数据已经写完了,因为还 ...
- 【HDU 6299】Balanced Sequence
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 我们贪心地把每一个括号序列能匹配都按照栈的规则都匹配出来. (直接递增匹配对数*2就可以了 最后栈里面就只剩下类似))))((((( ...
- 高级函数-sign
==========sign函数介绍(补充)=========== sign(n):判断n>0返回1;n=0返回0;n<0返回-1. select sign(10),sign(0) ...
- 怎样获取ios设备的唯一标识
非常多地方都会须要用到唯一标志. 比方: 1. 我们相用一个设备的唯一标志当作用户id,特别是网络游戏,这样就能够省去注冊的麻烦. 2. 想把app相关的文件加密,密钥哪里来的?有些人可能会说hard ...
- UI_UIImagePickerController(读取图片)
创建图片 #pragma mark - 创建 photoImageView - (void)createphotoImageView { self.photoImageView = [[UIImage ...
- 初始化的数值(int、double等)(一)
首先考虑一个具有几个构造函数的MyClass类.如果我们决定在这个类的私有部分加入一个新的数据成员,称为int_data_: class MyClass { public: MyClass() : i ...
- Android程序全然退出的三种方法
1. Dalvik VM的本地方法 android.os.Process.killProcess(android.os.Process.myPid()) //获取PID,眼下获取自己的也仅仅有该 ...
- nodejs中require的路径是一个文件夹时发生了什么
node中使用require的时候如果路径是一个文件夹时,或者特殊的情况require('..');require('.'); 这是node实战这本书里说的情况,但是我在node6.9版本中发现不完全 ...
- c:\Windows\System32\drivers\etc\hosts的作用
c:\Windows\System32\drivers\etc\hosts 是域名解析文件. 可以直接用记事本打开.将IP地址重定向. 格式为:ip地址-空格-域名 可以将一个域名重新定向到一个IP ...