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 ...
随机推荐
- 利用java实现一个简单的链表结构
*博客搬家:初版发布于 2014/07/04 定义: 所谓链表就是指在某节点存储数据的过程中还要有一个属性用来指向下一个链表节点,这样的数据存储方式叫做链表 链表优缺点: 优点:易于存储和删除 缺点: ...
- [原创]markdown语法学习(commonmark)
CommonMark是markdown的解析器,以下是部分学习链接: commonmark规范 https://spec.commonmark.org/ commonmark帮助 https://co ...
- MySQL使用UNIQUE实现数据不重复插入
unique列在一个UNIQUE键上插入包含重复值的记录时,我们可以控制MySQL如何处理这种情况:使用IGNORE关键字或者ON DUPLICATE KEY UPDATE子句跳过INSERT.中断操 ...
- Jquery 在ios上事件委托失效
点击通过js遍历出来的列表,跳转页面.点击事件委托在document上, 像这样: $(document).on("click",".nav",function ...
- python安装方法
1.下载Python安装包 2.配置环境变量 path=%path%;C:\Python27 3.安装pip 默认已安装 4.配置pip环境变量 path=%path%;C:\Python27\Scr ...
- nginx常用配置3
## 六.浏览器本地缓存配置 语法:expires 60 s|m|h|d ```动静分离效果: server { listen 80; server_name localhost; location ...
- SPRING中的线程池ThreadPoolTaskExecutor(转)
转自:https://blog.csdn.net/zhanglongfei_test/article/details/51888433 一.初始化 1,直接调用 ThreadPoolTaskExecu ...
- 关于table-layout的用法
定义:tableLayout 属性用来显示表格单元格.行.列的算法规则. 自动表格布局:auto(默认) 在自动表格布局中,列的宽度是由列单元格中没有折行的最宽的内容设定的. 此算法有时会较慢,这是由 ...
- Python机器学习库sklearn的安装
Python机器学习库sklearn的安装 scikit-learn是Python的一个开源机器学习模块,它建立在NumPy,SciPy和matplotlib模块之上能够为用户提供各种机器学习算法接口 ...
- iostat命令——监控系统设备的IO负载情况
iostat命令的安装 #yum install sysstat iostat常见选项 -t 输出数据时打印搜集数据的时间 -m 输出的数据以MB为单位 -d 显示磁盘的统计信息 # iost ...