HDU1162-Eddy's picture(最小生成树)
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(最小生成树)的更多相关文章
- 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 (最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu 1162 Eddy's picture(最小生成树算法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1162 Eddy's picture Time Limit: 2000/1000 MS (Java/Ot ...
- hdu1162 Eddy's picture 基础最小生成树
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm> # ...
- HDUOJ-----(1162)Eddy's picture(最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)To ...
- hdu Eddy's picture (最小生成树)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
- Eddy's picture(最小生成树)
Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Tota ...
- hdoj 1162 Eddy's picture
并查集+最小生成树 Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java ...
- HDU 1162 Eddy's picture
坐标之间的距离的方法,prim算法模板. Eddy's picture Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32 ...
- Eddy's picture(prime+克鲁斯卡尔)
Eddy's picture Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Tota ...
随机推荐
- CSS,height:auto和height:100%有什么区别?
auto是随内容的高度而撑开的.100%是根据父级元素的高度来决定的.例如:<div style="height:100px;width:200px;"> <di ...
- Lynis 2.2.0 :面向Linux系统的安全审查和扫描工具
Lynis是一款功能非常强大的开源审查工具,面向类似Unix/Linux的操作系统.它可以扫描系统,查找安全信息.一般的系统信息.已安装软件及可用软件信息.配置错误.安全问题.没有设密码的用户帐户.错 ...
- 局域网iis添加主机头
局域网上用主机头访问不像外网,直接设置主机头就行了, 在局域网设置了主机头还要修改host文件 打开host快捷的方法: win7在win+r运行里面运行C:\Windows\System32\dri ...
- java.util.Iterator
public interface Iterator<E>: 对 collection 进行迭代的迭代器. 方法摘要: boolean hasNext() 如果仍有元素可以迭代,则返回 tr ...
- C++中构造函数或析构函数定义为private
转自:http://www.blogjava.net/fhtdy2004/archive/2009/05/30/278971.html 很多情况下要求当前的程序中只有一个object.例如一个程序只有 ...
- redis数据类型:Strings
String是最简单的数据类型,一个key对应一个value,string类型是二进制安全的,redis的String可以包含任何数据, 比如jpg图片或者系列化的对象. Set方法: 设置key对应 ...
- php 过滤emoji表情
function yz_expression() { foreach ($_POST as $key => &$value) { $value = preg_replace_callba ...
- Objective-C 2.0属性(Property)介绍
通常在声明一些成员变量时会看到如下声明方式: @property (参数1,参数2) 类型 名字: 这里我们主要分析在括号中放入的参数,主要有以下三种: setter/getter方法(assign/ ...
- Android依赖注入:Google Guice on Android的使用及相关资源
本文转自:http://blog.csdn.net/sangming/article/details/8878104 RoboGuice 使用谷歌自己的Guice库,给Android带来了简单和易用的 ...
- C语言隐式强制类型转换
今天又被精度问题困扰,把最基本的东西忘了. int n = 5; int cnt = 5.5; double sum = (n-cnt); 运算完后sum是 -0.5.不知道什么时候n转换成doub ...