原题链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 5428    Accepted Submission(s): 1902

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.
 
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
 
Source
 
Recommend
lcy

题意:

给你平面上若干的点,每个点有个权值$a_i$,现在用这些点构成一棵树。存在一种操作,可以使得这棵树上的某条边的长度变为0,令这条边连接的两个点分别是$i,j$,这棵树的边权和为$B$,问你$(a_i+a_j)/B$的最小值。

题解:

看网上的做法都是求次小生成树,而我却不是这样做的。

首先使用Kruskal求出最小生成树,而后枚举两个点,将这两个点之间连接那条边,这样就构成了一个换,为了保持树的形态,必须删掉这两个点的唯一路径上最长的边。

找到这条边的方法是通过倍增来求解。

令$ancestor[u][i]$为节点$u$向上走$2^i$所能到达的节点,$maxEdge[u][i]$表示节点$u$向上走$2^i$所能碰到的最长的边,那么有以下转移:

$$ancestor[u][i]=ancestor[ancestor[u][i-1]][i-1]$$

$$maxEdge[u][i]=max(maxEdge[u][i-1],maxEdge[ancestor[u][i-1]][i-1])$$

预处理出这两个数组后,就能用类似LCA的思想求最长边了。

代码:

#include<iostream>
#include<cstring>
#include<vector>
#include<cstdio>
#include<algorithm>
#include<iomanip>
#define MAX_N 1234
#define MAX_D 25
using namespace std; int father[MAX_N];
int n; int popu[MAX_N]; struct edge {
int to;
double cost; edge(int t, double c) : to(t), cost(c) { } edge() { }
}; struct road {
int from, to;
double cost; road(int f, int t, double c) : from(f), to(t), cost(c) { } road() { }
}; bool cmp(road a,road b) {
if (a.cost == b.cost)
return popu[a.from] + popu[a.to] > popu[b.from] + popu[b.to];
return a.cost < b.cost;
} road ro[MAX_N*MAX_N];
int tot=;
vector<edge> G[MAX_N];
double B; int depth[MAX_N];
int ancestor[MAX_N][];
double maxEdge[MAX_N][]; struct Point {
double x, y; Point(double xx, double yy) : x(xx), y(yy) { } Point() { } double dis(Point a) {
return sqrt((x - a.x) * (x - a.x) + (y - a.y) * (y - a.y));
}
}; Point po[MAX_N]; void init() {
for (int i = ; i <= n; i++)
father[i] = i;
for (int i = ; i <= n; i++)G[i].clear();
tot = ;
B = ;
memset(depth, , sizeof(depth));
memset(ancestor, , sizeof(ancestor));
memset(maxEdge, , sizeof(maxEdge));
} int Find(int x){
if(x==father[x])return x;
return father[x]=Find(father[x]);
} void unionSet(int x,int y) {
int u = Find(x), v = Find(y);
if (u == v)return;
father[u] = v;
} bool Same(int x,int y){
return Find(x)==Find(y);
} void Kruskal() {
sort(ro + , ro + tot + , cmp);
for (int i = ; i <= tot; i++) {
int u = ro[i].from, v = ro[i].to;
if (Same(u, v))continue;
unionSet(u, v);
G[u].push_back(edge(v,ro[i].cost));
G[v].push_back(edge(u,ro[i].cost));
B += ro[i].cost;
}
} void dfs(int u,int p) {
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].to;
double c = G[u][i].cost;
if (v == p)continue;
depth[v] = depth[u] + ;
ancestor[v][] = u;
maxEdge[v][] = c;
dfs(v, u);
}
} void getAncestor() {
for (int j = ; j < MAX_D; j++) {
for (int i = ; i <= n; i++) {
ancestor[i][j] = ancestor[ancestor[i][j - ]][j - ];
maxEdge[i][j] = max(maxEdge[i][j - ], maxEdge[ancestor[i][j - ]][j - ]);
}
}
} double LCA(int u,int v) {
double res = -;
if (depth[u] < depth[v])swap(u, v);
for (int i = MAX_D - ; i >= ; i--) {
if (depth[ancestor[u][i]] >= depth[v]) {
res = max(res, maxEdge[u][i]);
u = ancestor[u][i];
if (depth[u] == depth[v])break;
}
}
if (u == v)return res;
for (int i = MAX_D - ; i >= ; i--) {
if (ancestor[u][i] != ancestor[v][i]) {
res = max(res, max(maxEdge[u][i], maxEdge[v][i]));
u = ancestor[u][i];
v = ancestor[v][i];
}
}
return max(res, max(maxEdge[u][], maxEdge[v][]));
} int T; int main() {
cin.sync_with_stdio(false);
cin >> T;
while (T--) {
cin >> n;
init();
for (int i = ; i <= n; i++)
cin >> po[i].x >> po[i].y >> popu[i];
for (int i = ; i <= n; i++)
for (int j = i + ; j <= n; j++)
ro[++tot] = road(i, j, po[i].dis(po[j]));
Kruskal();
depth[] = ;
dfs(, );
getAncestor();
double ans = -;
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
ans = max(ans, (popu[i] + popu[j]) / (B - LCA(i, j)));
cout << setprecision() << fixed << ans << endl;
}
return ;
}

PS:第一次写手写倍增,居然一发就AC了

HDU 4081 Qin Shi Huang's National Road System 最小生成树+倍增求LCA的更多相关文章

  1. HDU 4081 Qin Shi Huang's National Road System 最小生成树

    分析:http://www.cnblogs.com/wally/archive/2013/02/04/2892194.html 这个题就是多一个限制,就是求包含每条边的最小生成树,这个求出原始最小生成 ...

  2. 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 ...

  3. 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 ...

  4. 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 ...

  5. HDU 4081—— Qin Shi Huang's National Road System——————【次小生成树、prim】

    Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/3 ...

  6. hdu 4081 Qin Shi Huang's National Road System 树的基本性质 or 次小生成树思想 难度:1

    During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in Ch ...

  7. HDU - 4081 Qin Shi Huang's National Road System 【次小生成树】

    题目链接 http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意 给出n个城市的坐标 以及 每个城市里面有多少人 秦始皇想造路 让每个城市都连通 (直接或者 ...

  8. hdu 4081 Qin Shi Huang's National Road System(次小生成树prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4081 题意:有n个城市,秦始皇要修用n-1条路把它们连起来,要求从任一点出发,都可以到达其它的任意点. ...

  9. HDU 4081 Qin Shi Huang's National Road System [次小生成树]

    题意: 秦始皇要建路,一共有n个城市,建n-1条路连接. 给了n个城市的坐标和每个城市的人数. 然后建n-2条正常路和n-1条魔法路,最后求A/B的最大值. A代表所建的魔法路的连接的城市的市民的人数 ...

随机推荐

  1. Python基础(五)——闭包与lambda的结合

    (1)变量的域 要了解闭包需要先了解变量的域,也就是变量在哪一段“上下文”是有效的(类似局部变量和全局变量的区别),举一个很简单的例子.(例子不重要,就是涉及闭包就要时刻关注这个域) def test ...

  2. stm32启动地址

    理论上,CM3中规定上电后CPU是从0地址开始执行,但是这里中断向量表却被烧写在0x0800 0000地址里(Flash memory启动方式),那启动时不就找不到中断向量表了?既然CM3定下的规矩是 ...

  3. JS中如何操作数组

    背景:随笔中所应用到的代码来自于上一篇随笔,MVC&JQuery如何根据List动态生成表格,部分代码不再重复. 代码如下: $("#btnTan").click(func ...

  4. Spring核心技术(十三)——环境的抽象

    本章将描述一下Spring中针对环境的抽象. Environment是一个集成到容器之中的特殊抽象,它针对应用的环境建立了两个关键的概念:profile和properties. profile是命名好 ...

  5. UVa 10934 DP Dropping water balloons

    首先想一下特殊情况,如果只有一个气球,我们要确定高度只能从下往上一层一层地测试,因为如果气球一旦爆了,便无法测出气球的硬度. 如果气球有无数个,那么就可以用二分的方法来确定. 一般地,用d(i, j) ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  7. java EE技术体系——CLF平台API开发注意事项(1)——后端开发

    前言:这是一篇帮助小伙伴在本次项目中快速进入到java EE开发的一些说明,为了让同组小伙伴们开发的时候,有个清晰点的思路.昨天给大家演示分享了基本概况,但没有留下文字总结说明,预防后期有人再次问我, ...

  8. 如何使用Python的logging模块

    几个学习连接: Python官方链接: https://docs.python.org/3.4/library/logging.html?highlight=logging 翻译(不过是2.3版本的) ...

  9. cf- 297 < a >--字符串操作技巧

    A. Vitaliy and Pie time limit per test 2 seconds memory limit per test 256 megabytes input standard ...

  10. NOJ——1656搬砖(DP)

    [1656] 搬砖 时间限制: 2000 ms 内存限制: 65535 K 问题描述 开学了,万恶的大二学长们又要领着大一的鲜肉们一起敲代码搬砖了,这不,著名的杨神拿着n块砖头,当然他把这n块砖头的重 ...