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 ...
随机推荐
- lintcode 二叉树前序遍历
二叉树的前序遍历 给出一棵二叉树,返回其节点值的前序遍历. 您在真实的面试中是否遇到过这个题? Yes 样例 给出一棵二叉树 {1,#,2,3}, 1 \ 2 / 3 返回 [1,2,3]. / ...
- 更新字典 (Updating a Dictionary,UVa12504)
题目描述: 解题思路: 1.根据:和,获得字符串 2.使用两个map进行比较: #include <iostream> #include <algorithm> #includ ...
- 剑指offer-包含min函数的栈20
题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). class Solution: def __init__(self): self.st ...
- Git 命令详解及常用命令
Git 命令详解及常用命令 Git作为常用的版本控制工具,多了解一些命令,将能省去很多时间,下面这张图是比较好的一张,贴出了看一下: 关于git,首先需要了解几个名词,如下: 1 2 3 4 Work ...
- VUE中关于表单提交的简单实现
main.js import Vue from "../vue.js"; import App from "./App.js"; //启动 new Vue({ ...
- TCP系列25—重传—15、DSACK虚假重传探测
一.DSACK介绍 RFC2883通过指定使用SACK来指示接收端的重复包(duplicate packet)扩展了RFC2018对SACK选项的定义(SACK选项的介绍和示例参考前面内容).RFC2 ...
- 【转】how can i build fast
http://blog.csdn.net/pcliuguangtao/article/details/5830860
- isset、is_null、empty的区别
版本:PHP 5.4 1.isset() :检测变量是否存在,测试如下: $a = false; $b = null; $c; $d = 0; $e = true; var_dump(isset($a ...
- 显示系统中所有的socket信息
netstat -aon /proc/net/tcp /proc/net/udp /proc/net/unix 相关的代码是:tcp4_seq_show(struct seq_file *file, ...
- java 中使用Base64
byte[] cipherData = Base64.encodeBase64(plainText.getBytes()); //默认不换行 byte[] cipherData = Base64.en ...