POJ-2031(最小生成树+kruskal)
Building a Space Station
POJ-2031
注意,这里的输出需要是%f型而不是%lf型的,否则wa.
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<vector>
using namespace std;
const int maxn=101;
int n;
struct node{
double x;
double y;
double z;
double r;
};
node cell[maxn];
struct edge{
int from;
int to;
double w;
bool operator<(const edge& t)const{
return w<t.w;
}
};
edge edges[5003];
int set[maxn];
int find(int x){
return x==set[x]?set[x]:set[x]=find(set[x]);
}
double kruskal(int es){
double sum=0.000;
for(int i=0;i<n;i++){
set[i]=i;
}
sort(edges,edges+es);
for(int i=0;i<es;i++){
int from=edges[i].from;
int to=edges[i].to;
double w=edges[i].w;
int x=find(from);
int y=find(to);
if(x!=y){
//cout<<from<<" "<<to<<" "<<w<<endl;
set[x]=y;
sum+=w;
}
}
return sum;
}
int main(){
while(cin>>n&&n){
for(int i=0;i<n;i++){
scanf("%lf%lf%lf%lf",&cell[i].x,&cell[i].y,&cell[i].z,&cell[i].r);
// cin>>cell[i].x>>cell[i].y>>cell[i].z>>cell[i].r;
}
int es=0;//边的数量
for(int i=0;i<n;i++){
for(int j=0;j<i;j++){
double radius=cell[i].r+cell[j].r;//半径之和
double distance=sqrt((cell[i].x-cell[j].x)*(cell[i].x-cell[j].x)+(cell[i].y-cell[j].y)*(cell[i].y-cell[j].y)+(cell[i].z-cell[j].z)*(cell[i].z-cell[j].z));
if(distance<=radius)//重叠
edges[es].from=i,edges[es].to=j,edges[es++].w=0.000;
else{
edges[es].from=i,edges[es].to=j,edges[es++].w=distance-radius;
}
//cout<<distance<<" ";
}
//cout<<endl;
}
double end=kruskal(es);
printf("%.3f\n",end);
}
return 0;
}
POJ-2031(最小生成树+kruskal)的更多相关文章
- POJ 2031 Building a Space Station【经典最小生成树】
链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...
- Building a Space Station POJ - 2031
Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...
- POJ 2031 prim
Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 最小生成树——Kruskal与Prim算法
最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- POJ - 2031 Building a Space Station(计算几何+最小生成树)
http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...
- Building a Space Station POJ 2031 【最小生成树 prim】
http://poj.org/problem?id=2031 Description You are a member of the space station engineering team, a ...
- poj 1258 Agri-Net 最小生成树 kruskal
点击打开链接 Agri-Net Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 33733 Accepted: 13539 ...
- POJ 2031 Building a Space Station (最小生成树)
Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...
随机推荐
- poj3087 Shuffle'm Up
Description A common pastime for poker players at a poker table is to shuffle stacks of chips. Shuff ...
- Codeforces Round #135 (Div. 2) D. Choosing Capital for Treeland
time limit per test 3 seconds memory limit per test 256 megabytes input standard input output standa ...
- Codeforces Global Round 4 B. WOW Factor (前缀和,数学)
题意:找出序列中有多少子序列是\(wow\),但是\(w\)只能用\(vv\)来表示. 题解:我们分别记录连续的\(v\)和\(o\)的个数,用\(v1\)和\(v2\)存,这里要注意前导\(o\)不 ...
- k8s二进制部署 - harbor安装
harbor安装 # 目录说明: # /opt/src : 源码.文件下载目录 # /opt/release : 各个版本软件存放位置 # /opt/apps : 各个软件当前版本的软链接 [root ...
- 数据库之postgreSQL入门操作指南
一.增 二.删 三.改 四.查 五.SQL操作表 1.增加列 ALTER TABLE table_name ADD column_name datatype; 2.删除一列 ALTER TABLE t ...
- LVS+Keepalived深度理解,阐述你不知道的坑点
1. LVS简介 1. 什么是LVS? LVS是Linux Virtual Server的简写,意即Linux虚拟服务器,是一个虚拟的服务器集群系统.本项目在1998年5月由章文嵩博士成立,是中国国内 ...
- PAT l2-018 多项式A除以多项式B 【多项式+模拟】
这仍然是一道关于A/B的题,只不过A和B都换成了多项式.你需要计算两个多项式相除的商Q和余R,其中R的阶数必须小于B的阶数. 输入格式: 输入分两行,每行给出一个非零多项式,先给出A,再给出B.每行的 ...
- 洛谷p1725 露琪诺 单调队列优化的DP
#include <iostream> #include <cstdio> #include <cstring> using namespace std; int ...
- How to enable HTTPS for local development in macOS using Chrome
How to enable HTTPS for local development in macOS using Chrome HTTPS, macOS, Chrome local HTTPS htt ...
- ODM & mongoose
ODM & mongoose ODM (object data modeling) https://mongoosejs.com/ MongoDB NoSQL xgqfrms 2012-202 ...