题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162

Eddy's picture

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 10085    Accepted Submission(s):
5094

Problem Description
Eddy begins to like painting pictures recently ,he is
sure of himself to become a painter.Every day Eddy draws pictures in his small
room, and he usually puts out his newest pictures to let his friends appreciate.
but the result it can be imagined, the friends are not interested in his
picture.Eddy feels very puzzled,in order to change all friends 's view to his
technical of painting pictures ,so Eddy creates a problem for the his friends of
you.
Problem descriptions as follows: Given you some coordinates pionts on a
drawing paper, every point links with the ink with the straight line, causes all
points finally to link in the same place. How many distants does your duty
discover the shortest length which the ink draws?
 
Input
The first line contains 0 < n <= 100, the number
of point. For each point, a line follows; each following line contains two real
numbers indicating the (x,y) coordinates of the point.

Input contains
multiple test cases. Process to the end of file.

 
Output
Your program prints a single real number to two decimal
places: the minimum total length of ink lines that can connect all the points.
 
Sample Input
3
1.0 1.0
2.0 2.0
2.0 4.0
 
Sample Output
3.41
 
题目大意: 给出一些点的坐标,然后连线使得所有顶点都能够连通,并且线的长度最短。
 
解题思路: 根据题意,可以选用以边为主导的Kruskal算法计算。
 
AC代码:
19975116 2017-03-03 08:53:42 Accepted 1162 0MS 1524K 1304 B G++
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm> using namespace std; struct point
{
int x,y;
double l;
}p[];
int parent[],n;
double x[],y[];
bool cmp(point a, point b)
{
return a.l < b.l;
}
int find(int x)
{
int s,tmp;
for (s = x; parent[s] >= ; s = parent[s]);
while (s != x)
{
tmp = parent[x];
parent[x] = s;
x = tmp;
}
return s;
}
void Union(int A, int B)
{
int a = find(A), b = find(B);
int tmp = parent[a]+parent[b];
if (parent[a] < parent[b])
{
parent[b] = a;
parent[a] = tmp;
}
else
{
parent[a] = b;
parent[b] = tmp;
}
}
void kruskal(int k)
{
double sum = ;
int u,v,i,j = ;
memset(parent,-,sizeof(parent));
for (i = ; i <= k; i ++)
{
u = p[i].x; v = p[i].y;
if (find(u) != find(v))
{
sum += p[i].l;
Union(u,v);
j ++;
}
if (j == n-) break;
}
printf("%.2lf\n",sum);
}
int main ()
{
int i,j,k;
while (scanf("%d",&n)!=EOF)
{
k = -;
for (i = ; i <= n; i ++)
scanf("%lf%lf",x+i,y+i);
for (i = ; i < n; i ++)
for (j = +i; j <= n; j ++)
{
p[++ k].x = i;
p[k].y = j;
p[k].l = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
}
sort(p,p+k+,cmp);
kruskal(k);
}
return ;
}

算法理解:http://www.cnblogs.com/yoke/p/6506492.html

最新文章

  1. ubuntu下建立NFS共享,并用开发板挂载
  2. 关于处理小数点位数的几个oracle函数
  3. Apache ab压力测试时出现大量的错误原因分析
  4. Leetcode 1 two sum 难度:0
  5. Sass学习之路(4)——不同样式风格的输出方式
  6. 20150601_Andriod 打开新窗体
  7. burp
  8. SQL 使用Cursor(游标)遍历结果集
  9. 转:从BeagleBone谈AM335x硬件系统设计
  10. 【翻译自mos文章】当指定asm disk 为FRA时,11.2.0.3的dbua hang住
  11. sofa-pbrpc 1.1.1 发布,RPC 网络通信库
  12. Weinre 远程调试移动端页面
  13. Unity CommandInvokationFailure: Failed to re-package resources. 解决方案
  14. asp.net后台发送HTTP请求
  15. 【腾讯Bugly干货分享】Android内存优化总结&amp;实践
  16. BZOJ4860 BJOI2017 树的难题 点分治、线段树合并
  17. Mybatis中的like模糊查询四种方式
  18. (转)以太坊(Ethereum)全零地址(0x000000...)揭秘
  19. ArrayList删除--------ConcurrentModificationException问题
  20. Java 中 synchronized的用法详解(四种用法)

热门文章

  1. 解决php -v查看到版本于phpinfo()打印的版本不一致问题
  2. Locust源码目录结构及模块作用
  3. git遇到的常见错误整理
  4. vue项目element-ui框架中的弹窗中的表单验证清除问题
  5. Json 装 list&lt;object&gt;objectList
  6. (转)linux内存源码分析 - 内存回收(lru链表)
  7. MySQL prompt提示符总结
  8. Golang教程:数组和切片
  9. 架构实战项目心得(九):后台服务工具ldap:统一用户中心ldap工具使用以及安装
  10. C#语言-04.OOP基础