POJ 2031 BUilding a Space Station 两点间距离 + 最小生成树
2013-10-12 08:31
417 查看
没有什么技术含量。
套模板就好了。
套模板就好了。
#include <cmath> #include <iostream> #include <cstring> #include <cstdio> #include <algorithm> using namespace std; struct P { double x,y,z,r; }p[110]; struct E { int u,v; double w; }edge[10010]; double cal(P p1,P p2) { double w; w = sqrt( (p1.x-p2.x)*(p1.x-p2.x) + (p1.y-p2.y)*(p1.y-p2.y) + (p1.z-p2.z)*(p1.z-p2.z) ); w -= (p1.r+p2.r); return w > 0 ? w : 0; } int fp[110]; bool cmp(E e1,E e2) { return e1.w < e2.w; } int find(int x) { int k,j,r; r = x; while(r != fp[r]) { r = fp[r]; } k = x; while(k != r) { j = fp[k]; fp[k] = r; k = j; } return r; } bool merge(E e) { int fu = find(e.u); int fv = find(e.v); if(fu == fv) return false; fp[fu] = fv; return true; } int main() { int n; int i,j,top; double sumw; while(scanf("%d",&n) && n) { for(i = 1;i <= n; ++i) { scanf("%lf %lf %lf %lf",&p[i].x,&p[i].y,&p[i].z,&p[i].r); } for(i = 1,top = 0;i <= n; ++i) { for(j = i+1;j <= n; ++j) { edge[top].u = i; edge[top].v = j; edge[top++].w = cal(p[i],p[j]); } } sort(edge,edge+top,cmp); for(i = 1;i <= n; ++i) fp[i] = i; for(i = 0,sumw = 0;i < top ; ++i) { if(merge(edge[i])) { sumw += edge[i].w; } } printf("%.3f\n",sumw); } return 0; }
相关文章推荐
- UITableViewCell 的基本样式
- iOS开发资源分享:UIKit关键帧动画框架Jazz Hands、MTLog、Pocket Weather源码以及其他
- poj 3080 Blue Jeans ( 二分 + kmp)
- Visual Studio 2010 Rebuild问题
- Given x, less than 100. How will you generate true with probability x/100.
- String,StringBuffer与StringBuilder的区别??
- UITableViewCell 重用 cell错乱问题
- amssbuildingbylight
- Client does not support authentication protocol requested by server;
- 理性选择key-value Store
- UITableView 基本使用方法
- querySelector和querySelectorAll
- UIScrollView/UITableView 一直显示滚动条(ScrollBar Indicators)、滚动条Width(宽度)、滚动条Color(颜色)
- uuid
- NGUI相关小知识
- mysql中key 、primary key 、unique key 与index区别
- Android UI开发第三十三篇——Navigation Drawer For Android API 7
- iOS入门之UIButton/ViewController/UIImage
- CF 353D - Queue(思路题-难想!!)
- 一个 C# 获取高精度时间类(调用API QueryP*)