ProgramAlgTrain/20240917/P1991/P1991.cpp

93 lines
1.8 KiB
C++
Raw Normal View History

2024-09-17 12:04:21 +00:00
#include <algorithm>
#include <cmath>
#include <iomanip>
#include <ios>
#include <iostream>
#include <istream>
const int MAX_P = 500+5;
struct Dir{
int x,y;
};
struct Edge{
int s,e;
double dis;
bool operator<(const Edge that)const {
return this->dis < that.dis;
}
}edges[MAX_P*MAX_P];
int s,p;
Dir dirs[MAX_P];
decltype(Edge::dis) square(const decltype(Edge::dis) &n){
return n*n;
}
int check_sets[MAX_P];
int find_set(const int n){
if(check_sets[n]==n){
return n;
}else{
check_sets[n] = find_set(check_sets[n]);
}
return check_sets[n];
}
void merge_set(const int a,const int b){
check_sets[find_set(a)] = check_sets[find_set(b)];
}
int main(){
std::cin.sync_with_stdio(false),std::cin.tie(0),std::cout.tie(0);
std::cin>>s>>p;
for(int i=1;i<=p;i++){
check_sets[i]=i;
}
for(int i=1;i<=p;i++){
std::cin>>dirs[i].x>>dirs[i].y;
}
int flag = -1;
for(int i=1;i<=p;i++){
for(int j=i+1;j<=p;j++){
flag++;
edges[flag].e=i,edges[flag].s=j,edges[flag].dis=std::sqrt(square(dirs[i].x-dirs[j].x)+square(dirs[i].y-dirs[j].y));
}
}
std::sort(edges,edges+flag+1);
decltype(Edge::dis) ans{0};
// #define NV(v)std::cout<<#v<<" : "<<(v)<<"\n";
#define NV(v)
int cnt{0};
for(int i=0;i<flag;i++){
NV(find_set(edges[i].e));
NV(find_set(edges[i].s));
if(find_set(edges[i].e)!=find_set(edges[i].s)){
NV(edges[i].dis)
merge_set(edges[i].e,edges[i].s);
// std::cout<<"merge "<<edges[i].e<<" "<<edges[i].s<<"\n";
ans=edges[i].dis;
cnt++;
if(cnt>=p-s){
std::cout<<std::fixed<<std::setprecision(2)<<ans<<"\n";
return 0;
}
}
}
}