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
|
|
Sample Output
|
|
Solution
題目描述:
有 n 個城市,在中國秦始皇希望他們都用連接 n - 1 道路,以便他可以去到每一個城市從首都咸陽。儘管秦始皇是個暴君,他希望所有的道路是最低的總長度,從而使道路系統可能不會花費太多勞力。倒是一個(一些和尚)命名徐福告訴秦始皇,他可以通過魔法修路和魔法的道路將花費沒用錢,沒用勞動力。但徐福只能建一個神奇的道路秦始皇。所以,秦始皇不得不決定在哪裡建造魔法的道路。秦始皇想所有無魔法的道路是盡可能小的總長度,但徐福希望魔法道路可以效益為盡可能多的人。所以秦始皇決定 A / B 必須是最大的,其中 A 是魔法路兩端的總人口,而 B 是沒有魔法的道路的總長度。
題目解法:
找出 MST,窮舉所有所有魔法路的可能。
對於一個 MST 修改一條邊,可在 O(n)
時間內找到環,之後將環上最重的邊去除即可。
但是這題中,MST 會保留住,因此對 MST 任兩點路徑上的最重邊進行在 O(n^2)
內完成計算。
- MST 加入新點,可以在 O(n) 時間內完成 ?
|
|