#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1000+5;
int x[maxn],y[maxn],val[maxn];
int n;
struct edge{
int u,v;
double dist;
edge(int u=0,int v=0,double dist=0) :u(u),v(v),dist(dist) {}
bool operator < (const edge &rhs) const {
return dist>rhs.dist;
}
};
vector<vector<edge> > G(maxn);
double map[maxn][maxn];
double dist[maxn];
double f[maxn][maxn];
int vis[maxn];
void HeapPrim()
{
memset(vis,0,sizeof(vis));
memset(f,0,sizeof(f));
memset(dist,0x7f,sizeof(dist));
priority_queue<edge> pq;
pq.push(edge(0,1,0));
edge nd;
double ans=0;
while(!pq.empty())
{
do{
nd=pq.top();
pq.pop();
}while(vis[nd.v]&&!pq.empty());
if(!vis[nd.v])
{
vis[nd.v]=1;
map[nd.u][nd.v]=map[nd.v][nd.u]=0;
ans+=nd.dist;
f[nd.u][nd.v]=nd.dist;
for(int i=1;i<=n;i++)
{
if(vis[i]&&i!=nd.v)
{
f[i][nd.v]=max(f[i][nd.u],f[nd.u][nd.v]);
f[nd.v][i]=f[i][nd.v];
}
}
for(int i=0;i<G[nd.v].size();i++)
{
int e=G[nd.v][i].v;
if(!vis[e])
{
double d=G[nd.v][i].dist;
if(dist[e]>d){
dist[e]=d;
pq.push(edge(nd.v,e,d));
}
}
}
}
}
double ab=0;
for(int i=1;i<=n;i++)
{
for(int j=i+1;j<=n;j++)
{
ab=max(ab,(val[i]+val[j])/(ans-f[i][j]));
}
}
printf("%.2f\n",ab);
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(map,0,sizeof(map));
for(int i=0;i<=n;i++) {G[i].clear();}
scanf("%d",&n);
for(int i=1;i<=n;i++) {scanf("%d%d%d",&x[i],&y[i],&val[i]);}
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++)
{
int xx=x[i]-x[j];
int yy=y[i]-y[j];
G[i].push_back(edge(i,j,sqrt(xx*xx+yy*yy)));
G[j].push_back(edge(j,i,sqrt(xx*xx+yy*yy)));
map[i][j]=map[j][i]=sqrt(xx*xx+yy*yy);
}
}
HeapPrim();
}
return 0;
}
UVA 1494 Qin Shi Huang's National Road System(次小生成树+Prim)
最新推荐文章于 2020-07-01 21:30:20 发布