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;
}

最新文章

  1. HTML之DocType的几种类型 -转载
  2. 添加office权限时找不到ofice,com组件的方法
  3. 提高c++性能的编程技术笔记
  4. Bootstrap配套的js框架
  5. mysqldump备份与还原mysql数据的实例
  6. CSS3新特性(阴影、动画、渐变、变形、伪元素等) CSS3与页面布局学习总结——CSS3新特性(阴影、动画、渐变、变形、伪元素等)
  7. jQuery 迭代器
  8. jvm 调优 工具
  9. hdu_2544_最短路(spfa版子)
  10. 【Jquery】prop与attr的差别
  11. 使用ip开头的工具,而不是只会ifconfig
  12. swift UIview上添加视频播放
  13. LVDS接口分类,时序,输出格式
  14. 分形之谢尔宾斯基(Sierpinski)地毯
  15. [Python]运算符的优先级顺序
  16. SpringBoot集成redisson分布式锁
  17. 【Acm】算法之美—Anagrams by Stack
  18. linux no space left on device
  19. 网络排错与网络命令的理解ping-traceroute-host(nslookup)-tcpdump获取对方的mac
  20. jquery Mobile入门—多页面切换示例学习

热门文章

  1. 【uva 1515】Pool construction(图论--网络流最小割 模型题)
  2. zjnu1786 PROSJEK(二分)
  3. poj3252 Round Numbers (数位dp)
  4. 浅谈Webpack模块打包工具三
  5. 无需扫描即可查找和攻击域SQL Server (SPN)
  6. 009.NET5_程序的发布运行
  7. 101道Numpy、Pandas练习题
  8. vue watcher errors
  9. Laravel Homestead 安装 使用教程详解!
  10. Document This All In One