标签:
kruskal算法。
#include<cstdio> #include<algorithm> #include<cstring> #include<cmath> using namespace std; const int maxn = 1000 + 10; const int maxm = 2000000 + 10; struct Point { int x,y; } a[maxn]; struct Edge { int u,v,w; } g[maxm]; int n,k,res,eid; int f[maxn]; int find(int x) { return f[x]==x?x:f[x]=find(f[x]); } int sqr(int x) { return x*x; } int foo(Point a,Point b) { return sqr(a.x-b.x)+sqr(a.y-b.y); } void addedge(int a,int b,int c) { ++eid; g[eid].u=a; g[eid].v=b; g[eid].w=c; } bool cmp(Edge a,Edge b) { return a.w<b.w; } int main() { scanf("%d%d",&n,&k); for(int i=1;i<=n;i++) scanf("%d%d",&a[i].x,&a[i].y); for(int i=1;i<=n;i++) for(int j=i+1;j<=n;j++) addedge(i,j,foo(a[i],a[j])); sort(g+1,g+eid+1,cmp); for(int i=1;i<=n;i++) f[i]=i; for(res=1;res<=eid;res++) { int ru=find(g[res].u),rv=find(g[res].v); if(ru!=rv) { if(n>k) { f[ru]=rv; n--; } else break; } } printf("%.2lf\n",sqrt(g[res].w)); return 0; }
bzoj1821: [JSOI2010]Group 部落划分 Group
标签:
原文地址:http://www.cnblogs.com/invoid/p/5497553.html