【题解】Qin Shi Huang's National Road System HDU - 4081 ⭐⭐⭐⭐ 【次小生成树】
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个点的坐标以及每个点的人口, 要求将这N个点通过N-1条边连接起来, 权值为两点直接距离, B为距离和, 同时可以选中一条边, 使得该边权值变为0, A为该边两点人口数量. 求A/B的最大值
题解:
既然要求A/B的最大值, 就一定要A最大, B最小, 所以B需要求一下MST, A的话我们直接枚举每条边就好了, 如果该边已经在MST中,$$ ans = max(ans, A/(B-w[i][j]));$$ 否则$$ans = max(ans, A/(B-maxD[i][j]));$$
可以看到, 虽然这里没有直接使用次小生成树, 但是完全用到了次小生成树求出的几个数组, 这也是前面为何没有直接求出次小生成树的原因, 因为大部分的题目必然不是裸题, 所以更要仔细理解次小生成树的思想
#include<bits/stdc++.h>
using namespace std;
#define ms(x, n) memset(x,n,sizeof(x));
typedef long long LL;
const int inf = 1 << 30;
const LL maxn = 1010;
int N;
double w[maxn][maxn];
struct node {
int x, y;
int p;
node(int xx, int yy, int pp) { x = xx, y = yy, p = pp; }
node() {}
} vs[maxn];
double getDis(int x1, int y1, int x2, int y2) {
return sqrt((double)(x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
}
double d[maxn];
bool used[maxn];
double maxD[maxn][maxn]; //MST中从i->j的最大权值
int pre[maxn]; //某一点父节点
bool mst[maxn][maxn]; //该点是否已经在MST中
typedef pair<int, int> P;
double Prim(int s) {
fill(d, d + maxn, inf);
fill(pre, pre + maxn, s);
ms(maxD, 0); ms(used, 0); ms(mst, 0);
priority_queue<P, vector<P>, greater<P> > q;
q.push(P(d[s] = 0, s));
double res = 0;
while (!q.empty()) {
P cur = q.top();
q.pop();
int u = cur.second;
if (used[u])
continue;
used[u] = true, res += d[u];
mst[u][pre[u]] = mst[pre[u]][u] = true; //加入到MST中
for (int v = 1; v <= N; ++v) {
if (used[v] && w[u][v] < inf) //只更新MST中的
maxD[u][v] = maxD[v][u] = max(maxD[pre[u]][v], d[u]);
if (w[u][v] < d[v]) {
d[v] = w[u][v];
pre[v] = u; //更新父节点
q.push(P(d[v], v));
}
}
}
return res;
}
int main() {
//freopen("in.txt", "r", stdin);
int T, a, b, c;
scanf("%d", &T);
while (T--) {
ms(vs, 0); fill(w[0], w[0] + maxn * maxn, inf);
scanf("%d", &N);
for (int i = 1; i <= N; ++i) {
scanf("%d%d%d", &a, &b, &c);
vs[i] = node(a, b, c);
}
for (int i = 1; i < N; ++i)
for (int j = i + 1; j <= N; ++j)
w[i][j] = w[j][i] = getDis(vs[i].x, vs[i].y, vs[j].x, vs[j].y);
//枚举删边, 找出最大值
double B = Prim(1), A, ans = -1;
for (int i = 1; i < N; ++i)
for (int j = i + 1; j <= N; ++j) {
A = vs[i].p + vs[j].p;
//这条边未在MST中使用, 尝试加边并删去生成环中的最长边, 已使用则直接变0
if (mst[i][j]) {
ans = max(ans, A / (B - w[i][j]));
}
else {
ans = max(ans, A / (B - maxD[i][j]));
}
}
printf("%.2lf\n", ans);
}
return 0;
}
【题解】Qin Shi Huang's National Road System HDU - 4081 ⭐⭐⭐⭐ 【次小生成树】的更多相关文章
- Qin Shi Huang's National Road System HDU - 4081(树形dp+最小生成树)
Qin Shi Huang's National Road System HDU - 4081 感觉这道题和hdu4756很像... 求最小生成树里面删去一边E1 再加一边E2 求该边两顶点权值和除以 ...
- LA 5713 - Qin Shi Huang's National Road System(HDU 4081) MST
LA:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...
- 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/3 ...
- 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: ...
- 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 ...
- 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 ...
- 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 P4081] Qin Shi Huang’s National Road System
[hdu P4081] Qin Shi Huang’s National Road System Time Limit: 2000/1000 MS (Java/Others) Memory Li ...
随机推荐
- MongoDB 6.0 单实例基于用户角色实现授权登录
现代数据库系统能够存储和处理大量数据.因此,由任何一个用户单独负责处理与管理数据库相关的所有活动的情况相对较少.通常,不同的数据库用户需要对数据库的某些部分具有不同级别的访问权限:某些用户可能只需要读 ...
- springboot下添加全局异常处理和自定义异常处理
前言 在spring项目中,优雅处理异常,好处是可以将系统产生的全部异常统一捕获处理,自定义的异常也由全局异常来捕获,如果涉及到validator参数校验器使用全局异常捕获也是较为方便. 相关代码: ...
- K8s 里如何优雅地使用 /dev/shm 实现容器间共享内存
目录 1. 从 docker run 的 --shm-size 参数聊起 2. Linux 里的 /dev/shm 3. Docker 对共享内存的支持 4. K8s 里如何设置 /dev/shm 大 ...
- 实践解析HPA各关联组件扭转关系
本文分享自华为云社区<HPA各关联组件扭转关系以及建议>,作者:可以交个朋友. 一.背景 应用程序的使用存在波峰波谷现象,在应用流量处于低谷期间,可以释放因过多的Pod而浪费的硬件资源.在 ...
- 5分钟攻略Spring-Retry框架实现经典重试场景
前言 今天分享干货,控制了篇幅,5分钟内就能看完学会. 主题是Spring-Retry框架的应用,做了一个很清晰的案例,代码可下载自测. 框架介绍 Spring-Retry框架是Spring自带的功能 ...
- 开发期间flask运行方式(1.x和2.x区别)
1.x使用app.run(参数) 可以使用debug=True控制处于什么模式. app.run(host="127.0.0.1", port=5000, debug=True) ...
- 2023年最后一个工作日,当 hr总监找上我协商赔偿
今天是2023年最后一个工作日,hr 总监找上我协商赔偿一事,忆往昔三年前,公司刚融资1个亿,意气风发,博主入职即为公司巅峰,高级开发岗,14薪,各种福利,加班另算加班费,业务主要服务于众多500强集 ...
- ElasticSearch之Merge
Elasticsearch的shard,即对应Lucene的index. Lucene的index由多个segment组成. segment是index保存数据的最小单位,不支持修改. Elastic ...
- 文心一言 VS 讯飞星火 VS chatgpt (171)-- 算法导论13.2 4题
四.用go语言,证明:任何一棵含n个结点的二叉搜索树可以通过 O(n)次旋转,转变为其他任何一棵含n个结点的二叉搜索树.(提示:先证明至多n-1次右旋足以将树转变为一条右侧伸展的链.) 文心一言: 这 ...
- ESXi6.7物理机安装之网卡驱动封装Realtek PCIe GBE Family Controller =瑞昱r8168网卡驱动
https://blog.whsir.com/post-3423.html "我这里先提供一个ESXI6.5封装好的r8168网卡驱动ESXI6.5u2.iso,如果你的网卡也是这个,可以直 ...