poj 2031 Building a Space Station(最小生成树)
2015-07-22 15:26
549 查看
#include<cstdio> #include<cstring> #include<cmath> #include<algorithm> using namespace std; struct d { int u,v; double w; } e[10000+5]; bool cmp(d a,d b) { return a.w<b.w; } int bin[100+5]; double x[100+5],y[100+5],z[100+5],r[100+5]; int Find(int x) { int s; for(s=x; bin[s]>=0; s=bin[s]); while(s!=x) { int t=bin[x]; bin[x]=s; x=t; } return s; } void Union(int x1,int x2) { int f1=Find(x1),f2=Find(x2); int t=bin[f1]+bin[f2]; if(bin[f1]>bin[f2]) { bin[f1]=f2; bin[f2]=t; } else { bin[f2]=f1; bin[f1]=t; } } int main() { int n,m,i,j,u,v; double ans; while(~scanf("%d",&n)&&n) { for(i=0; i<n; i++) bin[i]=-1; for(i=0; i<n; i++) scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]); for(i=0,m=0; i<n; i++) for(j=i+1; j<n; j++) { double t=(x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]); t=sqrt(t); if(t<=r[i]+r[j]) { if(Find(i)!=Find(j)) Union(i,j); } else { e[m].u=i; e[m].v=j; e[m].w=t-r[i]-r[j]; m++; } } sort(e,e+m,cmp); for(ans=0,i=0; i<m; i++) { u=e[i].u; v=e[i].v; if(Find(u)!=Find(v)) { ans+=e[i].w; Union(u,v); } } printf("%.3f\n",ans); } return 0; }
相关文章推荐
- 225 Implement Stack using Queues
- require.js 详解
- 优化原理之UITableView - 循环利用
- hdu 5228 OO’s Sequence 多校 思维题
- Exception in MessageQueue callback: handleReceiveCallback
- HDU5288 OO’s Sequence
- UIView - 常用方法总结
- ios开发UI篇—在ImageView中添加按钮以及Tag的参数说明
- 【多校第一场】【筛法改】HDU 5288 OO’s Sequence
- ios开发UI篇—Kvc简单介绍
- express4.x Request对象获得参数方法
- 关于是用UE的技巧--持续更新......
- Android build
- Bullet物理引擎不完全指南(Bullet Physics Engine not complete Guide)
- IOS自定义UITableViewCell的高亮背景色
- Storyboard中的UIScrollView使用自动布局,使其能够滚动
- ueditor 定制使用
- CALayer与UIView的关系
- Diesel-engined devices toronto and additionally equipment enjoy a great fanatic sticking with
- Errors occurred during the build. Errors running builder 'Checkstyle Builder' on project