Minimum generation tree count, small generation tree, hdu4081

Source: Internet
Author: User
A-Qin Shi Huang's national road system Time limit:1000 ms Memory limit:32768kb 64bit Io format:% I64d & % i64usubmit status

Description

During the Warring States Period of your ent 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 uniied 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 cocould 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 people's life. A daoshi (some kind of Monk) named Xu Fu told Qin Shi Huang that he cocould build a road by magic and that magic road wocould cost no money and no labor. but Xu Fu cocould 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 your 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.
Wocould 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 shocould be rounded to 2 digits after decimal point.

Sample Input

241 1 201 2 30200 2 80200 1 10031 1 201 2 302 2 40

Sample output

65.0070.00 application time: 2 H, if you see this question for the second time, you should use 45min-1h30 min actual time: 1days19h lessons learned: Do not be scared on the surface .. in fact, it is a simple idea: we can see that we think of the Minimum Spanning Tree, so we first assume that T is the minimum spanning tree, and the weight is allt, here there is a minimum spanning tree that needs to be well-known, that is, for any d [I] [J], it is the IJ distance given by the question, there are two cases: 1 d [I] [J] 2 I in the Minimum Spanning Tree, J is the edge of the Minimum Spanning Tree (I, X1), (x1, x2 )... (XK, j) composition, then, the edge of each intermediate path has d [XK] [XK + 1] <D [I] [J], or D [I] [J] is in it. Now let's assume that I is used, j edge, then it is equivalent to adding an edge. d [I] [J] = when d [I] [J] is in the Minimum Spanning Tree, it is equivalent to directly updating the Minimum Spanning Tree. allt-d [I] [J] is enough, the Minimum Spanning Tree for all intermediate paths d [I] [J] 2 when d [I] [J ] When it is not in the Minimum Spanning Tree, the addition of d [I] [J] = 0 forms a ring. (In fact, the first case is a ring.) at this time, an edge is connected, so we thought of taking out the biggest edge. It must be a tree. The points in the tree not rooted in I do not need to be changed. For the subtree rooted in I, the edge that is not inside is still not inside. Otherwise, the weight of the subtree is increasing, so this ring must be deleted to form the smallest subtree. How can we find the maximum edge required for the path from I to J on the smallest spanning tree? Use a DP to perform the DP [I] [J] = max (DP [I] [I precursor node], DP [I] [J]). // assume that I add the smallest Spanning Tree first and there are various poses: This is like a small Spanning Tree, and there are enumeration sides, if you delete this edge, take the two cities with the largest population in the trees on both sides, or kruscal () directly seek the maximum edge, but you don't want to write it.
# Include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <queue> using namespace STD; const int maxn = 1105; int N; struct P {int X, Y, P;} V [maxn]; // Record City double d [maxn] [maxn]; // The given distance between cities bool vis [maxn]; // record the prim intermediate process double maxd [maxn] [maxn]; // used to record the maximum edge typedef pair <int, int> point to be passed through from I to J on the tree; // record a edge typedef pair from second to first <double, point> PR; // The priority_queue starting from second <PR, vector <PR >, Greater <Pr> que; // used to obtain the smallest weight edge of prim double prim () {memset (VIS, 0, sizeof (VIS )); vis [0] = true; // the start point is 0 and INT num = 1 is already in the tree; // The number of nodes in the current minimum spanning tree while (! Que. empty () que. pop (); // multiple sets of case double ans = 0; // records the total edge weight for (INT I = 1; I <n; I ++) {que. push (Pr (d [0] [I], point (I, 0); // Add the priority queue to prim connected to 0} while (Num <n) {double TD = que. top (). first; // get the int T = que. top (). second. first; int F = que. top (). second. second; que. pop (); If (vis [T]) continue; // if already added, vis [T] = true; num ++; ans ++ = TD; // Add some edge maxd [T] [f] = maxd [f] [T] = TD; // DP calculates the start point of the maximum edge, these two points are directly connected to for (INT I = 0; I <n; I ++) {If (! Vis [I]) {que. push (Pr (d [T] [I], point (I, T); // continue updating outside} else {if (I! = T) {maxd [T] [I] = maxd [I] [T] = max (maxd [f] [I], TD ); // evaluate the DP in it }}} return ans;} int main () {int t; scanf ("% d", & T); While (t --) {scanf ("% d", & N); For (INT I = 0; I <n; I ++) {scanf ("% d ", & V [I]. x, & V [I]. y, & V [I]. p) ;}for (INT I = 0; I <n; I ++) {// create a graph for (Int J = 0; j <= I; j ++) {d [I] [J] = d [J] [I] = SQRT (V [I]. x-V [J]. x) * (V [I]. x-V [J]. x) + (V [I]. y-V [J]. y) * (V [I]. y-V [J]. y) ;}} double allt = prim (); double maxrate =-1; for (INT I = 0; I <n; I ++) {// obtain all possible city pairs for (Int J = 0; j <I; j ++) {double rate = (V [I]. P + V [J]. p)/(allt-maxd [I] [J]); maxrate = max (maxrate, rate) ;}} printf ("%. 2f \ n ", maxrate);} return 0 ;}

  

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.