依旧是求最小生成树,不过处理时注意一点;
把所有的点存进来然后按顺序两两求长度,满足题意
10≤l≤1000
的边存进edges数组里,然后Kruskal模版即可
对于无法连通的ans=0输出”oh!”即可
Accepted的时候我好懵逼啊
还以为有啥特殊的坑点……
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
const int N=110;
const int M=5000;
struct edge{
int u,v;
double w;
}edges[M];
struct point{
double x,y;
}p[N];
int pcmp(point x,point y){
if(x.x==y.x) return x.y<y.y;
else return x.x<y.x;
}
int pre[N];
int find(int i){
return pre[i]==i?i:pre[i]=find(pre[i]);
}
double cmp(edge a,edge b){
return a.w<b.w;
}
double kruskal(int n,int m){
double ans=0;
for(int i=1;i<=n;i++) pre[i]=i;
sort(edges+1,edges+m+1,cmp);
for(int i=1;i<=m;i++){
int x=find(edges[i].u);
int y=find(edges[i].v);
if(x!=y){
ans+=edges[i].w;
pre[x]=y;
}
}
return ans;
}
double sqr(double x){
return x*x;
}
double getLen(point x,point y){
return sqrt(sqr(x.x-y.x)+sqr(x.y-y.y));
}
int main(){
int t;
scanf("%d",&t);
while(t--){
int n;
scanf("%d",&n);
for(int i=1;i<=n;i++){
scanf("%lf %lf",&p[i].x,&p[i].y);
}
sort(p+1,p+n+1,pcmp);
int cnt=1;
// for(int i=1;i<=n;i++) cout<<p[i].x<<" "<<p[i].y<<endl;
for(int i=1;i<=n;i++){
for(int j=i+1;j<=n;j++){
double l=getLen(p[i],p[j]);
if(l>=10.0&&l<=1000.0){
edges[cnt].u=i;
edges[cnt].v=j;
edges[cnt].w=l;
// cout<<edges[cnt].u<<" "<<edges[cnt].v<<" "<<edges[cnt].w<<endl;
cnt++;
}
}
}
double ans=kruskal(n,cnt)*100.0;
if(ans<1e-8) puts("oh!");
else printf("%.1f\n",ans);
}
}
/*
2
2
10 10
20 20
3
1 1
2 2
1000 1000
*/