POJ 2031 (最小生成树)
来源:互联网 发布:软件需求分为哪几类 编辑:程序博客网 时间:2024/06/06 04:19
题意是n个球求MST,球相交或者相切距离认为是0.
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>using namespace std;#define maxn 111struct 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 Findint 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;}
0 0
- POJ 2031 (最小生成树)
- poj 2031 最小生成树(prim)
- POJ 2031 最小生成树Kruskal算法
- Poj-2031 最小生成树+几何
- POJ 2031 (最小生成树)
- poj 最小生成树1258
- POJ 1258 最小生成树
- 最小生成树 poj 2485
- POJ-1258 最小生成树
- poj 1751 最小生成树
- poj 2421(最小生成树)
- poj 1789(最小生成树)
- poj 2349(最小生成树)
- poj 1861 最小生成树
- POJ 1679 最小生成树
- poj 1861 最小生成树
- poj 1251 最小生成树
- poj 1258 最小生成树
- poj1741 Tree 点分治
- unlink()
- 全功能智能车之CCD初步实现并发送数据到上位机(第十二篇)
- edX的一些管理命令
- 题解:HDU 1240: Asteroids! (DFS 或 BFS)
- POJ 2031 (最小生成树)
- Video对象的2个状态属性readyState和networkState属性的介绍
- android---异步通讯AsyncTask
- 搭建Open edX经验总结
- TextView限制最大行数,多出部分显示省略号,解决ellipsize = "end" 失效的问题
- nyoj 127 星际之门(一)【最小生成树】
- MATLAB 机器人逆解 ikine
- if语句优化
- 本地调试微信接口方法