Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 8430 Accepted Submission(s): 2987
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.
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
Statistic | Submit | Discuss | Note
题目大意:
秦始皇想要为所有城市间修路,路的花费等于两点的人口数之和。有个道士说可以不费人力任意修一条路。为最大限度地节省人力财力,皇帝给出了要求使A/B最大,A为魔法路的花费,B为剩余路的距离和。求A/B的最大值。
思路:
以前最小生成树都是用求边的kruskal算法做,突然用到prim算法还有点不习惯。
使人力花费最小,即建造一个最小生成树,用max[][]数组存任意两点的最大距离。然后枚举每一条路,如果在生成树中,直接减去。如果不在生成树中,必然组成了环,减去环中最大的距离即可。
附上AC代码:
#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000+5;
const int INF=0x7f7f7f;
int used[maxn][maxn];//标记该边是否在最小生成树中
double _max[maxn][maxn];//记录两点的最大距离
double cost[maxn][maxn];//记录两点之间距离
double lowc[maxn];//记录与该点相连的点的最小距离
int vis[maxn];//标记是否扫面过该点
int pre[maxn];//标记前驱
int T,n;
struct points{
double x,y,val;
}point[maxn];
double Dis(points a,points b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double prim(int n){
memset(vis,0,sizeof(vis));
memset(_max,0,sizeof(_max));
memset(used,0,sizeof(used));
double ans=0;
vis[0]=1;
pre[0]=-1;
for(int i=1;i<n;i++){
lowc[i]=cost[0][i];
pre[i]=0;
}
lowc[0]=0;
for(int i=1;i<n;i++){
double _min=INF*1.0;
int minid=-1;
for(int j=0;j<n;j++){
if(!vis[j]&&_min>lowc[j]){
_min=lowc[j];
minid=j;
}
}
ans+=_min;
vis[minid]=1;
used[minid][pre[minid]]=used[pre[minid]][minid]=1;
for(int j=0;j<n;j++){
if(vis[j]&&j!=minid){
_max[j][minid]=_max[minid][j]=max(_max[j][pre[minid]],lowc[minid]);
}
if(!vis[j]&&lowc[j]>cost[minid][j]){
lowc[j]=cost[minid][j];
pre[j]=minid;
}
}
}
return ans;
}
int main(){
ios::sync_with_stdio(false);
cin>>T;
while(T--){
cin>>n;
for(int i=0;i<n;i++){
cin>>point[i].x>>point[i].y>>point[i].val;
}
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
cost[i][j]=cost[j][i]=Dis(point[i],point[j]);
}
}
double dis=prim(n);
double sum=-1;
for(int i=0;i<n;i++){
for(int j=i+1;j<n;j++){
if(used[i][j]){
sum=max(sum,1.0*(point[i].val+point[j].val)/(dis-cost[i][j]));
}
else{
sum=max(sum,1.0*(point[i].val+point[j].val)/(dis-_max[i][j]));
}
}
}
printf("%.2f\n",sum);
}
return 0;
}