poj 2031 Building a Space Station

最小生成树的题目,prim搞一搞就可以了。

/*
 * Author: stormdpzh
 * Time: 2012/5/10 11:09:57
 */
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <set>
#include <algorithm>
#include <functional>

#define sz(v) ((int)(v).size())
#define rep(i, n) for(int i = 0; i < n; i++)
#define repf(i, a, b) for(int i = a; i <= b; i++)
#define repd(i, a, b) for(int i = a; i >= b; i--)
#define out(n) printf("%d\n", n)
#define wh(n) while(scanf("%d", &n) != EOF)
#define whz(n) while(scanf("%d", &n) != EOF && n != 0)
#define lint long long

using namespace std;

const int M = 105;
const double MaxD = 1000000000.0;

struct Node {
    double x, y, z;
    double r;
} node[M];
double dis[M][M];
double low[M];
bool visited[M];
int n;
//bool flag;

double getDis(int i, int j)
{
    return sqrt((node[i].x - node[j].x) * (node[i].x - node[j].x) + 
                (node[i].y - node[j].y) * (node[i].y - node[j].y) + 
                (node[i].z - node[j].z) * (node[i].z - node[j].z));
}

double gao()
{
    memset(visited, false, sizeof(visited));
    visited[0] = true;
    double res = .0;
    rep(i, n)
        low[i] = dis[0][i];
    rep(i, n - 1) {
        double tmp = MaxD;
        int id;
        rep(j, n) {
            if(!visited[j] && tmp > low[j]) {
                id = j;
                tmp = low[j];
            }
        }
        res += tmp;
        visited[id] = true;
        rep(j, n) {
            if(dis[id][j] < low[j])
                low[j] = dis[id][j];
        }
    }
    return res;
}

int main()
{
    whz(n) {
        rep(i, n)
            scanf("%lf%lf%lf%lf", &node[i].x, &node[i].y, &node[i].z, &node[i].r);
        rep(i, n) rep(j, n) {
            dis[i][j] = dis[j][i] = getDis(i, j) - node[i].r - node[j].r;
            if(dis[i][j] < .0)
                dis[j][i] = dis[i][j] = .0;
        } 
        printf("%.3f\n", gao());
    }
    
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值