Qin Shi Huang‘s National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 10874 Accepted Submission(s): 3846
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 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 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
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
2011 Asia Beijing Regional Contest
Recommend
lcy
本题思路:利于最小生成树得到次小生成树的思路,先求出最小生成树和图中每两个顶点之间路径的最大值,然后对于每一条边运算A / B 即可。这里需要注意的是需要对每一条边进行测试(想想为什么?)。
参考代码:
1 #include <cstdio> 2 #include <cmath> 3 #include <vector> 4 #include <cstring> 5 #include <algorithm> 6 using namespace std; 7 8 const int maxn = 1000 + 5, maxe = 1000 * 1000 / 2 + 5, INF = 0x3f3f3f3f; 9 int n, m, head[maxn]; 10 double Max[maxn][maxn]; 11 struct City { 12 int u, v, population; 13 }city[maxn]; 14 struct Edge{ 15 int u, v, population; 16 double w; 17 bool vis; 18 }edge[maxe]; 19 vector <int> G[maxn]; 20 21 bool cmp(const Edge &a, const Edge &b) { 22 return a.w < b.w; 23 } 24 25 int Find(int x) { 26 if(x == head[x]) return x; 27 else return head[x] = Find(head[x]); 28 } 29 30 double Distance(int i, int j) { 31 int x1 = city[i].u, y1 = city[i].v, 32 x2 = city[j].u, y2 = city[j].v; 33 return sqrt((double)(x2 - x1) * (x2 - x1) + (double)(y2 - y1) * (y2 - y1)); 34 } 35 36 double Kruskal() { 37 int cnt = 0; 38 double ans = 0; 39 sort(edge + 1, edge + m + 1, cmp); 40 for(int i = 1; i <= n; i ++) { 41 G[i].clear(); 42 G[i].push_back(i); 43 head[i] = i; 44 } 45 for(int i = 1; i <= m; i ++) { 46 int fx = Find(edge[i].u), fy = Find(edge[i].v); 47 if(cnt == n - 1) break; 48 if(fx != fy) { 49 edge[i].vis = true; 50 ans += edge[i].w; 51 cnt ++; 52 int len_fx = G[fx].size(), len_fy = G[fy].size(); 53 for(int j = 0; j < len_fx; j ++) { 54 for(int k = 0; k < len_fy; k ++) { 55 Max[G[fx][j]][G[fy][k]] = Max[G[fy][k]][G[fx][j]] = edge[i].w; 56 } 57 } 58 head[fx] = fy; 59 for(int j = 0; j < len_fx; j ++) { 60 G[fy].push_back(G[fx][j]); 61 } 62 } 63 } 64 if(cnt < n - 1) return INF; 65 return ans; 66 } 67 68 double Second_Kruskal(double MST) { 69 double ans = 0; 70 for(int i = 1; i <= m; i ++) { 71 ans = max(ans, edge[i].population / (MST - Max[edge[i].u][edge[i].v])); 72 } 73 return ans; 74 } 75 76 int main () { 77 int t; 78 scanf("%d", &t); 79 while(t --) { 80 m = 0; 81 scanf("%d", &n); 82 for(int i = 1; i <= n; i ++) { 83 scanf("%d %d %d", &city[i].u, &city[i].v, &city[i].population); 84 } 85 for(int i = 1; i <= n - 1; i ++) { 86 for(int j = i + 1; j <= n; j ++) { 87 edge[++m].u = i; 88 edge[m].v = j; 89 edge[m].vis = false; 90 edge[m].population = city[i].population + city[j].population; 91 edge[m].w = Distance(i, j); 92 } 93 } 94 double MST = Kruskal(); 95 double ans = Second_Kruskal(MST); 96 printf("%.2f\n", ans); 97 } 98 return 0; 99 }
HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)
原文地址:https://www.cnblogs.com/bianjunting/p/10840140.html