POJ 2031 (最小生成树)
2015-11-28 11:38
459 查看
题意是n个球求MST,球相交或者相切距离认为是0.
#include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #include <iostream> using namespace std; #define maxn 111 struct yuan { double x, y, z, r; }p[maxn]; struct node { int u, v; double w; bool operator < (const node &a) const { return w < a.w; } } edge[maxn*maxn]; int n, m; double dis (yuan a, yuan b) { double xx = a.x-b.x, yy = a.y-b.y, zz = a.z-b.z; double d = sqrt (xx*xx + yy*yy + zz*zz); if (d <= a.r+b.r) return 0; return d - a.r - b.r; } #define find Find int fa[maxn]; int find (int x) { return fa[x] == x ? fa[x] : fa[x] = find (fa[x]); } int main () { //freopen ("in", "r", stdin); while (cin >> n && n) { m = 0; for (int i = 1; i <= n; i++) fa[i] = i; for (int i = 1; i <= n; i++) { cin >> p[i].x >> p[i].y >> p[i].z >> p[i].r; for (int j = 1; j < i; j++) { edge[m].u = i, edge[m].v = j, edge[m++].w = dis (p[i], p[j]); } } sort (edge, edge+m); double ans = 0.0; int cnt = 0; for (int i = 0; i < m; i++) { int p1 = find (edge[i].u), p2 = find (edge[i].v); if (p1 != p2) { fa[p1] = p2; ans += edge[i].w; cnt++; } if (cnt == n-1) break; } printf ("%.3f\n", ans); } return 0; }
相关文章推荐
- unlink()
- poj1741 Tree 点分治
- 全功能智能车之CCD初步实现并发送数据到上位机(第十二篇)
- JS基础——选项卡套选项卡(函数传参)
- YUM仓库搭建
- 20151128-02 使用VideoView播放视频时无视频画面
- linux下查看cpu、内存和硬盘大小
- iOS 开发技巧收集整理
- Android四大组件之——Activity的生命周期(图文详解)
- IOS开发技术之合理利用类的类别
- Linux下定时执行python脚本
- Paxos
- 应试教育的死穴
- Aix换硬盘
- string奇怪引用计数器
- 第二十七节 MT-iBeacon基站使用iPhone空中升级
- 新入职项目经理务必避免的七个常见错误
- nyoj 170 网络的可靠性【最小生成树】
- 后缀自动机 初学
- bzoj2049 洞穴勘测 lct