POJ 2031 Building a Space Station 简单计算几何 + 最小生成树
来源:互联网 发布:mac版cad布局颜色设置 编辑:程序博客网 时间:2024/05/22 22:09
如题 :简单计算几何 + 最小生成树
求出连个cell的距离 小于零距离为零 否者就是两球心的距离减两球半径
因为边较少所以用邻接矩阵存储 最后用一个prim计算最小生成树 答案就出来了
注意 :POJ在G++中编译的 输出浮点型都要用f 不能用lf
下面看代码:
#include<cstdio>#include<cmath>#include<cstring>#define eps 1e-8#define N 105#define MAX 0x3fffffffusing namespace std;struct cell{ double x,y,z,r;}c[N];double Dis(cell a,cell b){ 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));}int n;double edge[N][N],res,dis[N];bool flag[N];double prim(int s){ for(int i = 0; i <= N; i++) dis[i] = MAX; memset(flag,false,sizeof(flag)); dis[s]=0; flag[s]=true; for(int i=1; i<=n; i++) { if(dis[i]>edge[s][i]) dis[i]=edge[s][i]; } for(int i=1; i<n; i++) { int pos=-1; double Min=0x3f3f3f3f; for(int j=1; j<=n; j++) { if(!flag[j]&&dis[j]<Min) { Min=dis[j]; pos=j; } } if(pos==-1) return -1; res += Min; flag[pos]=true; for(int j=1; j<=n; j++) { if(!flag[j]&&edge[pos][j]<dis[j]) dis[j]=edge[pos][j]; } } return res;}int main(){ #ifdef LOCAL freopen("in.txt","r",stdin); #endif // LOCAL int i,j; while((scanf("%d",&n),n)!=0) { for(i = 1;i <=n; i++) scanf("%lf%lf%lf%lf",&c[i].x,&c[i].y,&c[i].z,&c[i].r); for(i = 1; i<=n; i++) for(j = 1; j<=n; j++) { if(i == j){edge[i][j] = MAX;continue;} double d = Dis(c[i],c[j])-c[i].r-c[j].r; if(d < eps) edge[i][j] = 0; else edge[i][j] = d; } res = 0; double result = prim(1); printf("%.3f\n",result); } return 0;}
- POJ 2031 Building a Space Station 简单计算几何 + 最小生成树
- poj 2031 Building a Space Station(计算几何+最小生成树)
- poj 2031 -- Building a Space Station(最小生成树)
- POJ 2031 Building a Space Station【经典最小生成树】
- POJ 2031 Building a Space Station (最小生成树)
- poj 2031 Building a Space Station 最小生成树
- poj 2031 Building a Space Station (最小生成树)
- POJ 2031 Building a Space Station(最小生成树)
- POJ 2031 Building a Space Station(最小生成树)
- POJ 2031 Building a Space Station(最小生成树)
- POJ 2031-Building a Space Station(最小生成树)
- poj 2031 Building a Space Station 最小生成树
- poj 2031 Building a Space Station(最小生成树)
- poj 2031 Building a Space Station 最小生成树
- POJ 2031Building a Space Station Kruskal最小生成树
- poj 2031 Building a Space Station 【最小生成树 prime】
- poj 2031 Building a Space Station(最小生成树)
- poj 2031 Building a Space Station 【最小生成树 Prim】
- 参考ffmpeg的例子写的h264编解码显示程序
- jquery再体验
- MFC中使用ADO操作各类数据库的封装类,包括MySql、Access、Oracle、MSSql
- 悲观锁和乐观锁
- POJ 3181 Dollar Dayz 简单DP
- POJ 2031 Building a Space Station 简单计算几何 + 最小生成树
- poj 2912 rochambeau
- Oracle字符串函数
- JavaDoc生成chm帮助文件
- 子串分离
- FreeMarker 语法
- bluetooth 目录
- git push . :refs/for/foo 意思
- Oracle GoldenGate和Dataguard以及stream复制的区别