Qin Shi Huang's National Road System
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 7623 Accepted Submission(s): 2673
![](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.
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.
2 4 1 1 20 1 2 30 200 2 80 200 1 100 3 1 1 20 1 2 30 2 2 40
65.00 70.00
秦始皇修路:
刘汝佳的算法竞赛入门经典中343页344页有关于生成树相关问题的讲解。
后面给出的例题就是秦始皇修路。这个问题可以用借鉴求次小生成树的方法去解决。
本来是看了思路后自己写的代码,不过一直都是超时。
然后就百度题解。因为自己原来是用克鲁斯卡尔写的。就是先用克鲁斯卡尔算法求最小生成树,然后枚举删除边u-v,然后最小生成树就变成两个图,
分别求两个图中最大的人口的节点。然后修一条魔法道路。求一个值更新ans.不过自己存储图的时候结构不合理,TLE,看到别人的存储方式比较好,就
改了自己的存储图的方式,又TLE。真的没办法了,照着人家改来改去,最后代码几乎一样。不过自己的秒提秒TLE,人家的秒提秒AC。最后好不容易过了
,就想想看看自己哪里错了,结果又提交了一次,发现超时了,才发现是卡过的,心累,感觉自己太执着,TLE一下午加一晚上都没换方法。
的确用克鲁斯卡尔求生成树在枚举边进行深搜的确时间复杂度高。还是看同一个博主的。就是用Prim算法求最小生成树,并在此过程中求节点间的最小瓶颈路。
这一点在刘汝佳的算法竞赛入门蓝皮书344页(求解次小生成树)处有说明。
次小生成树就是所有生成树按照权值和第二大的树。如果次小生成树的权值和与最小生成树相同,则最小生成树不唯一。
次小生成树的求解方法是:先求最小生成树,并在此过程中节点之间的最小瓶颈路。(最小生成树中节点 i 到节点 j 的最小瓶颈路是 i 到 j 的边重权值最大的)
如果在最小生成树的基础上在添加一条边u - v,则就会形成回路,那么要使新的生成树权值小些,就删除u-v在最小生成树中权值最大的边。
#include<iostream>
#include<algorithm>
#include<string.h>
#include<stdio.h>
#include<math.h>
#define inf 0x3f3f3f3f
#define max(a,b) a>b? a:b
using namespace std;
const int maxn = 1002;
double Map[maxn][maxn];
bool mark[maxn][maxn];
double maxcost[maxn][maxn];
///maxcost[i][j]用来存放最小生成树中节点i到节点j中的那条最大边的权值
int closest[maxn];
double lowcost[maxn];
int vis[maxn];
int N;
struct pos
{
int x; ///坐标
int y;
int p; ///人口数量
}point[maxn];
double dis(struct pos p1,struct pos p2)
{
double temp = ((p1.x-p2.x)*(p1.x-p2.x)+(p1.y-p2.y)*(p1.y-p2.y))*1.0;
return sqrt(temp);
}
double Prim()
{
double ans = 0,Min;
memset(mark,false,sizeof(mark));
memset(maxcost,0,sizeof(maxcost));
int u;
for(int i = 1; i <= N; i++)
{
vis[i] = 0;
lowcost[i] = Map[1][i];
closest[i] = 1;
}
vis[1] = 1;
lowcost[1] = 0;
for(int i = 1; i < N; i++)
{
Min = inf;
u = 0;
for(int j = 1; j <= N; j++)
{
if(!vis[j] && lowcost[j]<Min)
{
Min = lowcost[j];
u = j;
}
}
if(u == 0) return -1;
vis[u] = 1;
ans += Min;
int pre = closest[u];
mark[pre][u] = mark[u][pre] = true; ///代表该边被最小生成树选中
maxcost[pre][u] = Min; ///pre到u只有自身一条边,所以maxcost为Min
for(int j = 1; j <= N; j++)
{
///j!=u自我感觉可以不要,但是不要就会wa,求大神指点
/**个人看法,当j=u时,maxcost[u][u] = max(maxcost[u][pre],maxcost[pre][u],
当前maxcost[u][u]的值肯定是错的,毋庸置疑,但是后续求解的时候根本用不到这个值,
觉得如果不写j!=u不会对答案造成影响,但是不要这个条件就是会Wa**/
if(vis[j]==1 && j != u) ///vis[j]=1是为了只更新最小生成树中的值。
maxcost[j][u] = max(maxcost[j][pre],maxcost[pre][u]);
}
for(int j = 1; j <= N; j++)
{
if(!vis[j] && Map[u][j]<lowcost[j])
{
lowcost[j] = Map[u][j];
closest[j] = u;
}
}
}
return ans;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
double MST;
double ans = 0;
scanf("%d",&N);
for(int i = 1; i <= N; i++)
scanf("%d%d%d",&point[i].x,&point[i].y,&point[i].p);
for(int i = 1; i <= N; i++)
{
Map[i][i] = 0;
for(int j = i+1; j <= N; j++)
Map[i][j] = Map[j][i] = dis(point[i],point[j]);
}
MST = Prim();
for(int i = 1; i <= N; i++)
{
for(int j = 1; j <= N; j++)
{
if(i != j)
{
if(mark[i][j]) ///用魔法修的边i,j是最小生成树中的边
ans = max(ans,(point[i].p+point[j].p)/(MST-Map[i][j]));
else
ans = max(ans,(point[i].p+point[j].p)/(MST-maxcost[i][j]));
/**用魔法修的边i,j不在最小生成树里,加入i-j一条边,会形成环,
减去i-j在最小生成中的最大边权,得到新的生成树**/
}
}
}
printf("%.2lf\n",ans);
}
return 0;
}