代码1:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
using namespace std;
const int N = 1000 + 10;
const double INF = 1.0*1e8;
struct point
{
double x,y;
double val;
}p[N];
double get_dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
vector<int> ed[N];
double map[N][N];
int n,pre[N];
double dis[N];
double sum;
void prim()
{
int now=1;
for(int i=1;i<=n;i++) ed[i].clear();
for(int i=1;i<=n;i++) dis[i] = 1.0*INF;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) if(now!=j && dis[j]>0 && map[now][j]>=0 && dis[j]>map[now][j]){
dis[j] = map[now][j];
pre[j] = now;
}
dis[now] = -1.0;
int min_p=-1;
double min_dis=1.0*INF;
for(int j=1;j<=n;j++) if(dis[j]>0){
if(dis[j] < min_dis)
{
min_p = j;
min_dis = dis[j];
}
}
if(min_p < 0) break;
sum += min_dis;
ed[min_p].push_back(pre[min_p]);
ed[pre[min_p]].push_back(min_p);
now = min_p;
}
}
double dfs(int x,int fa)
{
double max_val=p[x].val;
for(int i=0;i<ed[x].size();i++)
{
int j = ed[x][i];
if(j!=fa)
{
max_val = max( max_val,dfs(j,x) );
}
}
return max_val;
}
int main()
{
int t;
cin>>t;
while(t--)
{
sum = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].val);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
map[i][j] = get_dis(p[i],p[j]);
else
map[i][j] = 0.0;
prim();
double ans = 0;
for(int i=1;i<=n;i++)
for(int j=0;j<ed[i].size();j++)
{
int k = ed[i][j];
double s1 = dfs(i,k);
double s2 = dfs(k,i);
ans = max(ans, (s1+s2)/(sum - map[i][k]));
}
ans += 1e-8;
printf("%.2lf\n",ans);
}
return 0;
}
代码2:
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<iostream>
#include<vector>
#include<cstring>
using namespace std;
const int N = 1000 + 10;
const double INF = 1.0*1e8;
struct point
{
double x,y;
double val;
}p[N];
double get_dis(point a,point b)
{
return sqrt( (a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y) );
}
vector<int> ed[N];
double map[N][N];
int n,pre[N],vis[N];
double dis[N],maxcost[N][N];
double sum;
void prim()
{
int now=1;
for(int i=1;i<=n;i++) ed[i].clear();
for(int i=1;i<=n;i++) dis[i] = 1.0*INF;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++) if(now!=j && dis[j]>0 && map[now][j]>=0 && dis[j]>map[now][j]){
dis[j] = map[now][j];
pre[j] = now;
}
dis[now] = -1.0;
int min_p=-1;
double min_dis=1.0*INF;
for(int j=1;j<=n;j++) if(dis[j]>0){
if(dis[j] < min_dis)
{
min_p = j;
min_dis = dis[j];
}
}
if(min_p < 0) break;
sum += min_dis;
ed[min_p].push_back(pre[min_p]);
ed[pre[min_p]].push_back(min_p);
now = min_p;
}
}
void dfs(int s,int x)
{
for(int i=0;i<ed[x].size();i++){
int v = ed[x][i];
//printf("%d->%d\n",x,v);
if(vis[v]) continue;
vis[v] = 1;
maxcost[s][v] = max(maxcost[s][v] , max(map[x][v],maxcost[s][x]));
dfs(s,v);
}
}
int main()
{
int t;
cin>>t;
while(t--)
{
sum = 0;
scanf("%d",&n);
for(int i=1;i<=n;i++)
scanf("%lf%lf%lf",&p[i].x,&p[i].y,&p[i].val);
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(i!=j)
map[i][j] = get_dis(p[i],p[j]);
else
map[i][j] = 0.0;
prim();
double ans = 0;
//cout<<"hehe\n";
for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) maxcost[i][j] = 0;
for(int i=1;i<=n;i++){
memset(vis,0,sizeof(vis));
vis[i] = 1;
maxcost[i][i] = 0;
dfs(i,i);
}
//printf("orz\n");
for(int i=1;i<=n;i++)
for(int j=i+1;j<=n;j++)
{
ans = max(ans, (p[i].val+p[j].val)/(sum-maxcost[i][j]) );
}
ans += 1e-8;
printf("%.2lf\n",ans);
}
return 0;
}