题目连接: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. [Alpha]Scrum Meeting#1

    github 本次会议项目由PM召开,时间为4月1日晚上10点30分 时长10分钟 任务表格 人员 昨日工作 下一步工作 木鬼 - 撰写初版技术规格说明书(issue#1) - 撰写初版功能规格说明书 ...

  2. Windows搭建Go语言环境·

    对于Windows用户,Go语言提供两种安装方式(源码安装除外): .MSI安装:程序会自动配置你的安装 .ZIP安装:需要你手动设置一些环境变量 一.MSI安装 1.下载安装包(根据操作系统选择相应 ...

  3. django中使用时间帅选报RuntimeWarning: DateTimeField Coupon.valid_begin_date received a naive datetime (2018-08-16 20:51:40.135425) while time zone support is active.

    今天在使用当前时间进行筛选数据时出现了RuntimeWarning: DateTimeField Coupon.valid_begin_date received a naive datetime ( ...

  4. sql2008R2新建链接服务器。

    1:用sql新建链接服务器对象: /****** Object: LinkedServer [pad] Script Date: 10/23/2018 15:47:45 ******/ EXEC ma ...

  5. 匈牙利算法、KM算法

    PS:其实不用理解透增广路,交替路,网上有对代码的形象解释,看懂也能做题,下面我尽量把原理说清楚 基本概念 (部分来源.部分来源) 二分图: 设G=(V,E)是一个无向图,如果顶点V可分割为两个互不相 ...

  6. Spring boot 项目部署服务器

    Spring Boot 有两种部署到服务器的方式,这里介绍官方推荐的(jar包) 一.首先进行application.properties配置 # EMBEDDED SERVER CONFIGURAT ...

  7. React条件性渲染

    React条件性渲染的方式和Vue是不同的,之前用vue做项目时觉得vue是在是强大,通过v-if就可以选择性的渲染组件,另外,对于列表的渲染更是方便,一个v-for就可以进行快速的渲染,但是Reac ...

  8. 【ExtJS】关于标准模块化封装组件

    在此之前,自己封装自定义控件用的是这样的方式: Ext.define('My.XXX',{ extend: 'Ext.YYY', xtype: 'ZZZ', . . . items:[ ... ] } ...

  9. 在VIM中添加行号的方法

    VIM编辑器是可以显示行号的.但是,有时候我们需要在整个代码的行首添加行号.怎么实现呢?实现的方法有很多,这里就介绍我知道的一种吧. 在每行行首添加某个字符串 :%s/^/your_string/ 在 ...

  10. spring mvc中DispatcherServlet如何得到ModelAndView的

    首先看下面这种张图,这张图说明了spring mvc整体的流程. 本文讲的就是如何从DispatcherServlet中得到ModerAndView的过程. 首先看DispatherServlet这个 ...