POJ 2031

最小生成树

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <math.h>

using namespace std;

const int Maxn=110;
struct Pex{
	double x,y,z;
	double r;
};
Pex pt[Maxn];
int n;
double map[Maxn][Maxn];
double disp[Maxn];

double dist(Pex &x,Pex &y){
	double a=x.x-y.x;
	double b=x.y-y.y;
	double c=x.z-y.z;
	double dis=sqrt(a*a+b*b+c*c);
	double e=dis-x.r-y.r;
	return e>0?e:0;
}

void solve(){
	bool vis[Maxn]; double ans=0;
	memset(vis,false,sizeof(vis));
	for(int i=1;i<=n;i++)
	disp[i]=map[1][i];
	vis[1]=true;
	for(int i=1;i<=n;i++){
		double mint=1e10; int p=-1;
		for(int k=1;k<=n;k++){
			if(!vis[k]&&mint>disp[k]){
				mint=disp[k];
				p=k;
			}
		}
		if(p==-1) break;
		ans+=mint;
		vis[p]=true;
		for(int k=1;k<=n;k++){
			if(!vis[k]){
				disp[k]=min(disp[k],map[p][k]);
			}
		}
	}
	printf("%.3lf\n",ans);
}

int main(){
	while(scanf("%d",&n),n){
		for(int i=1;i<=n;i++)
		for(int j=i;j<=n;j++)
		map[i][j]=map[j][i]=0;
		for(int i=1;i<=n;i++)
		scanf("%lf%lf%lf%lf",&pt[i].x,&pt[i].y,&pt[i].z,&pt[i].r);
		for(int i=1;i<=n;i++){
			for(int j=i+1;j<=n;j++){
				map[i][j]=map[j][i]=dist(pt[i],pt[j]);
			}
		}
		solve();
	}
	return 0;
}

  

时间: 2024-10-13 03:18:06

POJ 2031的相关文章

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 最小生成树(MST) Kruskal算法

poj——2031 最小生成树(MST)  Kruskal算法 Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 4985   Accepted: 2503 Description You are a member of the space station engineering team, and are assigned a task in the constructio

POJ 2031 prim

Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2255 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

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<

(最小生成树) 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;

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

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

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

zoj 1718 poj 2031 Building a Space Station

最小生成树,用了Kruskal算法.POJ上C++能过,G++不能过... 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]. #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 10100; struct abc{ i