HDU4081: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): 10567 Accepted Submission(s): 3727
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081
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
题意:
给出一个无向图以及n个点的坐标以及点对应的权值。现在选出一条道路,使得修建它的费用为0,问A/B的最大值是多少,其中A为选出道路的两个端点的权值和,B为将图连通其它道路的花费。
题解:
想法就是枚举每条道路然后来计算,但是每次都求一次最小生成树有点麻烦,可以这样考虑:
如果这条道路在原图最小生成树中,那么答案就是(d[u]+d[v]) / (sum-dis[u][v]);
如果不在最小生成树中,那么答案也是(d[u]+d[v]) / (sum-dis[u][v]),我们加入这条边后,图必定会形成一个环,那么我们应该去掉最小生成树中u到v路径上的最大边权值。
那么,上面两个式子的dis含义都为点u与点v之间的最大边权值,关键把这个算出来就行了。
计算的话dfs一次就行了,O(n^2)就可以完成,只需要枚举已经算出来的点来进行更新。其实这就是求最小瓶颈路。
具体代码如下:
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <iostream>
#include <queue>
#include <cmath>
#define INF 0x3f3f3f3f
using namespace std;
typedef long long ll;
const int N = ;
struct node{
int x,y;
}p[N];
int t,n,tot;
int a[N];
double dis(int x,int y){
return sqrt((p[x].x-p[y].x)*(p[x].x-p[y].x)+(p[x].y-p[y].y)*(p[x].y-p[y].y));
}
struct Edge{
int u,v;double w;
bool operator < (const Edge &A)const{
return w<A.w;
}
}e[N*N];
int f[N],mp[N][N];
int find(int x){
return f[x]==x?f[x]:f[x]=find(f[x]);
}
double Kruskal(){
double ans=;
for(int i=;i<=n+;i++) f[i]=i;
for(int i=;i<=tot;i++){
int u=e[i].u,v=e[i].v;
int fx=find(u),fy=find(v);
if(fx==fy) continue ;
f[fx]=fy;
mp[u][v]=mp[v][u]=;
ans+=e[i].w;
}
return ans ;
}
double d[N][N];
int check[N];
void dfs(int u,int fa){
for(int i=;i<=n;i++){
if(check[i]) d[i][u]=d[u][i]=max(d[i][fa],dis(fa,u));
}
check[u]=;
for(int i=;i<=n;i++){
if(mp[i][u] && i!=fa) dfs(i,u);
}
}
int main(){
cin>>t;
while(t--){
scanf("%d",&n);
for(int i=;i<=n;i++){
int x,y;
scanf("%d%d%d",&x,&y,&a[i]);
p[i]=node{x,y};
}
tot = ;
for(int i=;i<=n;i++)
for(int j=i+;j<=n;j++)
e[++tot]=Edge{i,j,dis(i,j)};
memset(mp,,sizeof(mp));
sort(e+,e+tot+);
double sum=Kruskal();
memset(d,,sizeof(d));
memset(check,,sizeof(check));
dfs(,-);
double ans = ;
for(int i=;i<=tot;i++){
int u=e[i].u,v=e[i].v;
double w=e[i].w;
ans=max(ans,(a[u]+a[v])/(sum-d[u][v]));
}
printf("%.2lf\n",ans);
}
return ;
}
HDU4081:Qin Shi Huang's National Road System (任意两点间的最小瓶颈路)的更多相关文章
- 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 ...
- HDU4081 Qin Shi Huang's National Road System 2017-05-10 23:16 41人阅读 评论(0) 收藏
Qin Shi Huang's National Road System ...
- hdu-4081 Qin Shi Huang's National Road System(最小生成树+bfs)
题目链接: Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU4081 Qin Shi Huang's National Road System(次小生成树)
枚举作为magic road的边,然后求出A/B. A/B得在大概O(1)的时间复杂度求出,关键是B,B是包含magic road的最小生成树. 这么求得: 先在原图求MST,边总和记为s,顺便求出M ...
- hdu4081 Qin Shi Huang's National Road System 次小生成树
先发发牢骚:图论500题上说这题是最小生成树+DFS,网上搜题解也有人这么做.但是其实就是次小生成树.次小生成树完全当模版题.其中有一个小细节没注意,导致我几个小时一直在找错.有了模版要会用模版,然后 ...
- HDU4081 Qin Shi Huang's National Road System
先求最小生成树 再遍历每一对顶点,如果该顶点之间的边属于最小生成树,则剪掉这对顶点在最小生成树里的最长路径 否则直接剪掉连接这对顶点的边~ 用prim算法求最小生成树最长路径的模板~ #include ...
- 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 ...
- UValive 5713 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 ...
随机推荐
- C if 判断 else 否则
#include <stdio.h> int main(int argc, char **argv) { //新建三个变量进行比较 int a,b,c; //输入三个变量的值scanf(& ...
- 关闭Tomcat进程 一条语句(必看)
写在开始 MAC系统下进行JAVA研发,经常遇到的一个问题就是杀死异常Tomcat 通常都是用两条指令,先查询出Tomcat占用的进程,再kill掉该进程, 其实有一种联合语句的方式可以一条语句直接关 ...
- instanceof 运算符简介
文章摘自: http://www.ibm.com/developerworks/cn/web/1306_jiangjj_jsinstanceof/ https://developer.mozilla. ...
- mysql source 恢复 sql数据time_zone报错 已解决
报了一些变量的错误,类似于"time_zone" 等错误 解决: [root@iz8vbilqy0q9v8tds55bqzz conf.d]# vi /etc/my.cnf [my ...
- Pipeline组项目Postmortem
Pipeline组项目Postmortem 1. 设想和目标 1)目标我们的软件要解决什么问题?是否定义得很清楚?是否对典型用户和典型场景有清晰的描述? 我们的项目是学霸系统PipeLine, ...
- Daily Scrum 10
今天我们小组开会内容分为以下部分: part 1: 经过反复思考,对于上次组会确定的在系统中加入娱乐版块进行了更进一步的商讨; part 2:继续探讨算法实现: part 3:进行明日的任务分配; ◆ ...
- 11.24Daily Scrum
人员 任务分配完成情况 明天任务分配 王皓南 实现网页上视频浏览的功能.研究相关的代码和功能.990 测试 申开亮 实现网页上视频浏览的功能.研究相关的代码和功能.991 测试 王宇杰 负责后台代码测 ...
- Coursera-Note: Internet History, Technology and Secure (1st week to 9th week)
目录 Coursera-Note: Internet History, Technology and Secure 第一周 第二周 数据交换: Packet switching技术: 第三周 创造ht ...
- keydown事件下调用trigger事件执行两次
$('button[type=button]').on('click',login); //登录 $(document).keydown(function(event){ if(event.keyCo ...
- PCA算法理解及代码实现
github:PCA代码实现.PCA应用 本文算法均使用python3实现 1. 数据降维 在实际生产生活中,我们所获得的数据集在特征上往往具有很高的维度,对高维度的数据进行处理时消耗的时间很大, ...