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<queue>
#include<iostream>
using namespace std;
#define maxn 10000
double maps[maxn][maxn];
double x[maxn], y[maxn], z[maxn], r[maxn], dist[maxn];
int v[maxn];
int n;

void Init()
{
    int i, j;
    for(i=1; i<=n; i++)
    for(j=1; j<=n; j++)
    if(i==j) maps[i][j] = 0;
    else maps[i][j]=maps[j][i]= 10000000 ;
}

double Dij()
{
    memset(dist, 0, sizeof(dist));
    memset(v, 0, sizeof(v));
    for(int i=1; i<=n; i++)
        dist[i] = maps[1][i];

    v[1] = 1;

    double sum = 0;

    for(int i=1; i<n; i++)
    {
        int index;
        double  mins=10000000;

        for(int j=1; j<=n; j++)
        {
            if(!v[j] && dist[j] < mins)
            {
                index = j;
                mins = dist[j];
            }
        }

    if(mins == 10000000)  return 0;
        sum += mins;
        v[index] = 1;

        for(int j=1; j<=n; j++)
        {
            if(!v[j] && dist[j] >= maps[index][j])
            {
                dist[j]= maps[index][j];
            }
        }
    }

    return sum;
}
int main()
{
    while(scanf("%d", &n), n)
    {
         for(int i=1; i<=n; i++)
            scanf("%lf %lf %lf %lf", &x[i], &y[i], &z[i], &r[i]);

         Init();

         for(int i=1; i<n; i++)
         {
             for(int j=i+1; j<=n; j++)
             {

                 double d=sqrt((x[j]-x[i])*(x[j]-x[i])+(y[j] - y[i])*(y[j] - y[i])+(z[j]-z[i])*(z[j]-z[i])) ;

                  double p = r[i] + r[j];

                  if(d <= p)
                    {
                        maps[i][j]=maps[j][i]=0;
                        continue;
                    }

                 maps[i][j] = maps[j][i] = min(maps[i][j], d-p);

             }
         }

        double ans = Dij();

        printf("%.3lf\n",ans);
    }
    return 0;
}

时间: 2024-10-16 08:54:51

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

链接: 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;

C - Building a Space Station - poj 2031

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

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

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

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

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