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)的更多相关文章

  1. POJ 2031 Building a Space Station【经典最小生成树】

    链接: http://poj.org/problem?id=2031 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=22013#probl ...

  2. Building a Space Station POJ - 2031

    Building a Space Station POJ - 2031 You are a member of the space station engineering team, and are ...

  3. POJ 2031 prim

    Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 4400 Accepted: 2 ...

  4. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  5. 最小生成树——Kruskal与Prim算法

    最小生成树——Kruskal与Prim算法 序: 首先: 啥是最小生成树??? 咳咳... 如图: 在一个有n个点的无向连通图中,选取n-1条边使得这个图变成一棵树.这就叫“生成树”.(如下图) 每个 ...

  6. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  7. POJ - 2031 Building a Space Station(计算几何+最小生成树)

    http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边 ...

  8. 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 ...

  9. poj 1258 Agri-Net 最小生成树 kruskal

    点击打开链接 Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 33733   Accepted: 13539 ...

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

    Building a Space Station 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/C Description Yo ...

随机推荐

  1. 【uva 1615】Highway(算法效率--贪心 区间选点问题)

    题意:给定平面上N个点和一个值D,要求在x轴上选出尽量少的点,使得对于给定的每个店,都有一个选出的点离它的欧几里德距离不超过D. 解法:先把问题转换成模型,把对平面的点满足条件的点在x轴的直线上可得到 ...

  2. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  3. Educational Codeforces Round 88 (Rated for Div. 2) E、Modular Stability 逆元+思维

    题目链接:E.Modular Stability 题意: 给你一个n数,一个k,在1,2,3...n里挑选k个数,使得对于任意非负整数x,对于这k个数的任何排列顺序,然后用x对这个排列一次取模,如果最 ...

  4. hdu3564 Another LIS

    Problem Description There is a sequence firstly empty. We begin to add number from 1 to N to the seq ...

  5. Codeforces Round #594 (Div. 2) C. Ivan the Fool and the Probability Theory (思维,递推)

    题意:给你一个\(n\)x\(m\)的矩阵,需要在这些矩阵中涂色,每个格子可以涂成黑色或者白色,一个格子四周最多只能有\(2\)个和它颜色相同的,问最多有多少种涂色方案. 题解:首先我们考虑一维的情况 ...

  6. POJ1087 A Plug for UNIX(网络流)

    在会议开始之前,你收集所有记者想要使用的设备,并尝试设置它们.你注意到有些设备使用没有插座的插头.你想知道这些设备是否来自建造这个房间时并不存在的国家.对于一些插座,有几个设备使用相应的插头.对于其他 ...

  7. Intelligent IME HDU - 4287 字典树

    题意: 给你m个字符串,每一个字符对应一个数字,如下: 2 : a, b, c    3 : d, e, f    4 : g, h, i    5 : j, k, l    6 : m, n, o ...

  8. 牛客编程巅峰赛S1第3场 - 青铜&白银 A.位数求和

    题意:求所有\(n\)位数每位之和等于\(m\)的数的和. 题解:数据范围非常小,我们可以直接暴力枚举\(t\)到\(10*t\)的所有数字,逐位分解判断即可. 代码: class Solution ...

  9. LEETCODE - 1228【等差数列中缺失的数字】

    C++: class Solution { public:     int missingNumber(vector<int>& arr) {         int subnum ...

  10. Leetcode(206)-反转链表

    反转一个单链表. 示例: 输入: 1->2->3->4->5->NULL 输出: 5->4->3->2->1->NULL 思路:反转链表很简 ...