poj2031 Building a Space Station
来源:互联网 发布:淘宝发票规则 编辑:程序博客网 时间:2024/06/05 12:02
题目链接:http://poj.org/problem?id=2031
题意:给你n个球,让你用最小的花费,把这n个球联通,如果两个球相交,就默认联通,否者连接这两个球的花费就是,球面之间的最短距离,输出最小花费
解析:最小生成树裸题
#include <iostream>#include <algorithm>#include <cstdio>#include <cstring>#include <cmath>using namespace std;const int maxn = 1e5+100;struct node{ int u,v; double c; node() {} node(int _u,int _v,double _c) { u = _u; v = _v; c = _c; } bool operator < (const node &b)const { return c<b.c; }}res[maxn];struct point{ double x,y,z,r;}a[105];int fa[105];double dis(point p1,point p2){ return sqrt((p1.x-p2.x)*(p1.x-p2.x)+ (p1.y-p2.y)*(p1.y-p2.y)+ (p1.z-p2.z)*(p1.z-p2.z));}int getfa(int x){ if(fa[x]==x) return fa[x]; return fa[x] = getfa(fa[x]);}int main(void){ int n; while(~scanf("%d",&n)&&n) { for(int i=1;i<=n;i++) { scanf("%lf %lf %lf %lf",&a[i].x,&a[i].y,&a[i].z,&a[i].r); fa[i] = i; } int cnt = 0; for(int i=1;i<=n;i++) { for(int j=i+1;j<=n;j++) { double d = dis(a[i],a[j])-a[i].r-a[j].r; res[cnt++] = node(i,j,max(d,0.0)); } } double ans = 0; sort(res,res+cnt); for(int i=0;i<cnt;i++) { int t1 = getfa(res[i].u); int t2 = getfa(res[i].v); if(t1!=t2) { ans += res[i].c; fa[t1] = t2; } } printf("%.3f\n",ans); } return 0;}
阅读全文
0 0
- poj2031 - Building a Space Station
- poj2031--Building a Space Station
- POJ2031---Building a Space Station
- POJ2031 Building a Space Station
- poj2031 Building a Space Station
- POJ2031 Building a Space Station
- POJ2031 Building a Space Station
- poj2031 Building a Space Station
- POJ2031--Building a Space Station--最小生成树
- <模板>poj2031 Building a Space Station(最小生成树)
- poj2031 Building a Space Station 最小生成树,kruskal
- POJ2031-Building a Space Station -最小生成树
- POJ2031 Building a Space Station(最小生成树,prim)
- poj2031 Building a Space Station 三维空间的最小生成树
- POJ2031,Building a Space Station,最小生成树模版题
- poj2031 Building a Space Station(最小生成树)
- poj2031——Building a Space Station(prim)
- POJ2031 Building a Space Station(最小生成树,Prim)
- Java 中带参带返回值方法的使用
- 芬伯格主张计算机设计重构计算机技术
- codevs 2059逃出克隆岛
- 【理解HotSpot虚拟机】串行垃圾收集器Serial和Serial Old原理
- poj 2007 Scrambled Polygon(凸包Graham扫描线模板)
- poj2031 Building a Space Station
- 题目491:幸运三角形
- 你是怎样搭建.net架构
- oracle函数——通用函数
- SPRINGMVC注解之@RESPONSEBODY和@REQUESTBODY详解
- Java中方法的重载
- Java学习笔记 11 instanceof 、equal 和 == 的联系和区别
- Servlet学习笔记—Servlet接口和HttpServlet类
- 等考上机高分技巧:计算机等考上机六大注意事项