Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 6090 Accepted Submission(s): 2125
Total Submission(s): 6090 Accepted Submission(s): 2125
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.
![](https://i-blog.csdnimg.cn/blog_migrate/0463d69c7b3c446140af4f4b66ad5007.jpeg)
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.
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
题意:有n座城,每座城有一定人口,现在秦始皇想建n-1条边联通n座城,徐福可以把其中一条道路施加魔法,使它不用花费金钱和人力。秦始皇希望完工后,被施展魔法联通的两个城市的总人口A,与除开这条道路的n-2条道路的长度和B作除法即A/B最大,输出A/B。
思路:要使得A/B最大,B就要尽量少,要问使得n-1条路的权值和最小,肯定要是最小生成树。现在因为有一条边可以用魔法建造,但是是哪条?可以枚举每一条边,如果这条边本来就在最小生成树上,长度为dis(i,j),这时候有A/(B-dis(i,j)),如果这条边不在最小生成树上,如果加入这条边,会有n条边,最小生成树形成环,得删除一条边,为了A/B最大,自然是删除除了新加入的边(i,j)外,最小生成树点i和点j间的最长的边,长度是dp(i,j),那么有A/(B-dp(i,j))。这样枚举后便能获得A/B最大值。(这样将最小生成树的一条边更换,就是次小生成树)
最小生成树点i和点j之间最长的边,可以用dp得出。
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
#define maxn 1005
#define inf 999999999
using namespace std;
double edge[maxn][maxn], dp[maxn][maxn], mincost[maxn];
int num[maxn], p[maxn][2], pre[maxn], N;
bool vis[maxn], used[maxn][maxn];
double MST, res;
double dis(int x1, int y1, int x2, int y2){
return sqrt((x1-x2)*(x1-x2)*1.0+(y1-y2)*(y1-y2)*1.0);
}
void prim()
{
memset(vis, 0, sizeof vis);
memset(used, 0, sizeof used);
memset(dp, 0, sizeof dp);
//memset(pre, 0, sizeof pre);
for(int i = 1;i <= N;i++){
mincost[i] = edge[1][i];
pre[i] = 1;
}
vis[1] = 1;
int v;
MST = 0;
while(true){
v = -1;
for(int u = 1;u <= N;u++){
if(!vis[u]&&(v==-1||mincost[u]<mincost[v])) v = u;
}
if(v == -1) break;
used[v][pre[v]] = used[pre[v]][v] = 1;
//printf("%d %d\n", v, pre[v]);
MST += edge[v][pre[v]];
vis[v] = 1;
for(int j = 1;j <= N;j++){
if(vis[j]&&j!=v){
dp[v][j] = dp[j][v] = max(dp[j][pre[v]], mincost[v]);
}
if(!vis[j]){
if(mincost[j] > edge[v][j]){
mincost[j] = edge[v][j];
pre[j] = v;
}
}
}
}
}
int main()
{
int t, i, j;
scanf("%d", &t);
while(t--){
scanf("%d", &N);
for(i = 1;i <= N;i++)
scanf("%d %d %d", &p[i][0], &p[i][1], &num[i]);
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++){
if(i != j) edge[i][j] = edge[j][i] = dis(p[i][0], p[i][1], p[j][0], p[j][1]);
else edge[i][j] = 0;
}
prim();
res = 0;
for(i = 1;i <= N;i++)
for(j = 1;j <= N;j++){
if(i != j){
//printf("%lf\n", (edge[i][j]));
if(used[i][j]) res = max(res, (num[i]+num[j])*1.0/(MST-edge[i][j]));
else res = max(res, (num[i]+num[j])*1.0/(MST-dp[i][j]));
}
}
printf("%.2lf\n", res);
}
}