Question:
Similar to the Minimum Spanning Tree, the Minimum Spanning Tree is used first, and then vertices are enumerated to delete edges and add edges.
[Cpp]
# Include <cstdio>
# Include <algorithm>
# Include <cstring>
# Include <iostream>
# Include <cmath>
Using namespace std;
Const int maxn = 1010*1010;
Struct NODE {
Int u, v;
Double w;
Bool operator <(const NODE & a) const
{
Return a. w> w;
}
} Node [maxn * 2], in [1005];
Struct TREE
{
Int u, next;
Double w;
} Tree [maxn * 5];
Int head [1010];
Bool vis [1010];
Int f [1005];
Double g [1, 1010] [2, 1010];
Int find (int x)
{
Return f [x] = x? X: f [x] = find (f [x]);
}
Int val [1005];
Void dfs (int root, int x)
{
Vis [x] = true;
For (int I = head [x]; I! =-1; I = tree [I]. next)
If (! Vis [tree [I]. u])
{
G [root] [tree [I]. u] = max (tree [I]. w, g [root] [x]);
Dfs (root, tree [I]. u );
}
}
Void kruskal (int n, int e)
{
Memset (head,-1, sizeof (head ));
Double sum = 0, ans = 0;
Int p = 0;
For (int I = 0; I <= n; I ++) f [I] = I;
Sort (node, node + e );
For (int I = 0; I <e; I ++)
{
Int x = find (node [I]. u );
Int y = find (node [I]. v );
If (x! = Y)
{
Sum + = node [I]. w;
F [y] = x;
// Tree
Tree [p]. u = node [I]. u; tree [p]. w = node [I]. w;
Tree [p]. next = head [node [I]. v]; head [node [I]. v] = p ++;
Tree [p]. u = node [I]. v; tree [p]. w = node [I]. w;
Tree [p]. next = head [node [I]. u]; head [node [I]. u] = p ++;
}
}
For (int I = 0; I <= n; I ++)
For (int j = 0; j <= n; j ++)
G [I] [j] = 0;
For (int I = 1; I <= n; I ++)
{
For (int j = 0; j <= n; j ++)
Vis [j] = false;
Dfs (I, I );
}
// G [I] [j]
For (int I = 1; I <= n; I ++)
For (int j = I + 1; j <= n; j ++)
Ans = max (ans, (val [I] + val [j])/(sum-g [I] [j]);
Printf ("%. 2f \ n", ans );
}
Int main ()
{
Int ca, n, num;
Int x [1005], y [1005];
Scanf ("% d", & ca );
While (ca --)
{
Scanf ("% d", & n );
Num = 0;
For (int I = 1; I <= n; I ++)
{
Scanf ("% d", & x [I], & y [I], & val [I]);
}
For (int I = 1; I <= n; I ++)
{
For (int j = I + 1; j <= n; j ++)
{
Node [num]. u = I;
Node [num]. v = j;
Node [num]. w = sqrt (x [I]-x [j]) * (x [I]-x [j]) + (y [I]-y [j]) * (y [I]-y [j]);
Num ++;
}
}
Kruskal (n, num );
}
Return 0;
}