HDU - 4081
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.
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
题目大意:
有n个城市,需构造一个生成树(边的权值和为S),取其中的一条边s[i][j],使得这条边连接的两个城市的人口数之和a / (S - s[i][j]) 最大
可以确定的是,如果确定了徐福修建哪条路的话,a就确定了,所以就变成了求含这条边的最小的生成树。
先求一次最小生成树MST,然后枚举每条边
1 如果这条边在最小生成树里,比值为 a / (MST - s[i][j])
2 如果这条边不在最小生成树里,就把这条边加进去再减去一条边,就变成了n个点,n条边求次小生成树
怎么求次小生成树呢?
肯定不可能朴素的用prim或者kruskal 一遍遍的求,因为遍历边已经是O(n^2)了,所以我们想,在点i与点j之间加一条边肯定会形成一个环,只需要把这个环里除刚加的这条边以外最长的边删掉就OK
在求最小生成树的同时将i到j路径上的最长的边记录下来就可以O(1)的实现求次小生成树了
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<string>
#include<cstring>
#include<queue>
#include<cmath>
#define INF 0x3f3f3f3f
using namespace std;
int T,n;
double ans,ANS;
bool b[1010],used[1010][1010];
int ai[1010],bi[1010],c[1010],pre[1010];
double d[1010],a[1010][1010],p[1010][1010];
void prim()
{
for(int i = 1;i <= n; ++i)
{
d[i] = INF;
b[i] = 0;
}
// cout<<"d1 = "<<d[1]<<endl;
for(int i = 2;i <= n; ++i)
if(a[1][i])
{
d[i] = a[1][i];
pre[i] = 1;
}
b[1] = 1;
int u;
for(int i = 1;i < n; ++i)
{
double minn = INF * 1.0;
for(int j = 1;j <= n; ++j)
if(minn > d[j] && !b[j])
{
minn = d[j];
u = j;
}
b[u] = 1;
//cout<<"a = "<<a[pre[u]][u]<<endl;
// printf("%d %d\n",pre[u],u);
ans += a[pre[u]][u];
//cout<<"ans = "<<ans<<endl;
used[pre[u]][u] = used[u][pre[u]] = 1;
for(int j = 1;j <= n; ++j)
{
if(b[j] && j != u) p[u][j] = p[j][u] = max(p[j][pre[u]] , d[u]);
if(a[u][j] < d[j] && !b[j])
{
d[j] = a[u][j];
pre[j] = u;
}
}
}
}
int main()
{
// cout<<INF<<endl;
scanf("%d",&T);
while(T--)
{
scanf("%d",&n);
memset(ai , 0 , sizeof(ai));
memset(bi , 0 , sizeof(bi));
memset(c , 0 , sizeof(c));
memset(a , 0 , sizeof(a));
memset(used , 0 , sizeof(used));
for(int i = 1;i <= n; ++i)
scanf("%d%d%d",&ai[i],&bi[i],&c[i]);
for(int i = 1;i <= n; ++i)
for(int j = 1;j <= n; ++j)
{
a[i][j] = a[j][i] = sqrt((double)((ai[i] - ai[j]) * (ai[i] - ai[j]) + (bi[i] - bi[j]) * (bi[i] - bi[j])));
}
ans = 0;
prim();
ANS = 0;
//cout<<"ans = "<<ans<<endl;
//cout<<ANS<<endl;
for(int i = 1;i <= n; ++i)
for(int j = 1;j <= n; ++j)
if(i != j)
{
if(used[i][j])
{
ANS = max(ANS , (c[i] + c[j]) * 1.0 / (ans - a[i][j]));
//cout<<" d = "<<(c[i] + c[j])<<' '<<(ans - a[i][j])<<endl;
}
else ANS = max(ANS , (c[i] + c[j]) * 1.0 / (ans - p[i][j]));
}
//cout<<ANS<<endl;
printf("%.2f\n",ANS);
}
return 0;
}