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 ...
随机推荐
- 【简单学习shell】iptables命令实用
构造设备离线iptables命令iptables -I INPUT -p all -s 10.71.115.159 -j DROP 断链iptables -I INPUT -p all -s 10.7 ...
- python基础(二)- 字符串
一.运算符 1.结果是值: 算术运算 赋值运算 2.结果是布尔值: 比较运算:> < == >= <= != <> 逻辑运算:and or ...
- strace -o /tmp/dhc$$ dhclient -d eth2
http://askubuntu.com/questions/5187/why-is-dhclient-saying-siocsifaddr-permission-denied ip link add ...
- 正则表达式:reg.test is not a function
正则中 比如 var reg = "/^[0-9]$/" 会报 reg.test is not a function 如果 var reg = /^[0-9]$/ 就不会有错 因为 ...
- 2.2 Xpath-helper (chrome插件) 爬虫、网页分析解析辅助工具
1. Xpath-helper下载 可以直接在chrome浏览器中的扩展程序搜索 Xpath-helper进行添加 也可以直接在http://www.chromein.com/crx_11654.ht ...
- Microsoft SqlSever 数据库--软谋1
百度百科--Microsoft SqlSever SQL是英文Structured Query Language的缩写,意思为结构化查询语言.SQL语言的主要功能就是同各种数据库建立联系,进行沟通.按 ...
- java 图形界面 Socket编程
一.使用图形界面实现客户端服务器端的通信: 上代码: 服务器端代码: package cn.MyNET; import java.io.*; import java.net.*; import jav ...
- Android中调用系统的相机和图库获取图片
//--------我的主布局文件------很简单---------------------------------<LinearLayout xmlns:android="http ...
- 【第四篇】androidEventbus源代码阅读和分析
1,分析androidEventbus的注册源代码: 我们在使用androidEventbus的第一步是注册eventbus,如下代码: EventBus.getDefault().register( ...
- mongodb 数据库导入.cvs文件时某些字段类型变成NumberLong的解决办法
在mongodb中导入数据时,会在数据库中生成字段记录为NumberLong的数据,可以使用以下方式将其转换为String db.Account.find().forEach( function(it ...