poj2031 Building a Space Station 三维空间的最小生成树

题目链接:

2031

http://write.blog.csdn.net/postedit?ref=toolbar

题意:

在三维空间中有N个球形空间站,给出每个空间站的三维坐标x,y,z 和半径.其中空间站之间可能存在相交,包含,相离等情况。如果非相离则两空间站的距离为0。求连同所有空间站的最小生成树。

题解:

三维构图两空间站的距离等于两圆心距离减去两圆的半径 如果距离小于0,则距离为0

在套kruskal模板即可

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
struct node
{
    int u,v;
    double w;
} edge[10005];
double map[105][4],ans;
int fa[105];
int m,s;
int cmp(node a,node b)
{
    return a.w<b.w;
}
int find(int x)
{
    int d=x,t;
    while(fa[d]>=0)
        d=fa[d];
    while(x!=d)       //压缩路径
    {
        t=fa[x];
        fa[x]=d;
        x=t;
    }
    return d;
}
void Kruskal()
{
    int r1,i,j,r2,n=0,ss;
    for(i=0; i<s; i++)
    {
        r1=find(edge[i].u),r2=find(edge[i].v);
        if(r1!=r2)
        {
            n++;
            ss=fa[r1]+fa[r2];
            ans+=edge[i].w;
            if(fa[r1]<fa[r2])
                fa[r1]=ss,fa[r2]=r1;
            else
                fa[r1]=r2,fa[r2]=ss;
        }
        if(n==m-1)
            break;
    }
    return;
}
int main()
{
    int i,j;
    double weight;
    while(scanf("%d",&m)&&m)
    {
        ans=s=0;
        memset(fa,-1,sizeof(fa));
        for(i=1; i<=m; i++)
        {
            scanf("%lf%lf%lf%lf",&map[i][0],&map[i][1],&map[i][2],&map[i][3]);
            for(j=1; j<i; j++)
            {
                edge[s].u=j,edge[s].v=i;
                weight=sqrt(pow(map[i][0]-map[j][0],2)+pow(map[i][1]-map[j][1],2)+pow(map[i][2]-map[j][2],2));
                if(weight>map[i][3]+map[j][3])              //距离
                    edge[s++].w=weight-map[i][3]-map[j][3];
                else
                    edge[s++].w=0;
            }
        }
        sort(edge,edge+s,cmp);
        Kruskal();
        printf("%.3lf\n",ans);
    }
    return 0;
}
时间: 2024-10-05 05:13:01

poj2031 Building a Space Station 三维空间的最小生成树的相关文章

poj2031(Building a Space Station)

题目地址:Building a Space Station 题目大意: 在一个三维的空间里有若干个球体,求从走过所有球花费最少,题目要求两个球体接触就不需要建桥意为花费为0,实力给吃的n行, 其中包括 球的X,Y,Z的坐标,r为球的半径.这里就清晰了,意思就是求无向图最小生成树,val就为两个求圆心的距离减去两个球的半径. 解题思路: 建图,dijstra求最小生成树,记住双向的和eps处理精度. 代码: 1 #include <algorithm> 2 #include <iostre

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

POJ - 2031 Building a Space Station(计算几何+最小生成树)

http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边权为它们球心的距离-两者半径之和.这样来跑Prim就ok了.注意精度. #include<iostream> #include<cmath> #include<cstring> #include<queue> #include<vector> #in

poj 2031 Building a Space Station 【最小生成树 Prim】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5778   Accepted: 2874 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

POJ 2031 Building a Space Station (最小生成树)

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepted: 2614 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

poj 2031 Building a Space Station【最小生成树prime】【模板题】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

[2016-04-14][POJ][203][Building a Space Station]

时间:2016-04-14 21:43:30 星期四 题目编号:[2016-04-14][POJ][203][Building a Space Station] 题目大意:给定n个球体,每个球体可能重合,可能包含,可能分离,问把每个球体连接起来(重合和包含看做已经连接),至少需要多长的路 分析:最小生成树,边权为 max(0,disij?ri?rj)max(0,disij?ri?rj),即重合和内含,边权为0 #include<cstdio> #include<cstring> #

poj 2931 Building a Space Station &amp;lt;克鲁斯卡尔&amp;gt;

Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2910 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are ex

POJ 2031 Building a Space Station

Building a Space Station Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 2031 64-bit integer IO format: %lld      Java class name: Main You are a member of the space station engineering team, and are assigned