UVa 10034 - Freckles
题目大意:给出n个点的坐标(x,y),要求用线段将n个点连接起来,求最小的线段和。
最小生成树问题,用Kruskal算法进行求解,其中用到了并查集。将所有的点连接,构成一张图,对每一条边进行编号,两点间的距离作为权重。
#include <cstdio>
#include <algorithm>
#include <cmath>
#define square(x) (x)*(x)
#define MAXN 10000
#define POINTN 100+10
using namespace std; double w[MAXN], point[POINTN][]; // the weight of edge
int u[MAXN], v[MAXN], r[MAXN], p[MAXN];
// u[i] and v[i] save the end points of the ith edge seperately, r[i] save the ith edge of non-decreasing ordered edges double distance(double x1, double y1, double x2, double y2)
{
double t = square(x2-x1) + square(y2-y1);
return sqrt(t);
} int cmp(const int i, const int j)
{
return w[i] < w[j];
} int find(int x)
{
return p[x] == x ? x : p[x] = find(p[x]);
} int main()
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
int N;
scanf("%d", &N);
bool first = true;
while (N--)
{
int n;
scanf("%d", &n);
for (int i = ; i < n; i++)
scanf("%lf%lf", &point[i][], &point[i][]);
int k = ; // the size of edge
for (int i = ; i < n; i++)
for (int j = i+; j < n; j++)
{
w[k] = distance(point[i][], point[i][], point[j][], point[j][]);
u[k] = i;
v[k] = j;
k++;
}
double ans = ;
for (int i = ; i < n; i++) p[i] = i;
for (int i = ; i < k; i++) r[i] = i;
sort(r, r+k, cmp); // sort the edge
for (int i = ; i < k; i++)
{
int e = r[i];
int x = find(u[e]);
int y = find(v[e]);
if (x != y)
{
ans += w[e];
p[x] = y;
}
}
if (first) first = false;
else printf("\n");
printf("%.2lf\n", ans);
}
return ;
}
代码中用到了间接排序——排序的关键字是对象的“代号”,而不是对象本身,将排序后第i小的边的序号保存在r[i]中。这样对象本身的数据就不需要移动了,比如这道题的u[i]和v[i],自己也就能理解到这点了...代码是照抄别人的,解释还是照抄别人的...人工版的Ctrl-C & Ctrl-V 啊...
UVa 10034 - Freckles的更多相关文章
- UVA 10034 Freckles 最小生成树
虽然是道普通的最小生成树题目,可还是中间出了不少问题,暴露的一个问题是不够细心,不够熟练.所以这篇博客就当记录一下bug吧. 代码一:kruskal #include<stdio.h> # ...
- uva 10034 Freckles (kruskal||prim)
题目上仅仅给的坐标,没有给出来边的长度,不管是prim算法还是kruskal算法我们都须要知道边的长度来操作. 这道题是浮点数,也没啥大的差别,处理一下就能够了. 有关这两个算法的介绍前面我已经写过了 ...
- 10034 - Freckles 克鲁斯克尔最小生成树!~
/* 10034 - Freckles 克鲁斯克尔最小生成树!- */ #include<iostream> #include<cstdio> #include<cmat ...
- uva 10034 Problem A: Freckles
http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...
- uva 10034
计算所有点之间的权值 然后就是最小生成树 #include<cstring> #include<string> #include<cstdio> #includ ...
- UVA题目分类
题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...
- (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO
http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...
- ACM训练计划step 1 [非原创]
(Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...
- 算法竞赛入门经典+挑战编程+USACO
下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...
随机推荐
- Android SharedPreferences存图片,转码解码图片
保存图片 首先创建ByteArrayStream对象,然后用BitmapFactroy把图片加载进来,然后compress压缩图片到流, 然后toByteArray()就行了 public voi ...
- android Spinner 续
android Spinner 续 动态增删Spinner中的数据项 public class EX04_09 extends Activity{ private static final Stri ...
- ZOJ 3699 Dakar Rally(贪心)
这是一道贪心题,他的贪心思想很容易想明白,我们保证油箱里的油始终是最便宜的我们最后的花费就能是最少的.实现方法就是:比如现在在i点,我们看邮箱满载能最远到达哪里,不妨设最远到达j,(j >= i ...
- JS面向对象基础
以往写代码仅仅是为了实现特定的功能,后期维护或别人重用的时候,困难很大. Javascript作为完全面向对象的语言,要写出最优的代码,需要理解对象是如何工作的. 1. 对象是javasc ...
- java实现webservice
第一步:web工程--新建server-config.wsdd 文件与web.xml同级 其内容如下 <?xml version="1.0" encoding="U ...
- word 书签排序算法
直接上代码 /// <summary> /// 通过计算插入引文的位置格式化合适的引文序号 /// </summary> /// <returns></ret ...
- jQuery方式事件冒泡的2个方法
方式1:通过 event.stopPropagation(); $("div").click(function (event) { slide.call(this); event ...
- Python核心编程第二版(中文).pdf 目录整理
python核心编程目录 Chapter1:欢迎来到python世界!-页码:7 1.1什么是python 1.2起源 :罗萨姆1989底创建python 1.3特点 1.3.1高级 1.3.2面向 ...
- 照着例子学习 protobuf-lua
参考文章:cocos2dx使用lua和protobuf 首先得下载protobuf-gen-lua的插件,插件Git地址在此. 下载完之后进入到protoc-gen-lua\plugin这个目录,并在 ...
- 简单的字符串比较题 POJ 1936
Description You have devised a new encryption technique which encodes a message by inserting between ...