标签:还需 pre vector https cst str amp return 通过
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<string>
#include<vector>
#include<cmath>
using namespace std;
const int maxn=505;
const int maxm=300000;
const double INF=0X3F3F3F3F;
int s,n;
int e;
struct node{
int from;
int to;
double cost;
bool operator<(const node& t)const{
return cost<t.cost;
}
};
node edge[maxm];
int x[maxn];
int y[maxn];
int set[maxn];
int find(int x){
return set[x]==x?set[x]:set[x]=find(set[x]);
}
double kruskal(int sn){
sort(edge,edge+e);
for(int i=1;i<=n;i++){
set[i]=i;
}
double maxs=-INF;
int ans=0;
for(int i=0;i<e;i++){
int from=edge[i].from;
int to=edge[i].to;
double cost=edge[i].cost;
int x=find(from);
int y=find(to);
if(x!=y){
set[x]=y;
maxs=max(cost,maxs);
ans++;
}
if(ans==sn-1){
break;
}
}
if(ans<sn-1){
return -1;
}else{
return maxs;
}
}
int main(){
int t;
scanf("%d",&t);
while(t--){
e=0;
scanf("%d%d",&s,&n);
for(int i=1;i<=n;i++){
scanf("%d%d",&x[i],&y[i]);
}
for(int i=1;i<=n;i++){
for(int j=1;j<i;j++){
double cost=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
edge[e++]=node{i,j,cost};
}
}
printf("%.2f\n",kruskal(n-s+1));
}
return 0;
}
POJ-2349(kruskal算法+最小生成树中最大边的长度)
标签:还需 pre vector https cst str amp return 通过
原文地址:https://www.cnblogs.com/GarrettWale/p/11474031.html