hdu 1162 Eddy's picture (Kruskal 算法)
题目连接: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
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?
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.
places: the minimum total length of ink lines that can connect all the points.
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 ;
}
hdu 1162 Eddy's picture (Kruskal 算法)的更多相关文章
- hdu 1162 Eddy's picture(最小生成树算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- HDU 1162 Eddy's picture (最小生成树)(java版)
Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...
- 题解报告:hdu 1162 Eddy's picture
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
- HDU 1162 Eddy's picture
坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- hdu 1162 Eddy's picture (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1162 Eddy's picture (prim)
Eddy's pictureTime Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tot ...
- HDU 1162 Eddy's picture (最小生成树 prim)
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- HDU 1162 Eddy's picture (最小生成树 普里姆 )
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- hdu 1162 Eddy's picture(最小生成树,基础)
题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<string.h> #include <ma ...
随机推荐
- anaconda安装出现failed to create anacoda menue
1.卸载Anaconda后重新安装Anaconda出现各种问题,粗暴解决方式:直接将安装目录放在C盘主路径下,完美解决. 2.然后无选择忽略,忽略,忽略,提示安装成功,依旧没有 菜单 进入 cmd,找 ...
- Go语言介绍
Go语言简介 Go语言是谷歌2009发布的第二款开源编程语言. Go语言专门针对多处理器系统应用程序的编程进行了优化,使用Go编译的程序可以媲美C或C++代码的速度,而且更加安全.支持并行进程. Go ...
- BZOJ - 3295 三维偏序 空间转换
题意:动态逆序对,共m次删除操作,求每次操作前的逆序对个数 删除操作转换为添加操作,首先对时间a进行简单排序 然后用cdq分治处理b维,树状数组处理c维 此时需要求的是对于某有序组\((a,b,c)\ ...
- Shiro在SpringBoot中的使用
Demo代码请参考:https://github.com/roostinghawk/ShiroDemo 以下为主要代码(经过验证,测试) 1. pom.xml:引用shiro <dependen ...
- mongo嵌套查询
db.getCollection('TradeBookingRepresentation').find({uitid:'168282:20190214010009224', tradeVersion: ...
- oracle模糊查询提高效率的方法
转载:https://blog.csdn.net/weixiaohuai/article/details/83513957 https://blog.csdn.net/chihen/article/d ...
- proxyTable设置跨域
如何设置跨域 1.在config--index.js 中配置 proxyTable: { '/api': { target: 'http://www.xxx.com', //目标接口域名 change ...
- Linux工具安装配置
1.修改主机名/添加别名访问 修改/etc/sysconfig/network中的hostnameNETWORKING=yesHOSTNAME=dlserver01; 修改/etc/hosts文件 1 ...
- 2019.04.19 读书笔记 比较File.OpenRead()和File.ReadAllBytes()的差异
最近涉及到流的获取与转化,终于要还流的债了. 百度了一下,看到这样的两条回复,于是好奇心,决定看看两种写法的源码差异. 先来看看OpenRead() public static FileStream ...
- MySQL默认约束DEFAULT
当插入记录时,如果没有明确为字段赋值,则自动赋予默认值. 例如: 性别: 1. 男 2. 女 3. 保密