UVa 1494 - Qin Shi Huang's National Road System

contents

  1. 1. Problem
  2. 2. Input
  3. 3. Output
  4. 4. Sample Input
  5. 5. Sample Output
  6. 6. Solution

Problem

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.

\epsfbox{p5713.eps}

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

1
2
3
4
5
6
7
8
9
10
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

1
2
65.00
70.00

Solution

題目描述:

有 n 個城市,在中國秦始皇希望他們都用連接 n - 1 道路,以便他可以去到每一個城市從首都咸陽。儘管秦始皇是個暴君,他希望所有的道路是最低的總長度,從而使道路系統可能不會花費太多勞力。倒是一個(一些和尚)命名徐福告訴秦始皇,他可以通過魔法修路和魔法的道路將花費沒用錢,沒用勞動力。但徐福只能建一個神奇的道路秦始皇。所以,秦始皇不得不決定在哪裡建造魔法的道路。秦始皇想所有無魔法的道路是盡可能小的總長度,但徐福希望魔法道路可以效益為盡可能多的人。所以秦始皇決定 A / B 必須是最大的,其中 A 是魔法路兩端的總人口,而 B 是沒有魔法的道路的總長度。

題目解法:

找出 MST,窮舉所有所有魔法路的可能。

對於一個 MST 修改一條邊,可在 O(n) 時間內找到環,之後將環上最重的邊去除即可。
但是這題中,MST 會保留住,因此對 MST 任兩點路徑上的最重邊進行在 O(n^2) 內完成計算。

  • MST 加入新點,可以在 O(n) 時間內完成 ?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
#include <stdio.h>
#include <stdlib.h>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;
struct E {
int x, y, v;
E(int a=0, int b=0, int c=0):
x(a), y(b), v(c) {}
bool operator<(const E &a) const {
return v < a.v;
}
};
E D[1000005];
vector<E> tree[1005];
int p[1005], rank[1005];
int findp(int x) {
return p[x] == x ? x : (p[x] = findp(p[x]));
}
int joint(int x, int y) {
x = findp(x), y = findp(y);
if(x == y)
return 0;
if(rank[x] > rank[y])
rank[x] += rank[y], p[y] = x;
else
rank[y] += rank[x], p[x] = y;
return 1;
}
double kruscal(int n, int m) {
double sum = 0;
sort(D, D+m);
for(int i = 0; i < n; i++) {
p[i] = i, rank[i] = 1;
tree[i].clear();
}
for(int i = 0; i < m; i++) {
if(joint(D[i].x, D[i].y)) {
sum += sqrt(D[i].v);
tree[D[i].x].push_back(E(D[i].x, D[i].y, D[i].v));
tree[D[i].y].push_back(E(D[i].y, D[i].x, D[i].v));
}
}
return sum;
}
int visited[1005];
double maxEdge[1005][1005];
void dfs(int u, int n) {
visited[u] = 1;
for(int i = 0; i < tree[u].size(); i++) {
int v = tree[u][i].y;
if(visited[v]) continue;
double cost = sqrt(tree[u][i].v);
maxEdge[u][v] = maxEdge[v][u] = cost;
for(int j = 0; j < n; j++) {
if(visited[j]) {
maxEdge[j][v] = maxEdge[v][j] = max(maxEdge[j][u], cost);
}
}
dfs(v, n);
}
}
int main() {
int testcase;
scanf("%d", &testcase);
while(testcase--) {
int n, e;
int X[1005], Y[1005], P[1005];
scanf("%d", &n);
for(int i = 0; i < n; i++)
scanf("%d %d %d", &X[i], &Y[i], &P[i]);
e = 0;
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
D[e++] = E(i, j, (X[i] - X[j])*(X[i] - X[j])
+ (Y[i] - Y[j])*(Y[i] - Y[j]));
}
}
double mstcost = kruscal(n, e);
double ret = 0;
for(int i = 0; i < n; i++)
visited[i] = 0;
dfs(0, n);
for(int i = 0; i < n; i++) {
for(int j = i + 1; j < n; j++) {
ret = max(ret, (P[i] + P[j])/(mstcost - maxEdge[i][j]));
}
}
printf("%.2lf\n", ret);
}
return 0;
}