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

Author

eddy

Recommend

JGShining

这是一个最小生成树的模板题目

下面的代码用了Kruskal算法

#include <cstdio>
#include <vector>
#include <algorithm>
#include <cmath> using namespace std; const int N = ; struct Edge
{
int x, y;
double w;
}; struct Point
{
double x;
double y;
}; int pre[N];
Point point[N];
Edge edges[N * N / ]; int i_p, i_e, cnt;
double res; int root(int x)
{
if (x != pre[x])
{
pre[x] = root(pre[x]);
}
return pre[x];
} bool merge(int x, int y)
{
int fx = root(x);
int fy = root(y); bool ret = false; if (fx != fy)
{
pre[fx] = pre[fy];
ret = true;
--cnt;
}
return ret;
} void init(int n)
{
cnt = n;
res = ; for (int i = ; i <= n; ++i)
{
pre[i] = i;
}
} bool cmp(const Edge &a, const Edge &b)
{
return a.w < b.w;
} int main()
{
int n;
double dx, dy; while (scanf("%d", &n) != EOF)
{
init(n); i_e = i_p = ; for (int i = ; i < n; ++i)
{
scanf("%lf %lf", &dx, &dy);
point[i_p].x = dx;
point[i_p].y = dy;
++i_p;
} for (int i = ; i < n; ++i)
{
for (int j = i + ; j < n; ++j)
{
edges[i_e].x = i;
edges[i_e].y = j;
double dd = (point[i].x - point[j].x) * (point[i].x - point[j].x);
dd += (point[i].y - point[j].y) * (point[i].y - point[j].y);
edges[i_e].w = sqrt(dd);
++i_e;
}
} sort(edges, edges + i_e, cmp); //the cnt == 1 indicates that the mixnum spanning tree is builded sucessfully.
for (int i = ; i < i_e && cnt != ; ++i)
{
if (merge(edges[i].x, edges[i].y))res += edges[i].w;
} printf("%.2lf\n", res);
}
return ;
}

HDU1162-Eddy's picture(最小生成树)的更多相关文章

  1. HDU 1162 Eddy's picture (最小生成树)(java版)

    Eddy's picture 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 ——每天在线,欢迎留言谈论. 题目大意: 给你N个点,求把这N个点 ...

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

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

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

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

  4. hdu1162 Eddy's picture 基础最小生成树

    #include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...

  5. HDUOJ-----(1162)Eddy's picture(最小生成树)

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

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

    Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

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

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

  8. hdoj 1162 Eddy's picture

    并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  9. HDU 1162 Eddy's picture

    坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32 ...

  10. Eddy's picture(prime+克鲁斯卡尔)

    Eddy's picture Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Tota ...

随机推荐

  1. java实现UDP聊天---转载

    import java.io.*; import java.net.*; class Send implements Runnable { private DatagramSocket ds; pub ...

  2. 使用Erlang和Yaws开发REST式的服务

    看过那张很出名的“Apache vs. Yaws”图么?是不是在考虑你也应该使用Yaws了?这些图给人的第一印象是,Yaws在可伸缩性上具有难以置信的巨大优势,它可以扩展到80000个并行的连接,而 ...

  3. ECOS-Ecstore 伪静态规则

    .htaccess 文件 RewriteEngine On RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME ...

  4. Linux CPU 核数检查脚本

    #!/bin/bash physicalNumber=0 coreNumber=0 logicalNumber=0 HTNumber=0 logicalNumber=$(grep "proc ...

  5. 表单属性问题readonly、disabled、checked,prop的使用

    获取在匹配的元素集中的第一个元素的属性值. 随着一些内置属性的DOM元素或window对象,如果试图将删除该属性,浏览器可能会产生错误.jQuery第一次分配undefined值的属性,而忽略了浏览器 ...

  6. 安卓手机微信页面position: fixed位置错误

    今天做项目的时候发现动用position: fixed做弹窗时,用margin-top:50%这样外边距来响应式的控制位置时,在微信里打开页面的弹窗,弹窗在手机上显示的位置和实际上在手机上的位置不一样 ...

  7. 表单提交是ajax提交,PC提交没问题但是手机提交就会一直跳到error,并且也没状态码一直是0

    真是被自己蠢死了button标签他会自动提交刷新页面 <form id="baoming_from"> <p>请填写您的个人信息</p> < ...

  8. OpenStack回顾随笔(第一章)

    1. OpenStack历史    OpenStack前身是NASA和Rackspace合作的项目,2010年开源,与其他主流开源云管理系统:Citrix的Cloudstack和桉树的OpenNebu ...

  9. Linux RSS/RPS/RFS/XPS对比

    RSS适合于多队列网卡,把不同的流分散的不同的网卡多列中,至于网卡队列由哪个cpu处理还需要绑定网卡队列中断与cpu RPS:适合于单队列网卡或者虚拟网卡,把该网卡上的数据流让多个cpu处理 RFS: ...

  10. Subordinates

    Subordinates time limit per test 1 second memory limit per test 256 megabytes input standard input o ...