题目连接: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

hdu 1162 Eddy's picture (Kruskal 算法)的更多相关文章

  1. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  2. HDU 1162 Eddy's picture (最小生成树)(java版)

    Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...

  3. 题解报告:hdu 1162 Eddy's picture

    Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...

  4. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  5. hdu 1162 Eddy's picture (最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  6. hdu 1162 Eddy's picture (prim)

    Eddy's pictureTime Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. HDU 1162 Eddy's picture (最小生成树 prim)

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  8. HDU 1162 Eddy's picture (最小生成树 普里姆 )

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  9. hdu 1162 Eddy's picture(最小生成树,基础)

    题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...

随机推荐

  1. 认识CSS中盒子模型

    前端之HTML,CSS(六) CSS 盒子模型 CSS中的重点,理解盒子模型对于CSS才能有更清晰的认识.网页说简单一点其实就是一块一块的拼接出来的,可以想象成拼图,所有图块拼接在一起就成了一幅图像. ...

  2. php的魔术常量以及类的模式方法

    魔术方法class A { const PI = 3.14; static $type = 'type1'; public $a1='a1'; public function fun1(){ var_ ...

  3. 前端cookie、localStorage、sessionStorage缓存技术总结

    转载自:https://www.cnblogs.com/belove8013/p/8134067.html 1.Cookie JavaScript是运行在客户端的脚本,因此一般是不能够设置Sessio ...

  4. unity3d vscode

    原来unity3d里assets store有一个插件,下载就行了,插件名就叫vscode 下载完了之后,preference里就会出现,vscode,Enable Integration 这一项勾上 ...

  5. git多账户使用

    由于公司和个人分别有git账号,而git客户端在默认情况下是只能使用一个账号,如果需要使用多个账号,需要额外的设置. 1. 不使用全局设置,使用如下清除全局设置 git config --global ...

  6. java实现猴子选大王问题(约瑟夫问题)

    题目:m只猴子围成一圈报数,报n的猴子自动离开,然后下一位重新从1开始报数,一直循环,最后剩下的那个猴子就是猴大王,写出程序求出最后是大王的那只猴子最初的位置. package learn; impo ...

  7. c# 实现RPC框架的思路

    RPC框架,就是远程调用一个方法就像是本地调用一样. 用于网络消息的话,那么你的收发消息通过异步方法可以写在一起,很方便 核心代码 using System; using System.Collect ...

  8. 转 Unity企业级支持案例与分析

    Unity大中华区技术支持总监张黎明以“Unity企业级支持案例与分析”为主题进行了分享. 以下为演讲实录: 张黎明:非常感谢大家来参加今年的Unite,其实我现在看到有的朋友已经不是第一次来参加Un ...

  9. Struts2拦截器和标签

    一.struts2拦截器 1.struts2是框架,封装了很多的功能,struts2里面封装的功能都是在拦截器里面. 2 struts2里面封装了很多的功能,有很多拦截器,不是每次这些拦截器都执行,每 ...

  10. Oracle 12c心得

    1.重新启动Listener后,远程客户端登录不了,只能全新启动Oralce服务才能正常,经分析,用Net Manager增加一个服务器的IP地址的监听. 执行 net start 监听服务名 再远程 ...