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