# 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.

### Ouput

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.

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

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<memory.h>
#include<cmath>
#define M 1010
#define inf 99999999999.0
using namespace std;
double g[M][M],path[M][M];
//注意存在小数所以改成double
double dist[M],cost[M];
//cost为每个城市的人口
int pre[M],vis[M],r[M][2];
//用数组r来保存每个城市的坐标，然后通过距离公式求出距离存在图g中
bool used[M][M];//最小树的标记数组
int n,m;
double mst;
void init(){//初始化
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
g[i][j]=g[j][i]=inf;
}
}
return;
}
void Found(){//距离公式计算出每条边的边长
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
g[i][j]=g[j][i]=sqrt((r[i][0]-r[j][0])*(r[i][0]-r[j][0])+(r[i][1]-r[j][1])*(r[i][1]-r[j][1]));
}
}
}
double prime(){
double mst=0.0;//最小生成树的值
memset(path,0,sizeof(path));
memset(vis,0,sizeof(vis));
memset(used,0,sizeof(used));
vis[1]=1;
for(int i=1;i<=n;i++){
dist[i]=g[1][i];
pre[i]=1;//记录前缀
}
for(int i=1;i<n;i++){
int u=-1;
for(int j=1;j<=n;j++){
if(!vis[j]){
if(u==-1||dist[j]<dist[u]){
u=j;
}
}
}
used[u][pre[u]]=used[pre[u]][u]=true;//确定此边已经使用过了
mst+=g[pre[u]][u];
vis[u]=1;
for(int j=1;j<=n;j++){
if(vis[j]&&j!=u){//path记录了已经加入最小生成树的点到u的最长路
path[j][u]=path[u][j]=max(path[j][pre[u]],dist[u]);
}
if(!vis[j]){
if(dist[j]>g[u][j]){//松弛
dist[j]=g[u][j];
pre[j]=u;
}
}
}
}
return mst;
}
double second_tree(){
double res=inf;
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(i!=j&&!used[i][j]){//次小生成树
res=min(res,mst-path[i][j]+g[i][j]);
}
}
}
return res;
}
int main(){
int t;
int x,y,z;
cin>>t;
while(t--){
cin>>n;
m=((n-1)*n)/2;
init();
memset(r,0,sizeof(r));
memset(cost,0,sizeof(cost));
for(int i=1;i<=n;i++){
scanf("%d%d%lf",&r[i][0],&r[i][1],&cost[i]);
}
Found();
mst=prime();
double sec_mst=second_tree();
double ratios= -1.0;
for(int i=1; i<=n; ++i){
for(int j=1; j<=n; ++j)if(i!=j){
if(used[i][j]){
ratios=max(ratios, (cost[i]+cost[j])/(mst-g[i][j]));
}
else{
ratios=max(ratios, (cost[i]+cost[j])/(mst-path[i][j]));
}
}
}
printf("%.2f\n", ratios);
}
return 0;
}

03-25 1099

01-18 1435

02-12 304

09-23 1692

03-22 604

10-16 888

02-04 296

11-04 1272

04-01 326

10-11 334