【POJ 2031】 Building a Space Station (三维坐标求距离+最小生成树)
2015-08-24 17:47
471 查看
【POJ 2031】 Building a Space Station
n个空间球 可能存在包含或者覆盖的状况 想要让所有球都联通 每个球有坐标x,y,z和半径 问还需要搭建最少多长的路
存储所有需要搭建的路 跑一遍最小生成树即可 注意原本就重叠的球距离0直接加入
代码如下:
n个空间球 可能存在包含或者覆盖的状况 想要让所有球都联通 每个球有坐标x,y,z和半径 问还需要搭建最少多长的路
存储所有需要搭建的路 跑一遍最小生成树即可 注意原本就重叠的球距离0直接加入
代码如下:
#include <iostream> #include <cstdio> #include <cstring> #include <cmath> #include <algorithm> #define esp 1e-5 using namespace std; typedef struct Point//三维坐标 { double x,y,z,r; }Point; typedef struct Edge//边 { int u,v; double w; bool operator <(const struct Edge a)const { return w - a.w < -esp; } }Edge; Point pt[100];//点 int pre[100];//Kruskal并查集 Edge eg[10000]; int tp; void Init(int x)//初始化并查集 { for(int i = 0; i < x; ++i) pre[i] = i; } int Find(int x) { return pre[x] == x? pre[x]: (pre[x] = Find(pre[x])); } int main() { int n,i,j,k,r,cnt; double dis,sum; while(~scanf("%d",&n) && n) { Init(n); cnt = tp = 0; for(i = 0; i < n; ++i) { scanf("%lf %lf %lf %lf",&pt[i].x,&pt[i].y,&pt[i].z,&pt[i].r); for(j = 0; j < i; ++j) { dis = sqrt((pt[i].x-pt[j].x)*(pt[i].x-pt[j].x)+(pt[i].y-pt[j].y)*(pt[i].y-pt[j].y)+(pt[i].z-pt[j].z)*(pt[i].z-pt[j].z));//两点距离 if(dis - pt[i].r - pt[j].r > esp)//如果距离减两半径为正 则需要加边 { eg[tp].u = i; eg[tp].v = j; eg[tp++].w = dis - pt[i].r - pt[j].r; } else//否则距离零 直接加入一个集合 { k = Find(i); r = Find(j); if(k != r) { pre[k] = r; cnt++; } } } } sort(eg,eg+tp); sum = 0; for(i = 0; i < tp; ++i)//Kruskal { k = Find(eg[i].u); r = Find(eg[i].v); if(k != r) { pre[k] = r; sum += eg[i].w; cnt++; } if(cnt == n-1) break; } printf("%.3f\n",sum); } return 0; }
相关文章推荐
- IOS-6-UI知识总结-1(代码添加控件、视图切换、多种传值方式)
- iOS下UITableView的单元格重用逻辑
- 总结Android UI 自动化测试
- TableViewCell添加2个和多个侧滑菜单按钮
- leetcode 187: Repeated DNA Sequences
- AS3 URLRequest相关,浏览器请求头MIME类型引发的问题
- 类似与fiddler的抓包工具 burp suite free edition
- iOS开发-Day29-UI UIScrollView&多视图
- POJ3210 a stack or a queue
- MII、GMII、RMII、SGMII、XGMII、XAUI、Interlaken
- DuiLib : 使用CListUI显示海量数据
- JAVA UUID 生成
- easyiui textbox 光标 聚焦
- 程序检测系统是bios引导还是uefi引导
- UITabbarViewController+Navigation框架下的子视图的跳转示例之一
- UIActionSheet的常见用法
- poj 2051 priority_queue应用
- CF_313B_IlyaAndQueries
- 08-排序3. Talent and Virtue (25)
- 调用[[UIDevice currentDevice] userInterfaceIdiom]==UIUserInterfaceIdiomPad判断设备