[2016-04-14][POJ][203][Building a Space Station]

  • 时间:2016-04-14 21:43:30 星期四

  • 题目编号:[2016-04-14][POJ][203][Building a Space Station]

  • 题目大意:给定n个球体,每个球体可能重合,可能包含,可能分离,问把每个球体连接起来(重合和包含看做已经连接),至少需要多长的路

  • 分析:最小生成树,边权为 max(0,disij?ri?rj)max(0,disij?ri?rj),即重合和内含,边权为0

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. #include<cmath>
  5. using namespace std;
  6. const int maxn = 100 + 10;
  7. double g[maxn][maxn],lowc[maxn];
  8. int vis[maxn];
  9. struct Point{
  10. double x,y,z,r;
  11. Point(double a = 0,double b = 0,double c = 0,double d = 0):x(a),y(b),z(c),r(d){}
  12. }p[maxn];
  13. double dtmp(double a,double b){
  14. return (a - b)*(a - b);
  15. }
  16. double dis(int a,int b){
  17. return sqrt(dtmp(p[a].x,p[b].x) + dtmp(p[a].y,p[b].y) + dtmp(p[a].z,p[b].z));
  18. }
  19. double prim(int n){
  20. memset(vis,0,sizeof(vis));
  21. vis[0] = 1;
  22. for(int i = 1 ; i < n ; ++i){
  23. lowc[i] = g[0][i];
  24. }
  25. double ans = 0;
  26. for(int i = 1 ; i < n ; ++i){
  27. double minc = 999999999;
  28. int p = -1;
  29. for(int j = 0 ; j < n ; ++j){
  30. if(!vis[j] && minc > lowc[j]){
  31. minc = lowc[j];
  32. p = j;
  33. }
  34. }
  35. if( minc == 999999999) return -1;
  36. ans += minc;
  37. vis[p] = 1;
  38. for(int j = 0 ; j < n ; ++j){
  39. if(!vis[j] && lowc[j] > g[p][j]){
  40. lowc[j] = g[p][j];
  41. }
  42. }
  43. }
  44. return ans;
  45. }
  46. int main(){
  47. int n;
  48. while(~scanf("%d",&n) && n){
  49. double a,b,c,d,tmp;
  50. for(int i = 0 ; i < n ; ++i){
  51. scanf("%lf%lf%lf%lf",&a,&b,&c,&d);
  52. p[i] = Point(a,b,c,d);
  53. }
  54. for(int i = 0 ; i < n ; ++i){
  55. for(int j = 0 ; j < n ; ++j){
  56. tmp = dis(i,j) - p[i].r - p[j].r;
  57. g[i][j] = max(double(0.0),tmp);
  58. }
  59. }
  60. printf("%.3f\n",prim(n));
  61. }
  62. return 0;
  63. }

来自为知笔记(Wiz)

时间: 2024-10-24 15:15:11

[2016-04-14][POJ][203][Building a Space Station]的相关文章

ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 最小生成树 Kruskal算法

题目链接:ZOJ 1718 POJ 2031 Building a Space Station 修建空间站 Building a Space Station Time Limit: 2 Seconds      Memory Limit: 65536 KB You are a member of the space station engineering team, and are assigned a task in the construction process of the statio

poj 2031 Building a Space Station 【最小生成树 Prim】

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5778   Accepted: 2874 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

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

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5699   Accepted: 2855 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

POJ 2031 Building a Space Station

Building a Space Station Time Limit: 1000ms Memory Limit: 30000KB This problem will be judged on PKU. Original ID: 2031 64-bit integer IO format: %lld      Java class name: Main You are a member of the space station engineering team, and are assigned

poj 2931 Building a Space Station &amp;lt;克鲁斯卡尔&amp;gt;

Building a Space Station Time Limit: 1000MS Memory Limit: 30000K Total Submissions: 5869 Accepted: 2910 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are ex

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

Building a Space Station Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 5173   Accepted: 2614 Description You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You ar

POJ - 2031C - Building a Space Station最小生成树

You are a member of the space station engineering team, and are assigned a task in the construction process of the station. You are expected to write a computer program to complete the task. The space station is made up with a number of units, called

zoj 1718 poj 2031 Building a Space Station

最小生成树,用了Kruskal算法.POJ上C++能过,G++不能过... 算出每两个圆心之间的距离,如果距离小于两半径之和,那么这两个圆心之间的距离直接等于0,否则等于距离-R[i]-R[j]. #include<stdio.h> #include<string.h> #include<math.h> #include<algorithm> using namespace std; const int maxn = 10100; struct abc{ i

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

http://poj.org/problem?id=2031 题意 给出三维坐标系下的n个球体,求把它们联通的最小代价. 分析 最小生成树加上一点计算几何.建图,若两球体原本有接触,则边权为0:否则边权为它们球心的距离-两者半径之和.这样来跑Prim就ok了.注意精度. #include<iostream> #include<cmath> #include<cstring> #include<queue> #include<vector> #in