
1 #include<cstdio> 2 #include<cmath> 3 #include<algorithm> 4 #define ll long long 5 using namespace std; 6 struct ee{int x,y;double w;}e[1000000]; 7 int n,K,x[1110],y[1110],cnt,fa[2000]; 8 ll sqr(ll x) {return x*x;} 9 bool cmp(ee x,ee y){ 10 return x.w<y.w; 11 } 12 double calc(int i,int j){ 13 return sqrt(sqr(x[i]-x[j])+sqr(y[i]-y[j])); 14 } 15 16 int root(int x){ 17 if (fa[x]==x) return x; 18 fa[x]=root(fa[x]); 19 return fa[x]; 20 } 21 22 int main(){ 23 scanf("%d%d",&n,&K); 24 for(int i=1;i<=n;i++) fa[i]=i; 25 for(int i=1;i<=n;i++) scanf("%d%d",&x[i],&y[i]); 26 for(int i=1;i<=n;i++) 27 for(int j=i+1;j<=n;j++){ 28 e[++cnt].x=i,e[cnt].y=j,e[cnt].w=calc(i,j); 29 } 30 sort(e+1,e+cnt+1,cmp); 31 for(int i=1;i<=cnt;i++) { 32 int xx=root(e[i].x),yy=root(e[i].y); 33 if(xx!=yy){ 34 if(n>K){ 35 n--; 36 fa[xx]=yy; 37 } 38 else {printf("%.2lf",e[i].w);return 0;}}; 39 } 40 }
【BZOJ 1821】 [JSOI2010]Group 部落划分 Group
原文:http://www.cnblogs.com/wuminyan/p/5205018.html