HDU4081 Qin Shi Huang ' s national Road System "Kruska" "sub-niche into a tree"

Source: Internet
Author: User

Qin Shi Huang ' s National Road System

Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) total submission (s): 3979 Accepted S Ubmission (s): 1372
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 in 22 1 BC. That is Qin dynasty----the first imperial dynasty of China (not to being confused with the Qing dynasty, the Last dynasty O F 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-siz Ed Mausoleum guarded by a life-sized Terracotta Army, and a massive national road system. There is a stories about the road system:
There were n cities in China and Qin Shi Huang wanted them all being connected by n-1 roads, in order so he could go to Eve Ry City from the capital city Xianyang.
Although Qin Shi Huang is a tyrant, he wanted the total length of all roads to is minimum,so that the road system could 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 being as small as possible, but Xu Fu wanted the Magic Road To benefit as many people as possible----so Qin Shi Huang decided the value of A/b (the ratio of A to B) must is th e maximum, which A is the total population of the The both of the cites connected by the Magic Road, and B are the total length of none Magic Roads.
Would Qin Shi Huang?
A city can is considered as a point, and a road can is considered as a line segment connecting the points.

Input
The first line contains a integer t meaning that there is t test Cases (T <= 10).
For each test case:
The first line was an integer n meaning this there is n cities (2 < n <= 1000).
then n lines follow. Each line contains three integers x, y and p (0 <= X, y <=, 0 < P < 100000). (X, Y) is the coordinate of a city and P is the population of the.
It is guaranteed, that, each of the city have 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 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

Asia Beijing Regional Contest


Questions The main idea : There are n cities, Qin Shihuang to use N-1 Road to connect them all together, Qin Shihuang hope this N-1 road length

and the shortest. At this time, Xufu jumped out to say that he has magic, can be out of the way any one of them.

Qin Shihuang hope that Xu Fu will N-1 the longest road in the road, but Xufu hope to put the road to the most demanding the road

The road is changed (the manpower of each road is the population of the two cities). Eventually, Qin Shihuang gave a formula A/b

The road that Xu Fu has changed is the manpower required/except for the N-2 Road and the largest one.

The simplification is: give you the coordinates of n cities (x, Y) and the population. After getting his minimum spanning tree, remove the minimum spanning tree

An edge that makes this road connects the number of cities on both sides and/or the minimum spanning tree on the length of other N-2 routes and (A/b) maximum.

Output this maximum ratio.

Idea: In order to make the above formula A/b ratio maximum, you need B as small as possible.

To find the minimum spanning tree MST, and then select which edge to delete, you can traverse the edges on the MST, assuming that the edge length of the enumeration is

LEN[I][J], the end result is Max (A/mst-len[i][j]) (1<=i<=n,i+1<=j<=n). Where a is the minimum spanning tree

on the side. If a is not an edge on the minimum spanning tree. The minimum spanning tree plus a edge becomes a ring. To make him a former

A spanning tree, it is necessary to delete this ring in addition to the edge of a side outside the right value, is the original minimum spanning tree weight of the largest edge.

The end result is Max (A/mst-len[i][j]) (1<=i<=n,i+1<=j<=n). And a spanning tree formed by a side is actually

Is the second niche into a tree. So the problem is converted to the sub-niche into a tree, and then traverse the results.

Note: The code is timed out in C + + and g++ commits on AC. Don't know what the situation is ... C + + is arguably more efficient than g++.

Just right. Do not know what is the reason ~ Ask the great God to explain the explanation ha ~ Grateful.

Reference post: http://tech.ddvip.com/2013-10/1380576235203623.html


#include <algorithm> #include <cstdio> #include <cstring> #include <cmath>using namespace std; const int MAXN = 1010;const int MAXM = 1000010;int father[maxn];int Find (int x) {if (x! = Father[x]) father[x] =    Find (Father[x]); return father[x];}    struct node{int from;    int to; Double W;}; Node Edges[maxm];bool CMP (node A,node b) {return A.W < B.W;}    struct node1{int to; int next;};    Node1 vertex[maxn];struct node2{int x;    int y; Double D;};    Node2 POINT[MAXN];d ouble Dist (int i,int j) {Double x = point[i].x-point[j].x;    Double y = point[i].y-point[j].y; return sqrt (x*x+y*y);}    int N,M,HEAD[MAXN],END[MAXN];d ouble LEN[MAXN][MAXN];d ouble Kruskal () {int i,x,y,w,v,k = 0;    Double ans = 0;       for (i = 1; I <= N; ++i) {vertex[i].to = i;       Vertex[i].next =-1;    Head[i] = end[i] = i;        } for (i = 0; i < M; ++i) {if (k = = N-1) break;      if (EDGES[I].W < 0) continue;  x = Find (Edges[i].from);        y = Find (edges[i].to); if (x! = y) {for (w = head[x]; W! =-1; w = vertex[w].next) {for (v = head[y]; V! =-1; v = vertex[v].next) {len[vertex[w].to][vertex[v].to] = len[vertex[v].to][vertex[w].to]                = EDGES[I].W;            }} Father[y] = x;//changed to father[x] = y is wrong vertex[end[y]].next = head[x];            HEAD[X] = Head[y];            k++;            Ans + = EDGES[I].W;        if (k = = N-1) break; }} return ans; Double MAXV (double a,double b) {return a > B? a:b;}    int main () {int t,i,j;    scanf ("%d", &t);        while (t--) {scanf ("%d", &n);        for (i = 1; I <= N; ++i) father[i] = i;        for (i = 1; I <= N; ++i) {scanf ("%d%d%lf", &AMP;POINT[I].X,&AMP;POINT[I].Y,&AMP;POINT[I].D);        } M = 0; for (i = 1; I <= N; ++i) {           for (j = i+1; J <= N; ++j) {edges[m].from = i;                Edges[m].to = j;            EDGES[M++].W = Dist (i,j);        }} sort (edges,edges+m,cmp);        Double MST = Kruskal ();        Double ans = 0; for (i = 1; l <= N; ++i) {for (j = i+1; J <= N; ++j) ans = maxv (ans, (point[i].d + Po        INT[J].D)/(Mst-len[i][j]));    } printf ("%.2lf\n", ans); } return 0;}



HDU4081 Qin Shi Huang ' s national Road System "Kruska" "sub-niche into a tree"

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.