hdu 1162(最小生成树)
Eddy's picture
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 8797 Accepted Submission(s): 4476
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?
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.
program prints a single real number to two decimal places: the minimum
total length of ink lines that can connect all the points.
1.0 1.0
2.0 2.0
2.0 4.0
#include <stdio.h>
#include <iostream>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std; const int N = ;
struct Point {
double x,y;
}p[N];
struct Edge{
int s,e;
double len;
}edge[N*(N-)/];
int father[N],n;
double dis(Point a,Point b){
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int _find(int x){
if(x==father[x]) return x;
return _find(father[x]);
}
int cmp(Edge a,Edge b){
return a.len<b.len;
}
double kruskal(int m){
sort(edge+,edge+m+,cmp);
double cost=;
for(int i=;i<=m;i++){
int x = _find(edge[i].s);
int y = _find(edge[i].e);
if(x!=y){
father[x] = y;
cost +=edge[i].len;
}
}
return cost;
}
int main(){
while(scanf("%d",&n)!=EOF){
for(int i=;i<n;i++) father[i] = i;
for(int i=;i<n;i++){
scanf("%lf%lf",&p[i].x,&p[i].y);
}
int m =; ///边的数量
for(int i=;i<n;i++){
for(int j=i+;j<n;j++){ ///这里是i+1开始,从0开始会多出很多边
edge[m].s =i;
edge[m].e = j;
edge[m++].len = dis(p[i],p[j]);
}
}
m--; ///记得
printf("%.2lf\n",kruskal(m));
}
}
hdu 1162(最小生成树)的更多相关文章
- HDU 1233(最小生成树)
HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...
- 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 (Kruskal算法,prim算法,最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成 ...
- (最小生成树)Eddy's picture -- hdu -- 1162
链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 Time Limit: 2000/1000 MS (Java/Others) Memory ...
- 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 (最小生成树 prim)
题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...
- 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 (最小生成树 普里姆 )
题目链接 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 ...
随机推荐
- adb 显示手机分辨率
adb shell dumpsys window | grep "ShownFrame" | head -n 1 adb shell dwm size 当然,作为一个Android ...
- 绑定host域名 修改手机hosts域名
windows: C:\Windows\System32\drivers\etc\hosts # 在这儿输入你需要绑定的 hosts 116.31.72.421129 bro-user.flyme.c ...
- hadoop中namenode发生故障的处理方法
Namenode 故障后,可以采用如下两种方法恢复数据: 方法一:将 SecondaryNameNode 中数据拷贝到 namenode 存储数据的目录: 方法 二: 使用 -importCheckp ...
- 树&二叉树&哈夫曼树
1.树 需要注意的两点:n(n>=0)表示结点的个数,m表示子树的个数 (1)n>0时,树的根节点是唯一的. (2)m>0时,子树的个数没有限制. 结点的度和树的度 (1)结点的度是 ...
- Windows Server 2012 R2有哪些存储监控工具
[TechTarget中国原创] 大多数Windows管理员都知道,没有一种单一的方法可以用来监控存储或磁盘错误.虽然市场上有无数的管理工具可供你选择,但由于政策和规程的原因,企业之间的选择不尽相同. ...
- 【vim环境配置】在centos6.4上配置vim的一些零碎记录
上一篇日志已经step by step地实录了如何在本机mac上配置vim开发环境已经各种插件. 有了一定经验之后,开始在实验室远程server上centos6.4的环境下配置vim环境. 这台机器是 ...
- python+selenium安装指导
一. 安装python 1.Window 平台安装 Python 以下为在 Window 平台上安装 Python 的简单步骤: 打开 WEB 浏览器访问https://www.python ...
- 条件随机场(Conditional random field)
条件随机场真是把我给折磨坏了啊,本以为一本小小的<统计学习方法>攻坚剩下最后一章,心情还是十分愉悦的,打算一口气把它看完,结果真正啃起来真是无比的艰难啊,每一句对我都好像是天书一般,怎么这 ...
- 【多线程学习(1)】创建java多线程
1)java多线程的创建方式有三种: 1.继承Thread类 2.实现Runnable接口 3.实现Callable接口 第一种: //继承Thread类 class ExtendsThread ex ...
- IPv4地址分类及特征
IPv4地址分类及特征 IP地址后斜杠和数字代表的意思 其中有这样一个IP地址的格式:IP/数字,例如:111.222.111.222/24 这种格式平时在内网中用的不多,所以一下子看不懂,最后查了资 ...