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

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
题意:大意就是给你n个点求出这n个点形成的最小生成树。
题解:kruskal算法解。。开始的时候将边初始化成了0,WA了两次..好久没打过生疏了。
#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(最小生成树)的更多相关文章

  1. HDU 1233(最小生成树)

    HDU 1233(最小生成树 模板) #include <iostream> #include <algorithm> #include <cstdio> usin ...

  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&#39;s picture (Kruskal算法,prim算法,最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 [题目大意] 给你n个点的坐标,让你找到联通n个点的一种方法.保证联通的线路最短,典型的最小生成 ...

  4. (最小生成树)Eddy's picture -- hdu -- 1162

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1162 Time Limit: 2000/1000 MS (Java/Others)    Memory ...

  5. hdu 1162 Eddy's picture(最小生成树算法)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...

  6. HDU 1162 Eddy's picture (最小生成树 prim)

    题目链接 Problem Description Eddy begins to like painting pictures recently ,he is sure of himself to be ...

  7. hdu 1162 Eddy's picture (最小生成树)

    Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)To ...

  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. 被relativeLayout的grivate center 折腾死了

    <?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android=&q ...

  2. error LNK2001: unresolved external symbol @__security_check_cookie

    Q:VS2005编译的静态库, 在vc++6.0中连接出现错误 error LNK2001: unresolved external symbol @__security_check_cookie@l ...

  3. 以+scheduledTimerWithTimeInterval...的方式触发的timer,在滑动页面上的列表时,timer会暂定回调,为什么?如何解决?

    这里强调一点:在主线程中以+scheduledTimerWithTimeInterval...的方式触发的timer默认是运行在NSDefaultRunLoopMode模式下的,当滑动页面上的列表时, ...

  4. 《Cracking the Coding Interview》——第14章:Java——题目3

    2014-04-26 18:59 题目:final.finally.finalize有什么区别? 解法:烂大街之java语法题.此题被多少公司考过我不知道,反正我确实遇见过一次了. 代码: // 14 ...

  5. CocosCreator设置模拟器默认横竖屏以及机型

    之前好好的横屏,今天不知道为毛突然变成竖屏了,虽然可以在点击模拟器左上角进行设置,   但是 每次启动模拟器又变成竖屏了,折腾了很久,终于找到了设置的地方,记录下:        

  6. [译]16-spring基于注解的配置元数据

    从spring2.5起spring框架开始支持java注解的配置元数据.所以除了使用xml配置文件来描述bean的装配之外,你还 可以使用基于java注解的配置元数据来完成同样的功能. spring框 ...

  7. 孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句

    孤荷凌寒自学python第十三天python代码的外部模块引用与基本赋值语句 (完整学习过程屏幕记录视频地址在文末,手写笔记在文末) 从结构化编程流行以来,代码便被分块存储,称之为模块或库. 在pyt ...

  8. Code Blocks中配置OpenGL方法

    关于在Code Blocks中配置OpenGL的方法,在网上一直没有找到实用的方法,后来在马龙师兄的帮助下终于配置成功了,现把配置过程记录如下. (1)下载codeblocks,最好是带mingw的版 ...

  9. linux服务器基本安全配置手册

    转:忘了在哪转的,直接复制到笔记里了,贴出来分享 假如你想要搭建一个Linux服务器,并且希望可以长期维护的话,就需要考虑安全性能与速度等众多因素.一份正确的linux基本安全配置手册就显得格外重要. ...

  10. 更优雅的清除浮动float方法

    上篇文章是利用 :after 方法清除浮动float(作用于浮动元素的父元素上). ; } //为了兼容性,因为ie6/7不能使用伪类,所以加上此行代码. .outer:after {;;visibi ...