# [POJ 2349] Arctic Network Kruskal

#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MX = 1005;

struct Edge{
int u,v;
double len;
};
Edge edge[MX*MX];
int s,p,te = 0,cnt = 0;                             //te:总边数
int locat[MX][2],fa[MX];
double map[MX][MX],res[MX];

bool comp(const Edge& i,const Edge &j){
return i.len < j.len;
}
double cdis(int a1,int a2,int b1,int b2){           //得到两个点之间的距离
double x = ((b1-a1) * (b1-a1) + (b2-a2)*(b2-a2));
return sqrt(x);
}
int find(int x){return fa[x] == x ? x : fa[x] = find(fa[x]);}
void kruskal(){
int k = 0;
for (int i = 1;i <= p;i++) fa[i] = i;
sort(edge + 1,edge + te + 1,comp);
for (int i = 1;i <= te;i++){
int x = find(edge[i].u), y = find(edge[i].v);
if (k >= p-1) break;
if (x != y){
res[cnt++] = edge[i].len;
k++;
fa[x] = y;
}
}
}
int main(){
int T;
cin>>T;
while (T--){
memset(locat,0,sizeof(locat));
memset(map,0,sizeof(map));
memset(edge,0,sizeof(edge));
te = 0, cnt = 0;
cin>>s>>p;
for (int i = 1;i <= p;i++){
cin>>locat[i][0]>>locat[i][1];
}
for (int i = 1;i <= p;i++){
for (int j = 1;j <= p;j++){
map[i][j] = cdis(locat[i][0],locat[i][1],locat[j][0],locat[j][1]);
if (i <= j){
edge[++te].u = i;
edge[te].v = j;
edge[te].len = map[i][j];
}
}
}
kruskal();
printf("%.2lf\n",res[cnt - s]);
}
return 0;
}

©️2019 CSDN 皮肤主题: 编程工作室 设计师: CSDN官方博客