本文共 1519 字,大约阅读时间需要 5 分钟。
三维空间,给n个圆心x,y,z,半径r的圆,求最短的连线。
接触不需要连。
求距离,接触权值为0,不接触为权值长度减半径。
#include #include #include #include #include #include #include #include #include #include #include using namespace std;typedef long long LL;const int MAXM = 10000+10;const int MAXN = 100+10;struct Node{ double _x,_y,_z,_r;}node[MAXN];struct Path{ int _l,_r; double _value; bool operator < (const Path & that)const{ return this->_value < that._value; }}path[MAXM];double Get_Len(Node a,Node b){ return sqrt((a._x-b._x)*(a._x-b._x) + (a._y-b._y)*(a._y-b._y) + (a._z-b._z)*(a._z-b._z));}int Father[MAXN];int Get_F(int x){ return Father[x] = (Father[x] == x) ? x : Get_F(Father[x]);}int main(){ int n; while (cin >> n && n) { for (int i = 1;i<=n;i++) Father[i] = i; for (int i = 1;i<=n;i++) cin >> node[i]._x >> node[i]._y >> node[i]._z >> node[i]._r; int pos = 0; for (int i = 1;i<=n;i++) { for (int j = 1;j<=n;j++) { double len = Get_Len(node[i],node[j]); double r = node[i]._r + node[j]._r; path[++pos]._l = i; path[pos]._r = j; if (len <= r) path[pos]._value = 0; else path[pos]._value = len - r; } } sort(path+1,path+1+pos); double sum = 0; for (int i = 1;i <= pos;i++) { int tl = Get_F(path[i]._l); int tr = Get_F(path[i]._r); if (tl != tr) { Father[tl] = tr; sum += path[i]._value; } } printf("%.3lf\n",sum); } return 0;}
转载于:https://www.cnblogs.com/YDDDD/p/10330669.html