首页 > ACM题库 > HDU-杭电 > HDU 4081-Qin Shi Huang’s National Road System-最小生成树-[解题报告]HOJ
2015
04-16

HDU 4081-Qin Shi Huang’s National Road System-最小生成树-[解题报告]HOJ

Qin Shi Huang’s National Road System

问题描述 :

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.
Stammering Aliens

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.

输入:

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.

输出:

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.

样例输入:

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

 

先求出最小生成树,然后枚举树上的边,对于每条边“分别”找出这条割边形成的两个块中点权最大的两个

1.由于结果是A/B,A的变化会引起B的变化,两个制约,无法直接贪心出最大的A/B,故要通过枚举

2.不管magic road要加在哪里,加的边是否是最小生成树上的边,都会产生环,我们都要选择一条边删掉

注意删掉的边必须是树的环上的边,为了使结果最大,即找出最大的边

3.可以枚举两点,找出边,也可以枚举边,找出点,我是用后者,感觉比较容易写也好理解

 

 

 

#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <vector>
using namespace std;
#define N 1050
#define INF 1000000
struct point
{
	double x,y;
	int num;
}p[N];
int n;
double map[N][N],dist[N],A,B;
vector<int>vec[N];
double getdistance(point aa,point bb)
{
	return sqrt((aa.x-bb.x)*(aa.x-bb.x)+(aa.y-bb.y)*(aa.y-bb.y));
}
int pre[N];
void prim()
{
	for(int i=1;i<=n;++i)
		dist[i]=INF*1.0;
	double min_edge;
	int min_p,now=1;
	for(int i=1;i<n;++i)// 这里dist 表示到达已经更新好的点集的最小距离
	{
		min_edge=INF*1.0;
		for(int i=1;i<=n;++i)
			if(map[now][i]>0)
			{
				if(dist[i]>map[now][i])
					dist[i]=map[now][i],pre[i]=now;
			}
		dist[now]=-1.0;
		for(int i=1;i<=n;++i)
			if(dist[i]>0&&dist[i]<min_edge)
			{
				min_edge=dist[i];
				min_p=i;
			}
		vec[pre[min_p]].push_back(min_p);  // 最小边有可能来自之前更新过的,这里WA
		vec[min_p].push_back(pre[min_p]);
		now=min_p;
		B+=min_edge;
	}
}
int vis[N];
int dfs(int u,int fa)
{
	int max_num=p[u].num;
	int ret=u;int tt;
	for(int i=0;i<vec[u].size();++i)
		if(vec[u][i]!=fa)
		{
			tt=dfs(vec[u][i],u);
			if(max_num<p[tt].num)
				max_num=p[tt].num,ret=tt;
		}
	return ret;
}

int main ()
{
	//freopen("aa.txt","r",stdin);
	//freopen("bb.txt","w",stdout);
	int test;scanf("%d",&test);
	while(test--)
	{
		scanf("%d",&n);
		for(int i=1;i<=n;++i)
			scanf("%lf%lf%d",&p[i].x,&p[i].y,&p[i].num);
	
		for(int i=1;i<=n;++i)
		{
			for(int j=i+1;j<=n;++j)
				map[i][j]=map[j][i]=getdistance(p[i],p[j]);
			map[i][i]=0.0;
		}
		for(int i=0;i<=n;++i)
			vec[i].clear();
		B=0.0;
		memset(pre,-1,sizeof(pre));
		prim();
		double ans=-1.0;
		double res;int t1,t2;
		for(int i=1;i<=n;++i)
			for(int j=0;j<vec[i].size();++j)  // 枚举边,对于这条割边两侧的连通图分别找出点权最大的点
			{
				t1=dfs(i,vec[i][j]);
				t2=dfs(vec[i][j],i);
				res=(p[t1].num+p[t2].num)/(B-map[i][vec[i][j]]);
				if(res>ans)
					ans=res;
			}
		ans+=(1e-8);
		printf("%.2lf\n",ans);
	}
	//system("pause");
	return 0;
}

 

版权声明:本文为博主原创文章,未经博主允许不得转载。

参考:http://blog.csdn.net/jackyguo1992/article/details/8038819


  1. 很高兴你会喜欢这个网站。目前还没有一个开发团队,网站是我一个人在维护,都是用的开源系统,也没有太多需要开发的部分,主要是内容整理。非常感谢你的关注。