Building a Space Station——最小生成树

题目链接

题意:

给出n个球体的球心坐标和半径,可以在两个球体的表面连一条通路,代价为距离. 求使得所有球体联通的最小花费.

题解:

最小生成树裸板子

暴力把每个球体的表面之间的距离求出(即 dis=球心距 - 半径和

注意 如果 dis<0 则 dis=0

代码:

#include<iostream>
#include<stdio.h>
#include<math.h>
#include<algorithm>
#include<cstring>
#include<vector>
#define eps 1e-8
using namespace std;
typedef long long ll;
const int maxn = 1e6+5;
int f[maxn];
int cnt,n;
double x[maxn],y[maxn],z[maxn],r[maxn];
struct node
{
    int u,v;
    double w;
    bool operator < (const node &a)const
    {
        return w<a.w;
    }
}edge[maxn];
int sign(double x)
{
    if(fabs(x)<eps)return 0;
    return x<0?-1:1;
}
int Find(int x)
{
    return x==f[x]?x:f[x]=Find(f[x]);
}
double kruskal()
{
    double ans=0.0;
    for(int i=0; i<=n; i++)f[i]=i;
    sort(edge,edge+cnt);
    for(int i=0; i<cnt; i++)
    {
        int x=edge[i].u;
        int y=edge[i].v;
        int fx=Find(x);
        int fy=Find(y);
        if(fx!=fy)
        {
            f[fx]=fy;
            ans+=edge[i].w;
        }
    }
    return ans;

}
double get_dis(int i,int j)
{
    double dis=sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j])+(z[i]-z[j])*(z[i]-z[j]))-(r[i]+r[j]);
    if(dis>=0)return dis;
    return 0;
}
int main()
{
    while(~scanf("%d",&n) && n)
    {
        memset(edge,0,sizeof edge);
        for(int i=1; i<=n; i++)scanf("%lf%lf%lf%lf",&x[i],&y[i],&z[i],&r[i]);

        cnt=0;
        for(int i=1; i<=n; i++)
        {
            for(int j=i+1; j<=n; j++)
            {
                edge[cnt].u=i;
                edge[cnt].v=j;
                edge[cnt++].w=get_dis(i,j);
            }
        }
        printf("%.3f\n",kruskal());

    }
    return 0;
}

kruskal

原文地址:https://www.cnblogs.com/j666/p/11616886.html

时间: 2024-11-06 13:53:08

Building a Space Station——最小生成树的相关文章

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 - 2031C - Building a Space Station最小生成树

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with a number of units, called

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 【最小生成树 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【最小生成树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

poj2031(Building a Space Station)

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

[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