1 条题解
-
0
C++ :
#include <iostream> #include <cmath> #include <cstdio> #include <algorithm> using namespace std; const int N = 35; double x[N], y[N], z[N]; int father[N]; struct node{ int u, v; double d; bool operator < (const struct node n) const { return d < n.d; } }e[500]; double DIS( int a, int b ) { return sqrt( (x[a]-x[b])*(x[a]-x[b])+(y[a]-y[b])*(y[a]-y[b])+(z[a]-z[b])*(z[a]-z[b]) ); } int find( int x ) { if( father[x] == x ) return father[x]; return ( father[x] = find( father[x] ) ); } int main() { int t, n, m, i, j, c; double ans; cin>>t; while(t--) { cin>>n; for( i = 0; i < n; ++i ) father[i] = i, cin>>x[i]>>y[i]>>z[i]; for( ans = c = m = i = 0; i < n; ++i ) for( j = i + 1; j < n; ++j ) e[m].u = i, e[m].v = j, e[m++].d = DIS(i,j); sort( e, e + m ); for( i = 0; i < m; ++i ) { //cout<<e[i].d<<endl; int u = find(e[i].u); int v = find(e[i].v); if( u != v ) { ans += e[i].d; c++; father[u] = v; } if( c == n - 1 ) break; } printf("%.2lf\n", ans); } return 0; }
- 1
信息
- ID
- 1504
- 时间
- 1000ms
- 内存
- 32MiB
- 难度
- (无)
- 标签
- 递交数
- 0
- 已通过
- 0
- 上传者