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个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...
随机推荐
- crm 系统项目(三) 业务
1. 项目背景 crm系统是某教育平台正在使用的项目,系统主要为 销售部.运营部.教质部门提供管理平台,随着公司规模的扩展,对公司员工的业务信息量化以及信息化建设越来越重要. crm系统为不同角色的用 ...
- 【BZOJ 1297】[SCOI2009]迷路
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 如果点与点之间的距离都是1的话. 那么T次方之后的矩阵上a[1][n]就是所求答案了. 但是这一题的边权可能会大于1 但最多为10 ...
- Xshell 安装 Xftp
一.下载 Xftp 链接:https://pan.baidu.com/s/1dGeL2gD 密码:as9x 二.安装 Xftp 无脑下一步 三.点击 Xshell 上的新建文件传输 四.弹出 xftp ...
- Android px,dp,pt,sp的差别
px(像素点) mm 等Android不建议用 为什么电脑web开发能够用而Android不建议用? 由于px代表像素点个数,一般电脑分辨率都同样 不管14寸还是15寸都是1366*768而手机分辨率 ...
- C中操作文件的几种模式
使用文件的方式共同拥有12种,以下给出了它们的符号和意义. 文件打开方式 意义 rt 仅仅读打开一个文本文件.仅仅同意读数据 wt 仅仅写打开或建立一个文本文件,仅仅同意写数据 at 追 ...
- nyoj--914--Yougth的最大化(二分查找)
Yougth的最大化 时间限制:1000 ms | 内存限制:65535 KB 难度:4 描述 Yougth现在有n个物品的重量和价值分别是Wi和Vi,你能帮他从中选出k个物品使得单位重量的价值最 ...
- PFILE和SPFILE介绍
一.PFILE Pfile(Parameter File,参数文件)是基于文本格式的参数文件,含有数据库的配置参数. 1.PFILE - initSID.ora(默认PFILE名称),位置在$ORAC ...
- git相关整理
title: git相关整理 toc: false date: 2018-09-24 20:42:55 git merge 和 git merge --no--ff有什么区别? git merge命令 ...
- (转载)所有分类 > 开发语言与工具 > 移动开发 > Android开发 Android中的Service:默默的奉献者 (1)
前言 这段时间在看一些IPC相关的东西,这里面就不可避免的要涉及到service,进程线程这些知识点,而且在研究的过程中我惊觉自己对这些东西的记忆已经开始有些模糊了——这可要不得.于是我就干脆花了点心 ...
- 51nod 1448 二染色问题 (逆向考虑)
题目: 注意,这题不是把一块区域的黑翻成白.白翻成黑. 是把一块区域全部翻成白或者翻成黑. 初始为全白,看能否翻出题中的情况. 我们假设翻转若干次能得到图中的形状,那么我们找出最后一次的翻转,即全W或 ...