主要就是库鲁斯卡儿后边会变得有序如果你是用邻接表连得图顺序就乱了然后建的表也木用了,所以不采用邻接表建图克鲁斯卡尔的时候。易错点!!!!!
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
struct edgee
{
int from, to;
double value;
bool operator<(edgee b)
{
return value < b.value;
}
};
edgee edge[2000000];
int edgetot;
void addedge(int a, int b,double value)
{
edge[edgetot].from = a;
edge[edgetot].value = value;
edge[edgetot].to = b;
edgetot++;
}
struct pointt
{
double x, y; int population;
pointt(double x, double y, int p) :x(x), y(y), population(p)
{}
pointt()
{}
};
double dist(pointt a, pointt b)
{
return sqrt((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y));
}
pointt point[1050];
vector<int>tree[1050];
vector<int>stack;
int fa[1050];
int n;
void init()
{
for (int i = 0; i <= n; i++)fa[i] = i;
}
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int visit[1050];
double map[1050][1050];
void dfs(int num)
{
visit[num] = 1;
stack.push_back(num);
for (int i = 0; i < tree[num].size(); i++)
{
int to = tree[num][i]; double value = dist(point[to], point[num]);
if (visit[to] == 1)continue;
for (int j = 0; j < stack.size(); j++)
{
map[to][stack[j]] = map[stack[j]][to] = max(value,map[num][stack[j]]);
}
dfs(to);
}
}
double kruscal()
{
double ans = 0;
sort(edge, edge + edgetot);
for (int i = 0; i < edgetot; i++)
{
int from = edge[i].from; int to = edge[i].to;
int x = find(from);
int y = find(to);
if (x == y)continue;
fa[x] = y;
ans += edge[i].value;
tree[from].push_back(to);
tree[to].push_back(from);
}
return ans;
}
double getans()
{
double all=kruscal();
double ans = -1000000000;
dfs(1);
for (int i = 0; i < edgetot; i++)
{
//if (i > (i ^ 1))continue;
int from = edge[i].from; int to = edge[i].to;
if (ans < (point[from].population + point[to].population) / (all - map[from][to]))
{
//cout << "from:" << from << " " << "to:" << to << endl;
// cout << "最小生成树:" << all << " " << "两点最长边:" << map[from][to] << endl;
//cout << "from:x" << point[from].x << " " << "from:y:" << point[from].y << " " << "to:x" << point[to].x << " " << "to:y" << point[to].y<< endl;
ans = max(ans,(point[from].population + point[to].population) / (all - map[from][to]));
//cout << "population:" << point[from].population + point[to].population << " " << "ans:" << ans << endl;
}
}
return ans;
}
int main()
{
int t;
scanf("%d", &t);
while (t--)
{
scanf("%d", &n);
//n = 1000;
init();
for (int i = 1; i <= n; i++)
{
tree[i].clear(),visit[i]=0;
for (int j = i; j <= n; j++)
{
map[i][j] = map[j][i] = -1;
}
}
edgetot = 0; stack.clear();
for (int i = 1; i <=n; i++)
{
double x, y; int p;
//x = rand() % 1000; y = rand() % 1000; p = rand() % 99999+1;
scanf("%lf%lf%d", &x, &y, &p);
point[i] = pointt(x, y, p);
}
for (int i = 1; i <= n; i++)
{
for (int j = i+1; j <= n; j++)
{
double value = dist(point[i], point[j]);
addedge(i, j, value);
}
}
double ans = getans();
//cout << "test:" << endl;
//cout << "from:" << point[1].x << " " << point[1].y << " " << "to:" << point[2].x << " " << point[2].y <<"map:"<<map[1][2]<< "population:"
//<<point[1].population+point[2].population<<endl;
printf("%.2lf\n", ans);
}
return 0;
}