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 ...
随机推荐
- window上小而美的软件(推荐度按排名)
window上小而美的软件,推荐度按排名 Notepad++ 更好用更强大的笔记本 QTranslate 本地翻译神器 7-zip 解压缩软件 Wox 程序/文件/快捷 神器 1! Everthing ...
- springMVC使用拦截器检查用户登录
参考文章 编写拦截器类 package cultivate_web.interceptor; import javax.servlet.http.HttpServletRequest; import ...
- Java并发简介
年轻的时候学会了“使用”Servlet后,感觉自己什么都会做了,之后就不停的写所谓的业务逻辑,框架(这里说的不是structs,spring等,就是说servlet)给人们屏蔽了很多复杂性(更别说构建 ...
- 欢迎来怼--第七次Scrum会议
一.小组信息 队名:欢迎来怼 小组成员: 队长:田继平 成员:李圆圆,葛美义,王伟东,姜珊,邵朔,冉华 小组照片 二.开会信息 时间:2017/10/19 17:05~17:17,总计12min. 地 ...
- 20145214 《Java程序设计》第8周学习总结
20145214 <Java程序设计>第8周学习总结 教材学习内容总结 日志API 使用日志的起点是Logger类,Logger类的构造函数标示为protected,不是java.util ...
- c# throw抛出上一个异常
catch(exception e) { throw; } 不仅抛出这次的异常,也抛出之前的异常. 用法示例:函数A调用函数B,A用到此throw时,B中发生的异常也会继承过来. catch(exce ...
- PagedDataSource数据绑定控件和AspNetPager分页控件结合使用列表分页
1.引用AspNetPager.dll. 2.放置Repeater数据绑定控件. <asp:Repeater ID="Repeater1" runat="serve ...
- 结对作业二——WordCount进阶版
软工作业三 要求地址 作业要求地址 结对码云项目地址 结对伙伴:秦玉 博客地址 PSP表格 PSP2.1 个人开发流程 预估耗费时间(分钟) 实际耗费时间(分钟) Planning 计划 10 7 · ...
- The New Day
于博毅 160809107 爱好电脑研究 选大学专业的时候,把计算机类放在了第一专业,当时从小就很喜欢计算机,以前有接触过编程但仅限于看书,并没有动手实践过,选课的时候看了一下专业课程,都是我想学的 ...
- node中的path.resolve
path.resolve([arg1,arg2,...])根据参数的不同,返回值存在两种情况. 以下为参数的两种情况: 1.每个参数都不带'/',比如path.resolve(),或者path.res ...