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. 虚拟机装的XP,无法上网,因为没有安装网卡驱动,怎么解决

    本帖可以解决2个问题: 问题1. 虚拟机装的XP,无法上网,因为没有安装网卡驱动,怎么解决 问题2. 怎么往Oracle VM VirtualBox里传输文件??????? 步骤: 一.在虚拟机的左上 ...

  2. loadunner使用socket协议来实现多客户端连接同一服务器脚本(使用到IP欺骗技术)

    第一部分: #include "lrs.h" vuser_init(){ lrs_startup(257); return 0;} 第二部分: Action(){ char *Re ...

  3. FCFS

    (First Come First Served) 按照作业进入系统的先后次序来挑选作业,先进入系统的作业优先被挑选. FCFS算法的优缺点: 算法容易实现.但效率不高,只顾及作业等候时间,没考虑作业 ...

  4. akka 入门

    http://blog.csdn.net/thkhxm/article/details/40182835 1.首先安装akka的相关包-- http://akka.io/downloads/2.导入依 ...

  5. Infix to postfix conversion 中缀表达式转换为后缀表达式

    Conversion Algorithm 1.操作符栈压入"#": 2.依次读入表达式的每个单词: 3.如果是操作数则压入操作数栈: 4.如果是操作符,则将操作符栈顶元素与要读入的 ...

  6. 递归与DP

    每一个递归问题都可以改成DP来做...只不过DP会浪费一些空间罢了..DP只是把之前的结果存起来以防再算一遍罢了.....

  7. token 入门教程

    下载 先去Apache下载一个tomcat 启动 进到安装目录的bin目录运行startup.bat,D:\apache-tomcat-8.0.33\bin (如果双击startup.bat一会自动关 ...

  8. Entity Framework 学习高级篇2—改善EF代码的方法(下)

    ,IQueryable<Customers>>( (database) => database.Customers.Where(c => c.City == " ...

  9. php.ini与php-fpm.conf配置文件的区别

    php-fpm.conf是PHP-FPM特有的配置文件 php.ini是所以php模式中必须的配置文件 两者的区别是,php-fpm.conf是PHP-FPM进程管理器的配置文件,php.ini是PH ...

  10. Windows安装Composer出现【Composer Security Warning】警告

    提示信息: The openssl extension is missing from the PHP version you specified.This means that secure HTT ...