UVA 10034 By ACReaper
来源:互联网 发布:ar软件下载 编辑:程序博客网 时间:2024/05/21 14:54
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>using namespace std;const int maxn = 15024;int u[maxn];int v[maxn];double w[5 * maxn];int r[5 * maxn];int fa[maxn];struct pos{double x,y;};pos A[maxn];int cmp(const int a,const int b){return w[a] < w[b];}int find(int root){return root == fa[root]?root:find(fa[root]);}int main(){int tc;scanf("%d",&tc);for(int t = 1; t <= tc; t++){int n;scanf("%d",&n);for(int i = 1 ; i <= n; i++){scanf("%lf%lf",&A[i].x,&A[i].y);}memset(w,0,sizeof(w));int k = 1;for(int i = 1; i <= n; i++){//构建完全图 for(int j = i + 1;j <= n; j++){double d = sqrt((A[i].x - A[j].x) * (A[i].x - A[j].x) + (A[i].y - A[j].y) * (A[i].y - A[j].y));u[k] = i,v[k] = j,w[k++] = d;}}double ans = 0.0;int num_e = n * (n - 1) /2;for(int i = 1; i <= n; i++)fa[i] = i;for(int i = 1; i <= num_e; i++)r[i] = i;sort(r + 1,r + 1 + num_e,cmp);//for(int i = 1; i <= num_e;i++){int e = r[i];int x = find(u[e]);//找到起点int y = find(v[e]);if( x != y){fa[x] = y;ans += w[e];} }printf("%s", t == 1? "" : "\n");printf("%.2lf\n",ans);}return 0;}
2013 05 07
By ACReaper
- UVA 10034 By ACReaper
- UVA 567 By ACReaper
- Uva 11300 By ACReaper
- uva 10369 By ACReaper
- uva 10369 By ACReaper
- UVA 10986 By ACReaper
- UVA 10801 By ACReaper
- UVA 558 判断负环 By ACReaper
- 帕斯卡恒等式 By ACReaper
- 范德蒙很等式 By ACReaper
- 幂取模(数论初步) By ACReaper
- 离散概率初步 By ACReaper
- 表达式树(图论) By ACReaper
- 生成下一个排列 By ACReaper
- 流网络浅析 By ACReaper
- QuickSort简解(分治思想) By ACReaper
- Huffman算法(贪心算法) By ACReaper
- 最优三角划分(动态规划) By ACReaper
- 二叉树特性及详细例子
- 二分堆(binary)
- UVA 109 SCUD Busters【凸包模拟题】
- 4.Collection Underscore Methods(Official Tutorials)
- OSX: 在VirtualBox中运行BootCamp上的Windows-补遗
- UVA 10034 By ACReaper
- 数据库提示,正在使用无法还原
- 黑马程序员_GUI
- Java虚拟机(JVM)中的内存设置详解
- ios joystick 虚拟摇杆实现 ( 非 Cocos2d )
- 数据结构之单循环链表C++(模板)
- 安装程序时,内存不能读或不能写问题解决
- Linux I2C核心、总线与设备驱动I
- 用户、组或角色’XXXX’在当前数据库中已存在