您的位置:首页 > 产品设计 > UI/UE

POJ 2031 Building a Space Station

2015-05-08 23:19 363 查看
题意:在三维空间中,有若干个球体的坐标和半径已知,求最短需要多少长度的木棍将所有的求连接起来

链接:http://poj.org/problem?id=2031

思路:最小生成树,建图的时候注意边权的计算,xy两点之间的距离为disxy-rx-ry,若这个值小于0,则这两个点之间的距离为0,否则两点之间的距离为求得的值

注意点:无

以下为AC代码:

Run IDUserProblemResultMemoryTimeLanguageCode LengthSubmit Time
14176350luminous112031Accepted804K0MSG++3771B2015-05-08 23:26:27
#include <algorithm>
#include <iostream>
#include <climits>
#include <iomanip>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cstdio>
#include <string>
#include <vector>
#include <queue>
#include <stack>
#include <cmath>
#include <ctime>
#include <deque>
#include <list>
#include <map>
#include <set>
//#include <unordered_map>
//#include <unordered_set>
#define pb push_back
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define RS(s) scanf ( "%s", s )
#define RDI(a) scanf ( "%d", &a )
#define RDII(a, b) scanf ( "%d%d", &a, &b )
#define RDIII(a, b, c) scanf ( "%d%d%d", &a, &b, &c )
#define PL() printf ( "\n" )
#define PI(a) printf ( "%d", a )
#define PIL(a) printf ( "%d\n", a )
#define PSL(s) printf ( "%s\n", s )
#define PII(a,b) printf ( "%d %d", a, b )
#define PIIL(a,b) printf ( "%d %d\n", a, b )
#define PIII(a,b,c) printf ( "%d %d %d", a, b, c )
#define PIIIL(a,b,c) printf ( "%d %d %d\n", a, b, c )
#define rep(i,m,n) for ( int i = m; i <  n; i ++ )
#define REP(i,m,n) for ( int i = m; i <= n; i ++ )
#define dep(i,m,n) for ( int i = m; i >  n; i -- )
#define DEP(i,m,n) for ( int i = m; i >= n; i -- )
#define repi(i,m,n,k) for ( int i = m; i <  n; i += k )
#define REPI(i,m,n,k) for ( int i = m; i <= n; i += k )
#define depi(i,m,n,k) for ( int i = m; i >  n; i += k )
#define DEPI(i,m,n,k) for ( int i = m; i >= n; i -= k )
#define READ(f) freopen(f, "r", stdin)
#define WRITE(f) freopen(f, "w", stdout)
//#pragma comment ( linker, "/STACK:1024000000,1024000000" )
using namespace std;
const double pi = acos(-1.0);
template <class T>
inline bool RD ( T &ret )
{
char c;
int sgn;
if ( c = getchar(), c ==EOF )return 0; //EOF
while ( c != '-' && ( c < '0' || c > '9' ) ) c = getchar();
sgn = ( c == '-' ) ? -1 : 1;
ret = ( c == '-' ) ? 0 : ( c - '0' );
while ( c = getchar() , c >= '0' && c <= '9' ) ret = ret * 10 + ( c - '0' );
ret *= sgn;
return 1;
}
inline void PD ( int x )
{
if ( x > 9 ) PD ( x / 10 );
putchar ( x % 10 + '0' );
}
const double eps = 1e-10;
const int dir[4][2] = { 1,0, -1,0, 0,1, 0,-1 };
struct node{
double x, y, z, r;
node(){}
node ( double _x, double _y, double _z, double _r ) : x(_x), y(_y), z(_z), r(_r) {}
};
struct edge{
int l, r;
double wei;
edge(){}
edge( int _l, int _r, double _wei ) : l(_l), r(_r), wei(_wei) {}
};
int n;
int fa[205];
node p[205];
edge num[20005];
bool cmp ( const edge &a, const edge &b )
{
return a.wei < b.wei;
}
edge calc ( int u, int v )
{
double xi = p[u].x - p[v].x;
double yi = p[u].y - p[v].y;
double zi = p[u].z - p[v].z;
double dis = sqrt ( xi*xi+yi*yi+zi*zi ) - p[u].r - p[v].r;
if ( dis < 0.0 )dis = 0.0;
return edge ( u, v, dis );
}
int find ( int x )
{
if ( x == fa[x] ){
return x;
}
else{
fa[x] = find ( fa[x] );
return fa[x];
}
}
int main()
{
while ( RDI ( n ) && n ){
rep ( i, 0, 205 ) fa[i] = i;
REP ( i, 1, n ){
scanf ( "%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r );
}
int k = 0;
REP ( i, 1, n ){
REP ( j, i + 1, n ){
num[k++] = calc ( i, j );
}
}
sort ( num, num + k, cmp );
double ans = 0.0;
rep ( i, 0, k ){
int xi = find ( num[i].l );
int yi = find ( num[i].r );
if ( xi != yi ){
fa[xi] = yi;
ans += num[i].wei;
}
}
printf ( "%.3f\n", ans );
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: