C - Building a Space Station - poj 2031

來源:互聯網
上載者:User

標籤:

空間站是有一些球狀的房間組成的,現在有一些房間但是沒有相互串連,你需要設計一些走廊使他們都相通,當然,有些房間可能會有重合(很神奇的樣子,重合距離是0),你需要設計出來最短的走廊使所有的點都串連。分析:因為給的都是點的座標,所以構圖的時候會有一些麻煩,不過也僅此而已。。。*******************************************************************

 

#include<iostream>
#include<cstring>
#include<cstdio>
#include<queue>
#include<math.h>
#include<vector>
using namespace std;

#define maxn 105

struct point{double x, y, z, r;}p[maxn];
struct node
{
    int u, v;
    double len;

    friend bool operator < (node a, node b)
    {
        return a.len > b.len;
    }
};

int f[maxn];

double Len(point a, point b)//求兩點間的距離
{
    double x = a.x - b.x;
    double y = a.y - b.y;
    double z = a.z - b.z;
    double l = sqrt(x*x+y*y+z*z)-a.r-b.r;

    if(l < 0)l = 0;

    return l;
}
int Find(int x)
{
    if(f[x] != x)
        f[x] = Find(f[x]);
    return f[x];
}

int main()
{
    int N;

    while(scanf("%d", &N) != EOF && N)
    {
        int i, j;
        node s;
        priority_queue<node>Q;

        for(i=1; i<=N; i++)
        {
            scanf("%lf%lf%lf%lf", &p[i].x, &p[i].y, &p[i].z, &p[i].r);
            f[i] = i;
        }

        for(i=1; i<=N; i++)
        for(j=i+1; j<=N; j++)
        {
            s.u = i, s.v = j;
            s.len = Len(p[i], p[j]);
            Q.push(s);
        }

        double ans = 0;

        while(Q.size())
        {
            s = Q.top();Q.pop();
            int u = Find(s.u), v = Find(s.v);

            if(u != v)
            {
                f[u] = v;
                ans += s.len;
            }
        }

        printf("%.3f\n", ans);
    }

    return 0;
}

 

C - Building a Space Station - poj 2031

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.