hdoj 1162 Eddy's picture
并查集+最小生成树
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7669 Accepted Submission(s):
3882
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.
#include<stdio.h>
#include<algorithm>
#include<math.h>
using namespace std;
int set[110];
struct record//注意此题要用实型
{
double a;
double b;
double ju;//两点之间距离
}s[10000];
int find(int fa)
{
int ch=fa;
int t;
while(fa!=set[fa])
fa=set[fa];
while(ch!=fa)
{
t=set[fa];
set[ch]=fa;
ch=t;
}
return fa;
}
void mix(int x,int y)
{
int fx,fy;
fx=find(x);
fy=find(y);
if(fx!=fy)
set[fx]=fy;
}
double dis(double x1,double y1,double x2,double y2)
{
return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); //计算两点之间距离
}
bool cmp(record c,record d)
{
return c.ju<d.ju;
}
int main()
{
int m,j,i,point;
double sum;
double a[110];//这两个数组用来储存点的坐标,a数组储存横标
double b[110];//b数组储存纵标
while(scanf("%d",&point)!=EOF)
{
for(i=0;i<=point;i++)
set[i]=i;
for(i=1;i<=point;i++)
{
scanf("%lf%lf",&a[i],&b[i]);
}
m=0;
for(i=1;i<=point-1;i++) //此循环求任意两点之间距离
{ //并记录下此两点位置
for(j=i+1;j<=point;j++)
{
s[m].ju=dis(a[i],b[i],a[j],b[j]);
s[m].a=i;
s[m].b=j;
m++;
}
}
sort(s,s+m,cmp);
sum=0;
for(i=0;i<m;i++)
{
if(find(s[i].a)!=find(s[i].b))
{
mix(s[i].a,s[i].b);
sum+=s[i].ju;
}
}
printf("%.2lf\n",sum);
}
return 0;
}
hdoj 1162 Eddy's picture的更多相关文章
- hdu 1162 Eddy's picture (Kruskal 算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- 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
坐标之间的距离的方法,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
Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to become ...
随机推荐
- highcharts 柱状图
<!doctype html> <html lang="en"> <head> <script type="text/javas ...
- [itint5]单词游戏
http://www.itint5.com/oj/#36 此题在数据大些,而且全是A的情况下会超时(因为要匹配到很后面才false).通过利用数组本身作为visited标示,而且使用string引用, ...
- MyBatis的CRUD操作
MyBatis的两个主要配置文件 mytatis.xml:放在src目录下,常见的配置如下 <?xml version="1.0" encoding="UTF-8& ...
- python 操作符笔记:
操作符 描述 x if y else z 三元描述(2.5新加)(类似于c中的 x?y:z x or y 或 x and y 与 not x 非 x!=y ,x is y 序列成员测试 x | y 位 ...
- Struts中的 saveToken的方法
Struts中的 saveToken的方法 saveToken防止2次提交的问题 struts有自带的Token(令牌)的机制来解决重复提交(包括后退,刷新等).举例: 假设:假设有一个新增用 ...
- Hibernate征途(五)之继承映射和组件映射
之所以把这两种映射放到一起说,是因为二者都是以复用为目的,减少了代码和配置量,这是相同点:二者之间的不同点类似继承和实现的区别:继承的类是一个事物的抽象,而实现的接口仅仅是功能的抽象. 继承映射 如上 ...
- 中国海洋大学第四届朗讯杯高级组 Cash Cow(模拟)
题目:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2721 题意: 给定n个左标,跟那n个坐标 ...
- 生产环境上shell的解读
一直以来对shell都不是很熟悉,只停留在基本的linux上操作上,这周因为定位问题接触到了生产环境上的脚本,因此作为引子学习一下.很多命令只是点到,等真正需要独立完成的时候再去学习. #!/bin/ ...
- java高并发,如何解决,什么方式解决
之前我将高并发的解决方法误认为是线程或者是队列可以解决,因为高并发的时候是有很多用户在访问,导致出现系统数据不正确.丢失数据现象,所以想到 的是用队列解决,其实队列解决的方式也可以处理,比如我们在竞拍 ...
- 正则化(Regularization)
正则化(Regularization)是机器学习中抑制过拟合问题的常用算法,常用的正则化方法是在损失函数(Cost Function)中添加一个系数的\(l1 - norm\)或\(l2 - norm ...