Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3680 Accepted Submission(s): 1282
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
题意:n个点,要建n-1条边让n个点联通,每个点有一定的人口,修建每条边的花费为两点间的距离。现在可以用魔法建一条边,不需要任何花费。现在要让A/B最小,A为魔法所建的那条边的两个点的人口和,B为总花费。输出最小的A/B。
题解:解决这题首先要证明:当魔法所建的边确定的时候,要让B最小,剩下要建的边一定是最小生成树中的边。
1,当魔法所建的边为最小生成树上的边的时候,显然满足上述 定理。
2,当魔法所建的边不为最小生成树上的时候。由Kruska求最小生成树的过程我们可以知道,原最小生成树上,不与魔法所建边形成环的边一定是可选的,而要与魔法所形成环的边一定最长那条边不选,其余边要选。
仔细分析Kruska的算法过程即可证明上述定理的正确性。
当然我们去枚举魔法所建的边,仍然很麻烦,而且复杂度不靠谱。由上述定理可知,魔法所建的边其实就是替代了最小生成树上的一条边。我们可以枚举删除最小生成树的一条边,删完以后图就被分成了两个联通块,而要建一条边让两个联通块联通,我们可以贪心的选可以连接两个联通块的边中人口最多的那个条边即可,其实就是将分别两个联通块中人口最多的点连接起来。
复杂度O(n*n)。详情见代码:
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<set>
#include<vector>
#include<math.h>
using namespace std;
#define eps 1e-8
#define inff 1e20
int n;
struct node
{
int x,y;
int val;
}a[1100];
double dis[1100];
bool vis[1100];
int pre[1100];
double tu[1100][1100];
double juli(int i,int j)
{
return sqrt((a[i].x-a[j].x)*(a[i].x-a[j].x)*1.0+(a[i].y-a[j].y)*(a[i].y-a[j].y)*1.0);
}
struct lingjie
{
int en,next;
}E[1100*2];
int p[1100],num;
void init()
{
memset(p,-1,sizeof(p));
num=0;
}
void add(int st,int en)
{
E[num].en=en;
E[num].next=p[st];
p[st]=num++;
}
vector<pair<int,int> >ve;
double prim()
{
int i,j;
ve.clear();
init();
for(i=1;i<=n;i++)
{
dis[i]=inff;
vis[i]=false;
pre[i]=-1;
}
dis[1]=0;
double tem;
double re=0;
int id;
for(i=1;i<=n;i++)
{
tem=inff;
id=-1;
for(j=1;j<=n;j++)
{
if(vis[j])
{
continue;
}
if(dis[j]<tem)
{
tem=dis[j];
id=j;
}
}
vis[id]=true;
re+=dis[id];
if(pre[id]!=-1)
{
ve.push_back(make_pair(id,pre[id]));
add(id,pre[id]);
add(pre[id],id);
}
for(j=1;j<=n;j++)
{
if(dis[j]>tu[id][j])
{
dis[j]=tu[id][j];
pre[j]=id;
}
}
}
return re;
}
double dfs(int id,int fa)
{
double re=a[id].val;
int i,w;
for(i=p[id];i+1;i=E[i].next)
{
w=E[i].en;
if(w==fa)
continue;
re=max(re,dfs(w,id));
}
return re;
}
int main()
{
int i,j;
int t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
{
for(i=1;i<=n;i++)
{
scanf("%d%d%d",&a[i].x,&a[i].y,&a[i].val);
}
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
tu[i][j]=juli(i,j);
}
}
double tem;
tem=prim();
double ans=-inff;
double ix,fc;
for(i=0;i<(int)ve.size();i++)
{
ix=dfs(ve[i].first,ve[i].second);
fc=dfs(ve[i].second,ve[i].first);
ans=max(ans,(ix+fc)/(tem-tu[ve[i].first][ve[i].second]));
}
printf("%.2lf\n",ans);
}
}
return 0;
}