POJ 2031
来源:互联网 发布:js可以控制浏览器比例 编辑:程序博客网 时间:2024/06/11 23:42
#include <iostream>#include <cstdio>#include <algorithm>#include <cstring>#include <cmath>using namespace std;#define esp 10E-9int k,n;int p[105];struct Edge{ int u,v; double w;}edge[105*105];struct Cor{ double x,y,z,r;}cor[105];bool cmp(Edge a,Edge b){ return a.w<b.w;}int findpa(int x){ return p[x]==x?x:p[x]=findpa(p[x]);}double kruskal(){ for(int i=1;i<=n;i++) p[i]=i; double ans=0.0; int num=0; sort(edge,edge+k,cmp); for(int i=0;i<k;i++) { int p1=findpa(edge[i].u); int p2=findpa(edge[i].v); if(p1!=p2) { ans+=edge[i].w; p[p1]=p2; num++; } if(num==n-1)return ans; } return ans; //很费解,少了这一句之后一直wa。 }int main(){ while(scanf("%d",&n)!=EOF&&n) { k = 0; for(int i = 1; i <= n; i ++) scanf("%lf%lf%lf%lf",&cor[i].x,&cor[i].y,&cor[i].z,&cor[i].r); for(int i = 1; i <= n; i ++) for(int j = i+1; j <=n; j ++) { edge[k].u = i; edge[k].v = j; edge[k].w = sqrt((cor[i].x-cor[j].x)*(cor[i].x-cor[j].x)+(cor[i].y-cor[j].y) *(cor[i].y-cor[j].y)+(cor[i].z-cor[j].z)*(cor[i].z-cor[j].z))-cor[i].r-cor[j].r; if(edge[k].w<=esp) edge[k].w=0; k++; } double ans=kruskal(); printf("%.3lf\n",ans); } return 0;}
0 0
- poj 2031
- poj 2031
- poj 2031
- POJ 2031
- poj 2031
- poj 2031
- POJ 2031
- POJ 2031
- POJ 2031
- poj 2031 Kruskal
- POJ 2031 prim
- poj 2031(prim)
- POJ
- poj
- POJ
- POJ
- poj
- poj
- Android中如何使用ViewPager
- 包以德循环(OODA环)
- 进程的学习
- srm 539
- 算法学习之一java实现选择排序
- POJ 2031
- JS高级程序设计7-函数表达式
- Ubuntu 14.04 配置android源码开发/编译环境
- codeforces #312 B B. Amr and The Large Array(统计)
- java容器类---概述
- Java基础---synchronized 详解
- centos下配置apache用户及用户组权限
- Goldengate(ora11g)配置说明
- 字符串