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

链接:

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

代码:

#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <iostream>
#include <algorithm>
using namespace std;

const int N = 110;
const int INF = 0xfffffff;

struct node
{
    double x, y, z, r;
}s[N];

int n;
double J[N][N], dist[N];
bool vis[N];

double Prim()
{
    int i, j, index;
    double ans=0;

    memset(vis, 0, sizeof(vis));
    vis[1]=1;

    for(i=1; i<=n; i++)
        dist[i]=J[1][i];

    for(i=1; i<n; i++)
    {
        double MIN=INF;
        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]<MIN)
            {
                index=j;
                MIN=dist[j];
            }
        }
        vis[index]=1;
        if(MIN<INF)
        ans += MIN;

        for(j=1; j<=n; j++)
        {
            if(!vis[j] && dist[j]>J[index][j] )
                dist[j]=J[index][j];
        }
    }
    return ans;
}

int main ()
{

    while(scanf("%d", &n),n)
    {
        int i, j;

        memset(s, 0, sizeof(s));
        memset(J, 0, sizeof(J));
        for(i=1; i<=n; i++)
            scanf("%lf%lf%lf%lf", &s[i].x, &s[i].y, &s[i].z, &s[i].r);

        for(i=1; i<=n; i++)
        for(j=1; j<i; j++)
        {
            double k=sqrt((s[i].x-s[j].x)*(s[i].x-s[j].x)+(s[i].y-s[j].y)*(s[i].y-s[j].y)+(s[i].z-s[j].z)*(s[i].z-s[j].z))-s[i].r-s[j].r;
            if(k<0)
                J[i][j]=J[j][i]=0;
            else
                J[i][j]=J[j][i]=k;
        }

        double ans=Prim();

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

    }
    return 0;
}
时间: 2025-01-02 00:31:45

(最小生成树) Building a Space Station -- POJ -- 2031的相关文章

Building a Space Station POJ 2031 【最小生成树 prim】

http://poj.org/problem?id=2031 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 expected to write a computer program to complete the task. The space statio

Building a Space Station POJ - 2031 三维最小生成树,其实就是板子题

#include<iostream> #include<cmath> #include<algorithm> #include<cstdio> using namespace std; const int N=1e5; struct edge{ int a,b; double w; }e[N]; double x[N],y[N],z[N],r[N]; int n,tot,p[N]; bool cmp(edge a,edge b) { return a.w&l

Building a Space Station POJ 2031

题目链接: http://poj.org/problem?id=2031 题意:现给定一些细胞的坐标以及它们的半径,求它们彼此联通的最短路径是多少.实则是最小生成树. ////特别心塞,G++提交就错,C++提交就A,害我找错好半天... #include<stdio.h> #include<string.h> #include<stdlib.h> #include<math.h> #include<algorithm> #include<

C - Building a Space Station - poj 2031

空间站是有一些球状的房间组成的,现在有一些房间但是没有相互连接,你需要设计一些走廊使他们都相通,当然,有些房间可能会有重合(很神奇的样子,重合距离是0),你需要设计出来最短的走廊使所有的点都连接. 分析:因为给的都是点的坐标,所以构图的时候会有一些麻烦,不过也仅此而已... ******************************************************************* #include<iostream>#include<cstring>#i

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

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

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