题目思路
使用 Kruskal 求最小瓶颈生成树,很轻松可以求出 ,在通过总存在的边和减去选用的边和即可。
Solution 函数
变量名就自行看了。
void solution(){
cin>>n;
for(int i=1;i<=n;i++)cin>>x[i]>>y[i],fa[i]=i;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
tot++;
p[tot]={i,j,abs(x[i]-x[j])+abs(y[i]-y[j])};
}
}
sort(p+1,p+1+tot,cmp);
int bs=0,maxn=-1;
for(int i=1;i<=tot&&bs<=n-1;i++){
int u=p[i].u,v=p[i].v,w=p[i].w;
int fu=get(u),fv=get(v);
if(fu!=fv){
bs++;
maxn=max(maxn,w);
fa[fu]=fv;
cho[i]=1;
}
}cout<<maxn<<" ";
int cnt=0;
for(int i=1;i<=tot;i++){
if(p[i].w<=maxn&&!cho[i])cnt+=p[i].w;
}cout<<cnt;
}