HDU 4081 Qin Shi Huang ' s National Road System (sub-niche tree algorithm)

Source: Internet
Author: User

Link:

http://acm.hdu.edu.cn/showproblem.php?pid=4081

Topic:

Problem Description

During the Warring States Period of ancient, 476 BC to 221 BC, there-were seven kingdoms in----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 "a" emperor 1 BC. That is Qin dynasty----the "the" Imperial Dynasty of "the" is confused with the Qing dynasty, "The Last dynasty O f). So Ying Zheng named himself "Qin Shi Huang" because "Shi Huang" means "the" the "the" the "" The "" "Emperor"

Qin Shi Huang undertook gigantic projects, including the The great Wall of "the", "now famous City-siz Ed 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 and Qin Shi Huang wanted of all to them by connected Roads, in-order, "he could go" to every city Xianyang. Although Qin Shi Huang was a tyrant, who wanted the total length of all roads to is minimum,so that the road system may Cost too many people ' s life. A daoshi (some kind of monk) named Xu Fu told Qin Shi Huang that he could builds a road by magic and that Magic Road Cost no money and no labor. But Xu Fu could only builds one magic road for Qin Shi. 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 being the maximum, which a is the total Populat Ion of the two cites connected by the Magic Road, and B are the total length of none magic roads. Would you Qin Shi Huang? A city can is considered as a point, and a road can is considered as a line segment connecting two points.

Input

The ' The ' contains an integer t meaning that there are t test Cases (T <= 10). For each test case:the the-i-a 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 the "City of" and "P" is the population of. It is guaranteed the has a distinct location.

Output

For each test case, print a line indicating the above mentioned maximum ratio A/b. The result should is rounded to 2 digits on 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

Asia Beijing Regional Contest

The main effect of the topic:

There are n cities, Qin Shihuang to repair with N-1 Road to connect them up, ask from any point, can reach any other point. Qin Shihuang hope that all of these n-1 the length of the shortest path. Then Xufu suddenly came out, saying that he has magic, can not be human, financial resources to turn out any way out.

Qin Shihuang hope Xu Fu can make the N-1 road to repair the longest one, but Xu Xufu hope to be able to the number of manpower required to change out. The manpower required for each road refers to the sum of the number of the two cities connected by this route.

This column more highlights: http://www.bianceng.cn/Programming/sjjg/

In the end, Qin Shihuang gave a formula, a/b,a refers to Xu Xufu use magic to change the road required manpower, B refers to the other than Xu Xufu to change out of all the n-2 path length of the sum, select Make A/b value the largest one.

Analysis and summary

In order to make the largest A/b value, the first is the need to be B as small as possible, so you can first find the N-City minimum spanning tree. Then, it is decided to choose that one with Xu Fu's magic to change.

Therefore, you can enumerate each edge, assuming that the value of the minimum spanning tree is minmst, and that the edge length of the enumeration is w[i][j], and if the edge is already on the smallest spanning tree, then the value of the final expression is A/(Minmst-w[i][j]). If this one does not belong to the smallest spanning tree, then add this edge, there will be N edge, then it will make a ring, in order to make it a spanning tree, it is necessary to delete the ring on a tree. To make the spanning tree as small as possible, delete the path that has the largest weight except the one that joins. Suppose the value of the deleted edge is path[i][j], then it is a A/(Minmst-path[i][j]).

The key to solving this problem lies in how to find the second niche into a tree.

The following excerpt from online data:

[Second niche into tree]

Analogy to the previous short path method, it is easy to think of a "enumeration to delete the minimum spanning tree each edge, and then the minimum spanning tree" intuitive solution. If the prim+ heap is used, each minimum spanning tree time complexity is O (N*log (n+m) + M), the enumeration deletes an O (N) edge, and the time complexity is O (N^2*log (n+m) + n*m), near O (n^3) when the figure is dense. This method is simple and intuitive, but we have a simpler, more efficient, O (n^2+m) solution, which is described below.

First, the minimum spanning tree is obtained, and the sum of the recording weights is Minst. Enumerations Add each edge (U,V) that is not on the minimum spanning tree, plus a loop is bound to form later. To find the second-largest edge of the value on the ring (that is, except for the u,v), delete it and compute the sum of the weights of the current spanning tree. The minimum value of the sum of the spanning tree weights that are modified by all enumerations is the second niche tree.

When implemented, the simpler approach is to traverse the entire minimum spanning tree from each node I, defining f[j] as the maximum edge of the path from I to J. Traversal diagram to find out the value of f[j], and then for adding each edge (I,J) that is not in the minimum spanning tree, the sum of the new spanning tree weights is Minst + w (i,j) –f[j], and the minimum value is recorded, then the second niche becomes a tree.

The time complexity of the algorithm is O (n^2 + M). Since only one minimum spanning tree is used, the simplest prim can be used, and the time complexity is O (n^2). The bottleneck of the algorithm is not to find the minimum spanning tree, but to modify the enumeration of O (n^2+m), so it is not necessary to use a better minimum spanning tree algorithm.

Algorithm to solve the secondary niche tree:

Convention: A set of new spanning trees made by T for a feasible exchange, called the neighborhood set of a tree T, which is recorded as N (t).

Theorem 3: Set T to be the smallest spanning tree of graph G if T1 satisfies Ω (T1) =min{ω (T ') | T ' ∈n (t)}, then T1 is g

The second niche into a tree.

Proof: If T1 is not a secondary niche tree of G, then there must be another spanning tree t ', t ' =t that makes

Ω (t) ≤ω (t ') <ω (T1), defined by the T1 t that does not belong to N (t), then

E (t ') \e (t) ={a1,a2

1,......, at},e (t) \e (t ') ={b1,b2,......, bt}, where t≥2. According to the Lemma 1 know that there is a

The permutation bi1,bi2,......, bit so that the t+aj-bij is still the spanning tree of G and belongs to N (T), so Ω (AJ) ≥ω (bij),

So Ω (T ') ≥ω (t+aj-bij) ≥ω (T1), so contradictory. So T1 is the second niche tree of Fig G.

Through the above theorem, we have the basic idea of solving the problem of the secondary niche tree.

First, the minimum spanning tree T of the graph is obtained. Time complexity O (vlog2v+e)

Then, we find the right value and the smallest spanning tree of T, that is, the secondary niche of fig G is tree.

If it's just a simple enumeration, it's a very high degree of complexity. First, the complexity of enumerating two edges is O (VE), and then determine whether the exchange

The feasible degree of complexity is O (V), then the total time complexity is O (v2e). Such algorithms appear to be blind. After a simple

Analysis is not difficult to find, each add a not on the edge of the tree, can always form a ring, only to delete the ring on a side, to

Guaranteed after the exchange is still a spanning tree, and the deletion of the edge of the larger weight, the new generated tree weight and the smaller. We can

In order to reduce the complexity of the O (VE). It's a big step forward, but it's still not good enough.

Looking back at the previous model-the minimum limit spanning tree-we have faced similar problems and eventually adopted dynamic rules

The method of the stroke avoids the repetition calculation, which greatly reduces the complexity. For the subject, we can adopt a similar idea. First

First, do a one-step preprocessing to find the maximum weight on the path between each of the two nodes in the tree, and then the enumeration diagram is not

The edge of the tree, with just the pretreatment, we can use O (1) time to get the maximum edge of the right value on the ring formed.

How to preprocess it? Because this is a tree, so do not need any advanced algorithm, as long as the simple BFS can.

The time complexity of preprocessing is O (V2).

In this way, the time complexity of this step is reduced to O (V2).
To sum up, the time complexity of the second niche into a tree is O (V2).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.