HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)

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

时间: 2024-08-30 11:30:15

HDU-4081.Qinshihuang'sNationalRoadSystem(次小生成树变种)的相关文章

HDU 4081 Qin Shi Huang&#39;s National Road System 次小生成树变种

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) [Problem Description] During the Warring States Period of ancient China(476 BC to 221 BC), there were seven kingdoms in China ---

hdu 4463 Outlets Prim 次小生成树 简单题

Outlets Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 2200    Accepted Submission(s): 1028 Problem Description In China, foreign brand commodities are often much more expensive than abroad. T

HDU 4081 Qin Shi Huang&#39;s National Road System(最小生成树/次小生成树)

题目链接:传送门 题意: 有n坐城市,知道每坐城市的坐标和人口.现在要在所有城市之间修路,保证每个城市都能相连,并且保证A/B 最大,所有路径的花费和最小,A是某条路i两端城市人口的和,B表示除路i以外所有路的花费的和(路径i的花费为0). 分析: 先求一棵最小生成树,然后枚举每一条最小生成树上的边,删掉后变成两个生成树,然后找两个集合中点权最大的两 个连接起来.这两个点中必然有权值最大的那个点,所以直接从权值最大的点开始dfs. 为了使A/B的值最大,则A尽可能大,B尽可能小.所以B中的边一定

hdu 4081 Qin Shi Huang&#39;s National Road System 次小生成树 算法

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4180    Accepted Submission(s): 1450 Problem Description During the Warring States Period of ancient China(4

HDU 4081—— Qin Shi Huang&#39;s National Road System——————【次小生成树、prim】

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5608    Accepted Submission(s): 1972 Problem Description During the Warring States Period of ancient China(47

hdu 4081 Qin Shi Huang&#39;s National Road System 树的基本性质 or 次小生成树

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

hdu 4081 Qin Shi Huang&#39;s National Road System (次小生成树)

Qin Shi Huang's National Road System Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3843    Accepted Submission(s): 1336 Problem Description During the Warring States Period of ancient China(47

HDU 4081 Qin Shi Huang&#39;s National Road System 次小生成树

给你n个城市 每个城市有一定数量的人 连接2个城市需要的花费是他们之间的距离 现在要建一颗最小生成树 可以免费建其中一条边 设A为免费的那条边连接的2个城市的人口之和 B为修建的最小生成树的花费 求最大的A/B 先求最小生成树 设总花费为totol 然后可以枚举免费的那条边 枚举边等于A确定 在这条在最小生成树里的情况下 求最小的B 如果这条边是最小生成树里的边 那么很容易求得B 拿totol减去这条边就行了 如果不是 那么把这条边加到最小生成树 会出现一个环 找到这个环最大的那条边剪掉 就是次

hdu 4081 次小生成树

简单说下思想(这里引用自http://blog.csdn.net/jarily/article/details/8883858) /* *算法引入: *设G=(V,E,w)是连通的无向图,T是图G的一棵最小生成树; *如果有另一棵树T1,满足不存在树T',ω(T')<ω(T1),则称T1是图G的次小生成树; * *算法思想: *邻集的概念:由T进行一次可行交换得到的新的生成树所组成的集合,称为树T的邻集,记为N(T); *设T是图G的最小生成树,如果T1满足ω(T1)=min{ω(T')|T'∈