Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3750 Accepted Submission(s): 1305
Problem 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.

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.
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
Source
Recommend
lcy
题意:
给出 T 组样例,每个样例都有 N 个点,后给出 N 个点的坐标和值。要求从中选出 n - 1 条路,选中其中一条为魔法路,这时候的 A = 魔法路所连接两个点的总和值, B = n - 1 条路的总和长度值 - 魔法路的长度值。求 A / B 最大。
思路:
最小生成树 + LCA。因为要求 A / B 最大,相当于求 B 最小,即求最小生成树。求完之后并没有结束,因为此刻还未能确定答案。之后开始枚举任意两个点,使这两个点连边(相当于在枚举魔法边),如果这两个点连接的边并不是生成树上的边,添加之后则必定会生成环,这时候要删除这个环上权值最大的那条边以保证 B 最小(B = min_tree - max_val);如果已经是生成树上的边,则直接删除这条边即可(B = min_tree - G [ i ] [ j ] )。找环则通过 LCA 寻找路径来找,边往上边比较最大值即可。注意数据类型!!!!因为保存边的时候一不小心用了 int 所以导致无限 WA。
AC:
#include <cstdio> #include <cstring> #include <algorithm> #include <cmath> using namespace std; const int VMAX = 1010; const int EMAX = 1000010; typedef struct { int root; double len; } fath; typedef struct { double x, y, num; } node; typedef struct { double len; int a, b; } Map; int G_ind, n; double Min_sum; node no[VMAX]; Map G[EMAX]; double G1[VMAX][VMAX]; int root[VMAX]; int ind; int fir[VMAX], next[EMAX], v[EMAX]; double w[EMAX]; int cnt; bool vis[VMAX]; int vs[VMAX * 2], dep[VMAX * 2], id[VMAX]; fath fa[VMAX]; int dp[VMAX * 2][25]; bool cmp (Map a, Map b) { return a.len < b.len; } void init () { G_ind = ind = Min_sum = cnt = 0; for (int i = 1; i <= n; ++i) { fa[i].root = root[i] = i; } memset(fir, -1, sizeof(fir)); memset(G1, 0, sizeof(G1)); memset(vis, 0, sizeof(vis)); } void add_edge (int f, int t, double val) { v[ind] = t; w[ind] = val; next[ind] = fir[f]; fir[f] = ind; ++ind; } int Find (int x) { return root[x] == x ? x : root[x] = Find(root[x]); } void Min_tree () { sort(G, G + G_ind, cmp); int ans = 0; for (int i = 0; i < G_ind; ++i) { if (ans == n - 1) break; int a = Find(G[i].a); int b = Find(G[i].b); if (a != b) { ++ans; Min_sum += G[i].len; root[a] = b; add_edge(G[i].a, G[i].b, G[i].len); add_edge(G[i].b, G[i].a, G[i].len); G1[G[i].a][G[i].b] = G[i].len; G1[G[i].b][G[i].a] = G[i].len; } else continue; } } void dfs (int x, int d) { id[x] = cnt; dep[cnt] = d; vs[cnt++] = x; vis[x] = 1; for (int e = fir[x]; e != -1; e = next[e]) { int V = v[e]; if (!vis[V]) { fa[V].root = x; fa[V].len = w[e]; dfs (V, d + 1); vs[cnt] = x; dep[cnt++] = d; } } } void RMQ_init () { for (int i = 0; i < cnt; ++i) dp[i][0] = i; for (int j = 1; (1 << j) <= cnt; ++j) { for (int i = 0; i + (1 << j) < cnt; ++i) { int a = dp[i][j - 1]; int b = dp[i + (1 << (j - 1))][j - 1]; if (dep[a] < dep[b]) dp[i][j] = a; else dp[i][j] = b; } } } int RMQ (int L, int R) { int len = 0; while ((1 << (1 + len)) <= R - L + 1) ++len; int a = dp[L][len]; int b = dp[R - (1 << len) + 1][len]; if (dep[a] < dep[b]) return a; return b; } int LCA (int a, int b) { int L = min(id[a], id[b]); int R = max(id[a], id[b]); int node = RMQ(L, R); return vs[node]; } double Find_max_road (int a, int b) { int lca = LCA(a, b); double Max = 0; while (a != lca) { Max = max(Max, fa[a].len); a = fa[a].root; } while (b != lca) { Max = max(Max, fa[b].len); b = fa[b].root; } return Max; } int main() { int t; scanf("%d", &t); while (t--) { scanf("%d", &n); init(); for (int i = 1; i <= n; ++i) { scanf("%lf%lf%lf", &no[i].x, &no[i].y, &no[i].num); } for (int i = 1; i <= n - 1; ++i) { for (int j = i + 1; j <= n; ++j) { double x = (double)no[i].x - (double)no[j].x; double y = (double)no[i].y - (double)no[j].y; double len = sqrt(x * x + y * y); G[G_ind].a = i; G[G_ind].b = j; G[G_ind++].len = len; } } Min_tree(); dfs (1, 1); RMQ_init(); double ans = 0; for (int i = 1; i <= n - 1; ++i) { for (int j = i + 1; j <= n; ++j) { double Max; if (G1[i][j]) Max = G1[i][j]; else Max = Find_max_road(i , j); double ans1 = Min_sum - Max; ans1 = (no[i].num + no[j].num) / ans1; ans = max(ans, ans1); } } printf("%.2lf\n", ans); } return 0; }
相关推荐
本资源提供了关于 DFS 枚举组合情况的题目,例如 Qin Shi Huang's National Road System。 最大生成树 最大生成树是一种图论概念,指的是连通图的生成树中具有最大权重的树。本资源提供了关于最大生成树的题目,...
python学习资源
jfinal-undertow 用于开发、部署由 jfinal 开发的 web 项目
基于Andorid的音乐播放器项目设计(国外开源)实现源码,主要针对计算机相关专业的正在做毕设的学生和需要项目实战练习的学习者,也可作为课程设计、期末大作业。
python学习资源
python学习资源
python学习一些项目和资源
【毕业设计】java-springboot+vue家具销售平台实现源码(完整前后端+mysql+说明文档+LunW).zip
HTML+CSS+JavaScarip开发的前端网页源代码
python学习资源
【毕业设计】java-springboot-vue健身房信息管理系统源码(完整前后端+mysql+说明文档+LunW).zip
成绩管理系统C/Go。大学生期末小作业,指针实现,C语言版本(ANSI C)和Go语言版本
1_基于大数据的智能菜品个性化推荐与点餐系统的设计与实现.docx
【毕业设计】java-springboot-vue交流互动平台实现源码(完整前后端+mysql+说明文档+LunW).zip
内容概要:本文主要探讨了在高并发情况下如何设计并优化火车票秒杀系统,确保系统的高性能与稳定性。通过对比分析三种库存管理模式(下单减库存、支付减库存、预扣库存),强调了预扣库存结合本地缓存及远程Redis统一库存的优势,同时介绍了如何利用Nginx的加权轮询策略、MQ消息队列异步处理等方式降低系统压力,保障交易完整性和数据一致性,防止超卖现象。 适用人群:具有一定互联网应用开发经验的研发人员和技术管理人员。 使用场景及目标:适用于电商、票务等行业需要处理大量瞬时并发请求的业务场景。其目标在于通过合理的架构规划,实现在高峰期保持平台的稳定运行,保证用户体验的同时最大化销售额。 其他说明:文中提及的技术细节如Epoll I/O多路复用模型以及分布式系统中的容错措施等内容,对于深入理解大规模并发系统的构建有着重要指导意义。
基于 OpenCV 和 PyTorch 的深度车牌识别
【毕业设计-java】springboot-vue教学资料管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip
此数据集包含有关出租车行程的详细信息,包括乘客人数、行程距离、付款类型、车费金额和行程时长。它可用于各种数据分析和机器学习应用程序,例如票价预测和乘车模式分析。
把代码放到Word中,通过开发工具——Visual Basic——插入模块,粘贴在里在,把在硅基流动中申请的API放到VBA代码中。在Word中,选择一个问题,运行这个DeepSeekV3的宏就可以实现在线问答
【毕业设计】java-springboot+vue机动车号牌管理系统实现源码(完整前后端+mysql+说明文档+LunW).zip