Qin Shi Huang ' s National Road SystemTime
limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 4180 Accepted Submission (s): 1450
Problem descriptionduring 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.
Inputthe 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.
Outputfor 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
241 1 201 2 30200 2 80200 1 10031 1 201 2 302 2 40
Sample Output
65.0070.00
Reprint Blog Please declare the original address: Http://blog.csdn.net/lionel_d
The stupidest thing I've ever done in my life is to define the array type incorrectly, causing me to wrong 10 times ... The max array should be defined as a double, and the result is a dummy, defined as int. Really want to wall!!! Do not know the next niche into a tree, see this blog http://www.cnblogs.com/hxsyl/p/3290832.html. That's very detailed. At the beginning of the time, a little ideas are not, online search is clearly the smallest spanning tree problem, how to use the minimum spanning tree is difficult to solve it! Baidu found that, is the second niche into a tree. And the first K-niche into a tree!!! , tired sleep do not love below to see my code it:
#include <stdio.h> #include <math.h> #include <string.h> #include <stdlib.h> #define MAX 1010const double INF = 100000000.0; struct point{int x, y;}; Double Graph[max][max], Lowcost[max], max[max][max];//max[i][j] denotes one of the largest path weights in all paths of I to J, int Pop[max], Closest[max];//pop : Save the number of people per city, closest save the shortest path of the vertex bool Used[max][max];//used[i][j] Mark I to J this road is not in the smallest spanning tree. Double prim (int n) {bool Visited[max], memset (visited,false,sizeof (visited)), memset (max,0,sizeof (MAX)), memset (used, False,sizeof (used)); for (int i = 1; I <= n; ++i) {closest[i] = 1; lowcost[i] = Graph[1][i];} Visited[1] = True;d ouble sum = 0.0; for (int i = 0, i < n-1; ++i) {double min = INF; int index =-1; for (int j = 1; j <= N; ++J) {if (!visited[j] && lowcost[j]<min) {min = Lowcost[j];index = j;}} if (index = =-1) {break;} Visited[index] = true; sum + = Lowcost[index]; Used[index][closest[index]] = used[closest[index]][index] = true; for (Int J = 1; J <= N; ++J) {if (Visited[j] && Index! = J{Max[index][j] = Max[j][index] = Max[j][closest[index]]>lowcost[index]? Max[j][closest[index]]:lowcost[index]; }if (!visited[j] && lowcost[j]>graph[index][j]) {lowcost[j] = Graph[index][j]; closest[j] = index;}}} return sum;} Double dis (const point &a, const point &b) {Double x = (a.x-b.x) *1.0, y = (a.y-b.y) *1.0; return sqrt (x*x+y*y);} int main () {int t; Point P[max]; scanf ("%d", &t), and while (t--) {int n;scanf ("%d", &n), for (int i = 1; I <= n; ++i) {scanf ("%d%d%d",& P[i].x,&p[i].y,&pop[i]);} for (int i = 1; I <= n; ++i) {Graph[i][i] = 0.0, for (int j = 1; j < i; ++j) {graph[i][j] = graph[j][i] = Dis (p[i],p [j]);}} Double sum = Prim (n), ans = -1;for (int i = 1; I <= n; ++i) {for (int j = 1; j <= N; ++j) {if (i! = j) {if (Used[i][j] {Double R = (Pop[i]+pop[j]) *1.0/(sum-graph[i][j]); ans = ans>r?ans:r;} Else{double r = (Pop[i]+pop[j]) *1.0/(sum-max[i][j]); ans = Ans>r?ans:r;}}} printf ("%.2lf\n", ans);} return 0;}
with June
HDU 4081 Qin Shi Huang ' s national Road System sub-niche tree algorithm