题目链接
求最小生成树的最大边。
图是完全图,在稠密图中prim算法更优
#include<bits/stdc++.h>
#define FAST ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
typedef long long ll;
const int maxn = 1e5+5;
const ll INF = 1LL<<60;
using namespace std;
int n,m,tot;
ll dis[5005],mp[5001][5001];
int vis[5005];
struct P
{
ll x,y;
}p[5005];
ll cal(int i, int j){
return (p[i].x-p[j].x)*(p[i].x-p[j].x)+(p[i].y-p[j].y)*(p[i].y-p[j].y);
}
ll prime()
{
int now;
ll ans;
for (int i=1; i<=n; i++) dis[i]=INF, vis[i]=0;
dis[1]=0;
tot=0, now=1, ans=-1;
for (int t=1; t<=n; t++)
{
ll minn=INF;
for (int i=1; i<=n; i++){
if (!vis[i] && dis[i]<minn){
minn=dis[i];
now=i;
}
}
vis[now]=1;
ans=max(ans,dis[now]);
for (int j=1; j<=n; j++)
if (!vis[j] && mp[now][j]<dis[j]) dis[j]=mp[now][j];
}
return ans;
}
void solve()
{
cin>>n;
for (int i=1; i<=n; i++){
for (int j=1; j<=n; j++)
mp[i][j]=0;
}
for (int i=1; i<=n; i++)
cin>>p[i].x>>p[i].y;
for (int i=1; i<=n; i++){
for (int j=i+1; j<=n; j++)
mp[i][j]=mp[j][i]=cal(i,j);
}
ll res=prime();
cout<<res<<endl;
}
int main()
{
FAST;
// freopen("1.in","r",stdin);
int kase;
cin>>kase;
while(kase--)
{
solve();
}
return 0;
}