HDU4081 Qin Shi Huang's National Road System —— 次小生成树变形

 题目链接:https://vjudge.net/problem/HDU-4081

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8970    Accepted Submission(s): 3175


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
 
Recommend
lcy

题解:

问题:给出n个点及其坐标,求一棵生成树,使得A/B最大,其中A为生成树中某一条边(我们称之为魔法边)两端点的value和,B为生成树中除了上述边之外的其他边的权值之和。

看到了比率,还有生成树,第一反应是最优比率生成树。但再想想,最优比率生成树每次找答案都需要重置边权,而且好像也不能选定一条边之类的功能。总之,行不通。后来换了个角度思考,就柳暗花明了:

1.假定魔法边就是 u--v ,由于val[u]和val[v]是确定的,所以A/B中的A就已经确定了,为了使得A/B最大,我们要做的就是使得B最小,即使得除了魔法边之外的其他生成树边的权值和最小。所以我们就需要求出最小生成树。然后我们把u到v路径上边权最大的边删掉,然后用魔法边把u和v直接相连(两个操作之后仍然构成生成树)。这样,我们就求出了除了魔法边之外的其他生成树边的最小权值和。

2.有了上述结论,我们就可以用类似求次小生成树的方法,求出魔法边和A/B的最大值了。

代码如下:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cmath>
 4 #include <algorithm>
 5 #include <cstring>
 6 using namespace std;
 7 typedef long long LL;
 8 const double EPS = 1e-6;
 9 const int INF = 2e9;
10 const LL LNF = 9e18;
11 const int MOD = 1e9+7;
12 const int MAXN = 1e3+10;
13 
14 double cost[MAXN][MAXN], lowc[MAXN], Max[MAXN][MAXN];
15 bool vis[MAXN], used[MAXN][MAXN];
16 int x[MAXN], y[MAXN], val[MAXN],  pre[MAXN];
17 
18 double Prim(int st, int n)
19 {
20     double ret = 0;
21     memset(vis, false, sizeof(vis));
22     memset(used, false, sizeof(used));
23     memset(Max, 0, sizeof(Max));
24 
25     for(int i = 1; i<=n; i++)
26         lowc[i] = (i==st)?0:INF;
27     pre[st] = st;
28 
29     for(int i = 1; i<=n; i++)
30     {
31         int k;
32         double minn = INF;
33         for(int j = 1; j<=n; j++)
34             if(!vis[j] && minn>lowc[j])
35                 minn = lowc[k=j];
36 
37         ret += lowc[k];
38         vis[k] = true;
39         used[pre[k]][k] = used[k][pre[k]] = true;
40         for(int j = 1; j<=n; j++)
41         {
42             if(vis[j] && j!=k)
43                 Max[j][k] = Max[k][j] = max(Max[j][pre[k]], lowc[k]);
44             if(!vis[j] && lowc[j]>cost[k][j])
45             {
46                 lowc[j] = cost[k][j];
47                 pre[j] = k;
48             }
49         }
50     }
51     return ret;
52 }
53 
54 double SMST(double sum, int n)
55 {
56     double ret = 0;
57     for(int i = 1; i<=n; i++)
58         for(int j = i+1; j<=n; j++)
59             ret = max(ret, (val[i]+val[j])/(sum-Max[i][j]));
60     return ret;
61 }
62 
63 int main()
64 {
65     int T, n;
66     scanf("%d", &T);
67     while(T--)
68     {
69         scanf("%d",&n);
70         for(int i = 1; i<=n; i++)
71             scanf("%d%d%d", &x[i], &y[i], &val[i]);
72 
73         for(int i = 1; i<=n; i++)
74         for(int j = 1; j<=n; j++)
75             cost[i][j] = sqrt( 1.0*(x[i]-x[j])*(x[i]-x[j])+1.0*(y[i]-y[j])*(y[i]-y[j]) );
76 
77         double sum = Prim(1, n);
78         double ans = SMST(sum, n);
79         printf("%.2f
", ans);
80     }
81 }
View Code
原文地址:https://www.cnblogs.com/DOLFAMINGO/p/7754124.html