#include<iostream>
#include<cstring>
#include<stdio.h>
#include<queue>
#include<cmath>
#include<algorithm>
#include<string> using namespace std; int Tree[]; int findRoot(int x) // 查找节点x所在树的根节点
{
if(Tree[x] == -)
return x;
else
{ // 路径优化
int tmp = findRoot(Tree[x]);
Tree[x] = tmp;
return tmp;
}
} struct Edge
{
double a, b;
double cost;
}edge[]; bool cmp(Edge e1, Edge e2)
{
return e1.cost < e2.cost;
} struct Cursor // 坐标结构
{
double x, y; // 点的坐标(x, y)
}cur[]; int main()
{
int n;
while(scanf("%d", &n) != EOF)
{
for(int i = ; i <= n; ++i)
{
Tree[i] = -; // 初始时,所有点都是孤立的集合
// 此处不能写为scanf("%f %f", &(cur[i].x), &(cur[i].y)); 否则将产生输入错误
scanf("%lf %lf", &(cur[i].x), &(cur[i].y));
} int k = ; // 抽象出的边的总数
// 遍历所有的点对
for(int i = ; i <= n; ++i) // 将坐标抽象为图中的顶点
{
for(int j = i+; j <= n; ++j) // 连接两点的线段抽象成边
{
edge[k].a = i; // 该边的两个顶点编号
edge[k].b = j;
edge[k].cost = sqrt((cur[j].x-cur[i].x)*(cur[j].x-cur[i].x)+(cur[j].y-cur[i].y)*(cur[j].y-cur[i].y));
++k;
}
}
sort(edge+, edge+k, cmp); // 对边按权值递增排序 // 最小生成树模板
double ans = ;
for(int i = ; i < k; ++i)
{
int ra = findRoot(edge[i].a);
int rb = findRoot(edge[i].b);
if(ra != rb)
{
Tree[ra] = rb;
ans += edge[i].cost;
}
} printf("%.2lf\n", ans);
} return ;
}

Freckles (最小生成树)的更多相关文章

  1. UVA 10034 Freckles 最小生成树

    虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...

  2. 题目1144:Freckles(最小生成树进阶)

    题目链接:http://ac.jobdu.com/problem.php?pid=1144 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...

  3. 10034 - Freckles 克鲁斯克尔最小生成树!~

    /* 10034 - Freckles 克鲁斯克尔最小生成树!- */ #include<iostream> #include<cstdio> #include<cmat ...

  4. 九度OJ 1144:Freckles(斑点) (最小生成树)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:1538 解决:760 题目描述: In an episode of the Dick Van Dyke show, little Richi ...

  5. poj 2560 Freckles

    题目连接 http://poj.org/problem?id=2560 Freckles Description In an episode of the Dick Van Dyke show, li ...

  6. 最小生成树之Kruskal

    模板题,学习一下最小生成树的Kruskal算法 对于一个连通网(连通带权图,假定每条边上的权均为大于零的实数)来说,每棵树的权(即树中所有边的权值总和)也可能不同 具有权最小的生成树称为最小生成树 生 ...

  7. freckles

    题目描述: In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back ...

  8. POJ2560 Freckles

    Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %lld & %llu Description In an epis ...

  9. 【九度OJ】题目1144:Freckles 解题报告

    [九度OJ]题目1144:Freckles 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1144 题目描述: In an ...

随机推荐

  1. hiveUDF的使用

    在此自己总结下UDF的用法 1.首先最简单的UDF(普通用java扩充函数的方式,大多数简便函数可以用这个函数来实现,返回单个字段),其加强版UDGF据说对map一类数据类型有更好兼容,实现上略复杂 ...

  2. LeetCode404Sum of Left Leaves左叶子之和

    计算给定二叉树的所有左叶子之和. 示例: 3 / \ 9    20 / \ 15   7 在这个二叉树中,有两个左叶子,分别是 9 和 15,所以返回 24 class Solution { pub ...

  3. 04.Hibernate常用的接口和类---SessionFactory类和作用

    是一个生成Session的工厂类 特点: 1.由Configuration通过加载配置文件创建该对象. SessionFactory factory = config.buildSessionFact ...

  4. css的层叠性+继承性+优先级+权重

    一.层叠性 1.含义 多种css样式叠加,浏览器处理冲突的能力. 2.原则 1>一般情况下,若出现冲突,会按照css的书写顺序,以最后的样式为准 2>样式不冲突,就不会层叠 二.css的继 ...

  5. html常用标签详解3-a标签

    a标签 1.a标签的属性 a标签属于行内元素标签,双标签<a></a> href:a标签的跳转地址 target:打开方式(_self自身:_blank:新窗口) title: ...

  6. light oj 1068 数位dp

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

  7. python图像翻转

    准备跟着台湾的一个机器学习课程好好学学python,链接在这http://speech.ee.ntu.edu.tw/~tlkagk/courses_ML16.html 该课程开始有一个作业,叫做HW0 ...

  8. Windows 10专业版激活(附激活码)

    安全密钥:BT6TH-FN8VP-6WGCK-6BM9R-MWRDB(使用有效,在物理机,虚拟机都激活了一次) 六一八期间自己买了配件第一次组了台式机,系统是在123pe下的原版win10,装好机器之 ...

  9. IntelliJ IDEA 如何在同一个窗口创建多个项目

    一.IntelliJ IDEA与Eclipse的区别   二.在同一个窗口创建多个项目 1.打开IntelliJ IDEA,点击Create New Project 2.Java Enterprise ...

  10. PAT甲级——A1040 Longest Symmetric String

    Given a string, you are supposed to output the length of the longest symmetric sub-string. For examp ...