标签:
#include<iostream> #include<cstdio> #include<cstdlib> #include<cmath> #include<cstring> using namespace std; int n,k,top,x,y,ans=0,a[1001][3],f[1001]; double dis; struct node { int x,y; double d; }; node edge[500005]; double calc(int x1,int y1,int x2,int y2) { return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); } void insert(int xx,int yy,double dd) { edge[++top].x=xx; edge[top].y=yy; edge[top].d=dd; } bool cmp(node xx,node yy) { return xx.d<yy.d; } int find(int x) { return x==f[x]?x:find(f[x]); } int main() { scanf("%d%d",&n,&k); for (int i=1;i<=n;i++) scanf("%d%d",&a[i][1],&a[i][2]); top=0; for (int i=1;i<=n;i++) for (int j=i+1;j<=n;j++) { dis=calc(a[i][1],a[i][2],a[j][1],a[j][2]); insert(i,j,dis); } sort(edge+1,edge+top+1,cmp); for (int i=1;i<=n;i++) f[i]=i; for (int i=1;i<=top;i++) { int p=find(edge[i].x); int q=find(edge[i].y); if (p!=q) { f[p]=q; ans++; } if (ans==n-k) for (int j=i+1;j<=top;j++) { int p=find(edge[j].x),q=find(edge[j].y); if (p!=q) printf("%.2lf",edge[j].d); break; } } return 0; }
[JSOI2010][BZOJ1821]Group 部落划分 Group
标签:
原文地址:http://www.cnblogs.com/ws-fqk/p/4574639.html