POJ - 2031 Building a Space Station (prim)

The meaning of problems: The number given spherical station N, and the three-dimensional coordinates of each station x, y, z and a radius r, find the minimum cost to all the connected station (cost is equal to the distance between the station) if the contact, comprising, or intersect We do not need to build a bridge

Idea: or a minimum spanning tree problem, we first recorded information for each space station, space station twenty-two then all connected, if case of contact, contains, or intersection we put cost is set to 1, otherwise the distance radius with.

Each space station with a label to indicate, then recorded the star forward. Can be used to find the minimum spanning tree algorithm prim.

Another point to note is that the output of the last knot: To use the G ++ unusable% f% lf

Complete Code;

#include <iostream>
#include <cstring>
#include <cstdio>
#include <queue>
#include <vector>
#include <cmath>
#include <algorithm>
using namespace std;
const int maxn = 1000;
const int maxm = 1e5;
int n,m;
double ans;
typedef pair<double,int> pii;
struct Egde{
    int u,v,next;
    double w;
}edge[maxm];
struct node{
    double x,y,z,r;
}space[maxn];
struct cmp{
    bool operator () (pii a, pii b){
        return a.first > b.first;
    }
};
int head[maxn];
int vis[maxn];
double dist[maxn];
int top;
void init(){
    memset(head,-1,sizeof(head));
    memset(vis,0,sizeof(vis));
    memset(dist,-1,sizeof(dist));
    top = 0;
    ans = 0;
}
void add(int u,int v,double w){
    int i;//更新最小边权值 
    for(i=head[u]; ~i; i=edge[i].next){
        if(edge[i].v == v){
            if(edge[i].w > w) edge[i].w = w;
            return ;
        }    
    }
    edge[top].u = u;
    edge[top].v = v;
    edge[top].w = w;
    edge[top].next = head[u];
    head[u] = top++;
}
void prim(int s){
    int i;
    priority_queue<pii,vector<pii>, cmp>q;
    vis[s] = 1;
    dist[s] = 0;
    for(i = head[s];~i;i = edge[i].next){
        int v = edge[i].v;
        dist[v] = edge[i].w;
        q.push(make_pair(dist[v],v));
    }
    while(!q.empty()){
        pii t = q.top();
        q.pop();
        if(vis[t.second]) continue;
        ans += t.first;
        vis[t.second] = 1;
        
        for(i = head[t.second]; ~i;i = edge[i].next){
        int v = edge[i].v;
        if(!vis[v]&&(dist[v]>edge[i].w)||dist[v] == -1){
            dist[v] = edge[i].w;
            q.push(make_pair(dist[v],v));
            }
        }        
    }
    
}
double getDist(int i,int j){
    double sX = (space[i].x-space[j].x)*(space[i].x-space[j].x);
    double sY = (space[i].y-space[j].y)*(space[i].y-space[j].y);
    double sZ = (space[i].z-space[j].z)*(space[i].z-space[j].z);
    double dis =  sqrt(sX+sY+sZ);
    if(dis<=(space[i].r+space[j].r)) return 0.00;
    else return dis-(space[i].r+space[j].r); 
}
int main(){
    while(cin>>n&&n){
//        cin>>m;
        init();
        for(int i=0; i<n;i++){
            cin>>space[i].x>>space[i].y>>space[i].z>>space[i].r;
        }
        for(int i=0; i<n;i++){
            for(int j=0;j<n;j++){
                if(i==j) continue;
                add(i,j,getDist(i,j));
                add(j,i,getDist(j,i));
            }
        } 
        prim(0); 
        the printf ( " % .3f \ n- " , ANS); // Note: G ++ accuracy at output if use% f% lf with output will wa 
    } 
}

 

Guess you like

Origin www.cnblogs.com/Tianwell/p/11302317.html