hdu4081---Qin Shi Huang's National Road System

Qin Shi Huang's National Road System

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4043    Accepted Submission(s): 1393


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.
hdu4081---Qin Shi Huang's National Road System_第1张图片
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.
 

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
2011 Asia Beijing Regional Contest
 

Recommend
lcy   |   We have carefully selected several similar problems for you:   4085  4082  4089  4090  4087 
 

这题光求最小生成树是不够的,因为最小生成树不一定唯一
做法是次小生成树,然后枚举边

/*************************************************************************
    > File Name: hdu4081.cpp
    > Author: ALex
    > Mail: [email protected] 
    > Created Time: 2015年01月25日 星期日 17时40分08秒
 ************************************************************************/

#include <map>
#include <set>
#include <queue>
#include <stack>
#include <vector>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>

using namespace std;

const int N = 1010;
const int inf = 0x3f3f3f3f;
bool vis[N];
double lowc[N];
int pre[N];
double F[N][N];
double edge[N][N];
bool used[N][N];
struct node
{
	int x, y, num;
}city[N];

double prim (int n)
{
	memset (vis, 0, sizeof(vis));
	memset (used, 0, sizeof(used));
	memset (F, 0, sizeof(F));
	vis[1] = 1;
	pre[1] = -1;
	double sum = 0;
	for (int i = 2; i <= n; ++i)
	{
		lowc[i] = edge[1][i];
		pre[i] = 1;
	}
	pre[1] = -1;
	lowc[1] = 0;
	for (int i = 2; i <= n; ++i)
	{
		double minc = (double)inf;
		int p;
		for (int j = 1; j <= n; ++j)
		{
			if (!vis[j] && lowc[j] < minc)
			{
				minc = lowc[j];
				p = j;
			}
		}
		if (minc == inf)
		{
			return -1;
		}
		vis[p] = 1;
		sum += minc;
		used[pre[p]][p] = used[p][pre[p]] = 1;
		for (int j = 1; j <= n; ++j)
		{
			if (vis[j] && j != p)
			{
				F[j][p] = F[p][j] = max(F[j][pre[p]], lowc[p]);
			}
			if (!vis[j] && edge[p][j] < lowc[j])
			{
				lowc[j] = edge[p][j];
				pre[j] = p;
			}
		}
	}
	return sum;
}

double dist (node a, node b)
{
	int x = (a.x - b.x);
	int y = (a.y - b.y);
	return sqrt (double(x * x + y * y));
}

void solve (int n, double sum)
{
	double ans = 0;
	for (int i = 1; i <= n; ++i)
	{
		for (int j = i + 1; j <= n; ++j)
		{
			if (!used[i][j] && edge[i][j] != inf)
			{
				ans = max(ans, (1.0 * (city[i].num + city[j].num)) / (1.0 * (sum - F[i][j])));
			}
			if (used[i][j])
			{
				ans = max(ans, (1.0 * (city[i].num + city[j].num)) / (1.0 * (sum - edge[i][j])));
			}
		}
	}
	printf("%.2f\n", ans);
}

int main ()
{
	int t;
	scanf("%d", &t);
	while (t--)
	{
		int n;
		scanf("%d", &n);
		for (int i = 1; i <= n; ++i)
		{
			scanf("%d%d%d", &city[i].x, &city[i].y, &city[i].num);
		}
		memset (edge, inf, sizeof(edge));
		for (int i = 1; i <= n; ++i)
		{
			for (int j = i + 1; j <= n; ++j)
			{
				edge[i][j] = edge[j][i] = dist (city[i], city[j]);
			}
		}
		double sum = prim(n);
		solve (n, sum);
	}
	return 0;
}


你可能感兴趣的:(hdu4081---Qin Shi Huang's National Road System)