http://poj.org/problem?id=2031

题意

给出三维坐标系下的n个球体,求把它们联通的最小代价。

分析

最小生成树加上一点计算几何。建图,若两球体原本有接触,则边权为0;否则边权为它们球心的距离-两者半径之和。这样来跑Prim就ok了。注意精度。

  1. #include<iostream>
  2. #include<cmath>
  3. #include<cstring>
  4. #include<queue>
  5. #include<vector>
  6. #include<cstdio>
  7. #include<algorithm>
  8. #include<map>
  9. #include<set>
  10. #define rep(i,e) for(int i=0;i<(e);i++)
  11. #define rep1(i,e) for(int i=1;i<=(e);i++)
  12. #define repx(i,x,e) for(int i=(x);i<=(e);i++)
  13. #define X first
  14. #define Y second
  15. #define PB push_back
  16. #define MP make_pair
  17. #define mset(var,val) memset(var,val,sizeof(var))
  18. #define scd(a) scanf("%d",&a)
  19. #define scdd(a,b) scanf("%d%d",&a,&b)
  20. #define scddd(a,b,c) scanf("%d%d%d",&a,&b,&c)
  21. #define pd(a) printf("%d\n",a)
  22. #define scl(a) scanf("%lld",&a)
  23. #define scll(a,b) scanf("%lld%lld",&a,&b)
  24. #define sclll(a,b,c) scanf("%lld%lld%lld",&a,&b,&c)
  25. #define IOS ios::sync_with_stdio(false);cin.tie(0)
  26. #define lc idx<<1
  27. #define rc idx<<1|1
  28. #define rson mid+1,r,rc
  29. #define lson l,mid,lc
  30. using namespace std;
  31. typedef long long ll;
  32. template <class T>
  33. void test(T a) {
  34. cout<<a<<endl;
  35. }
  36. template <class T,class T2>
  37. void test(T a,T2 b) {
  38. cout<<a<<" "<<b<<endl;
  39. }
  40. template <class T,class T2,class T3>
  41. void test(T a,T2 b,T3 c) {
  42. cout<<a<<" "<<b<<" "<<c<<endl;
  43. }
  44. const int inf = 0x3f3f3f3f;
  45. const ll INF = 0x3f3f3f3f3f3f3f3fll;
  46. const ll mod = 1e9+;
  47. int T;
  48. void testcase() {
  49. printf("Case %d: ",++T);
  50. }
  51. const int MAXN = 1e5+;
  52. const int MAXM = ;
  53. const double PI = acos(-1.0);
  54. const double eps = 1e-;
  55. struct node{
  56. double x,y,z,r;
  57. }p[];
  58. double dist(node a,node b){
  59. return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y)+(a.z-b.z)*(a.z-b.z));
  60. }
  61. double g[][],lowc[];
  62. bool vis[];
  63. int main() {
  64. #ifdef LOCAL
  65. freopen("data.in","r",stdin);
  66. #endif // LOCAL
  67. int n;
  68. while(~scd(n)&&n){
  69. for(int i=;i<n;i++){
  70. scanf("%lf%lf%lf%lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r);
  71. }
  72. double maxx=0.0;
  73. for(int i=;i<n;i++){
  74. g[i][i]=0.0;
  75. for(int j=i+;j<n;j++){
  76. if(dist(p[i],p[j])-(p[i].r+p[j].r)<=eps){
  77. g[i][j]=g[j][i]=0.0;
  78. }else{
  79. g[i][j]=g[j][i]=dist(p[i],p[j])-(p[i].r+p[j].r);
  80. maxx=max(maxx,g[i][j]);
  81. }
  82. }
  83. }
  84. mset(vis,false);
  85. double ans=;
  86. vis[]=true;
  87. for(int i=;i<n;i++) lowc[i]=g[][i];
  88. for(int i=;i<n;i++){
  89. double minc = maxx;
  90. int p=-;
  91. for(int j=;j<n;j++){
  92. if(!vis[j]&&minc>lowc[j]){
  93. minc=lowc[j];
  94. p=j;
  95. }
  96. }
  97. ans+=minc;
  98. vis[p]=true;
  99. for(int j=;j<n;j++){
  100. if(!vis[j]&&lowc[j]-g[p][j]>eps){
  101. lowc[j]=g[p][j];
  102. }
  103. }
  104. }
  105.  
  106. printf("%.3f\n",ans);
  107. }
  108. return ;
  109. }

POJ - 2031 Building a Space Station(计算几何+最小生成树)的更多相关文章

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

    题目: Description You are a member of the space station engineering team, and are assigned a task in t ...

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

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

  3. poj 2031 Building a Space Station【最小生成树prime】【模板题】

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepte ...

  4. POJ 2031 Building a Space Station【最小生成树+简单计算几何】

    You are a member of the space station engineering team, and are assigned a task in the construction ...

  5. poj 2031 Building a Space Station(最小生成树,三维,基础)

    只是坐标变成三维得了,而且要减去两边的半径而已 题目 //最小生成树,只是变成三维的了 #define _CRT_SECURE_NO_WARNINGS #include<stdlib.h> ...

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

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

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

    Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepte ...

  8. POJ 2031 Building a Space Station

    3维空间中的最小生成树....好久没碰关于图的东西了.....              Building a Space Station Time Limit: 1000MS   Memory Li ...

  9. POJ - 2031 Building a Space Station 三维球点生成树Kruskal

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

  10. POJ 2031 Building a Space Station (prim裸题)

    Description You are a member of the space station engineering team, and are assigned a task in the c ...

随机推荐

  1. TimeLine CSS/Javascript 时间线

    https://casbootadminserver.herokuapp.com/#/applications/23bd8218/trace

  2. MYSQL INDEX BTREE HASH

    https://dev.mysql.com/doc/refman/5.6/en/index-btree-hash.html 译文:http://itindex.net/detail/54241-tre ...

  3. vue$ref

    vue的$ref方法 可以在元素上template中直接添加ref属性 类似html的id属性 用来做选项卡的切换的

  4. 使用docker-compose 大杀器来部署服务

    使用docker-compose 大杀器来部署服务 上 我们都听过或者用过 docker,然而使用方式却是仅仅用手动的方式,这样去操作 docker 还是很原始. 好吧,可能在小白的眼中噼里啪啦的对着 ...

  5. Macbook系统环境安装wget的2个方法 - 传统包及Homebrew安装

    文章目录 这里有2个方法可以安装wget命令工具: 考虑到自身项目的拓展需要,朋友建议学习Python爬虫这样对于做大数据采集有较大的帮助,老蒋虽然每天也都接触一些脚本和程序的修改,但是并没有专业和系 ...

  6. BZOJ3453 XLkxc(拉格朗日插值)

    显然f(i)是一个k+2项式,g(x)是f(i)的前缀和,则显然其是k+3项式,插值即可.最后要求的东西大胆猜想是个k+4项式继续插值就做完了.注意2p>maxint…… #include< ...

  7. NOIP 2018 游记(退役了!)

    一片空白 在霉的不能再霉的18年11月,Noip2018上,倒霉的我也是贼有意思,感冒加身,D2发烧,数组开小…我还能说什么MMP,身体和考试能力真的很重要. ……(省略无数字的心理活动,有空补上~) ...

  8. hdu5521(Meeting)spfa 层次网络最短路

    题意:给出几个集合,每个集合中有Si个点 且任意两个点的距离为ti,现在要求两个人分别从1和n出发,问最短多长时间才能遇到,且给出这些可能的相遇点; 取两个人到达某点时所用时间大的值 然后取最小的   ...

  9. Navicat的使用技巧

    1.快速查找表:选中一个数据库,然后在右侧会弹出如下的搜索框,搜索表名即可 2.快速清空表的内容:在窗口选中一张表,右键,选择“清空表”

  10. Web.config中设置启用webservice远程调试访问 参数看不到

    <system.web><compilation debug="true" /> <!--begin启用webservice远程访问--> &l ...