题目大意:给出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的更多相关文章

  1. UVA 10034 Freckles 最小生成树

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

  2. uva 10034 Freckles (kruskal||prim)

    题目上仅仅给的坐标,没有给出来边的长度,不管是prim算法还是kruskal算法我们都须要知道边的长度来操作. 这道题是浮点数,也没啥大的差别,处理一下就能够了. 有关这两个算法的介绍前面我已经写过了 ...

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

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

  4. uva 10034 Problem A: Freckles

    http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&p ...

  5. uva 10034

    计算所有点之间的权值   然后就是最小生成树 #include<cstring> #include<string> #include<cstdio> #includ ...

  6. UVA题目分类

    题目 Volume 0. Getting Started 开始10055 - Hashmat the Brave Warrior 10071 - Back to High School Physics ...

  7. (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO

    http://www.cnblogs.com/sxiszero/p/3618737.html 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年 ...

  8. ACM训练计划step 1 [非原创]

    (Step1-500题)UVaOJ+算法竞赛入门经典+挑战编程+USACO 下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成 ...

  9. 算法竞赛入门经典+挑战编程+USACO

    下面给出的题目共计560道,去掉重复的也有近500题,作为ACMer Training Step1,用1年到1年半年时间完成.打牢基础,厚积薄发. 一.UVaOJ http://uva.onlinej ...

随机推荐

  1. PowerDesigner中逆向工程将数据库中comment赋值到name

    '------------------------------------------------------------ ' '脚本功能: ' PowerDesigner中逆向工程完成后,将数据库中 ...

  2. 老问题:Android子线程中更新UI的3种方法

    在Android项目中经常有碰到这样的问题,在子线程中完成耗时操作之后要更新UI,下面就自己经历的一些项目总结一下更新的方法: 方法一:用Handler 1.主线程中定义Handler: Handle ...

  3. 3个普通IO识别22个按键试验(转)

    源:http://www.amobbs.com/forum.php?mod=viewthread&tid=2243715 吸取各位前辈的经验,将之前二极管用量多的问题优化一下,目前不用二极管能 ...

  4. Net 自定义Excel模板导出数据

    转载自:http://www.cnblogs.com/jbps/p/3549671.html?utm_source=tuicool&utm_medium=referral 1 using Sy ...

  5. 【WiFi密码破解详细图文教程】ZOL仅此一份 详细介绍从CDlinux U盘启动到设置扫描破解-破解软件论坛-ZOL中关村在线

    body { font-family: Microsoft YaHei UI,"Microsoft YaHei", Georgia,Helvetica,Arial,sans-ser ...

  6. android异步加载AsyncTask

    http://blog.csdn.net/abc5382334/article/details/17097633 http://keeponmoving.iteye.com/blog/1515611 ...

  7. zepto学习之路--源代码提取

    最近在看zepto的源代码,把一些有用的函数摘出来,看看zepto是怎么实现的,自己做的时候也可以用.说实话,zepto的实现有一些看起来还是很晦涩的,可能是自己的水平不够,看不透作者的真正的意图. ...

  8. js 获取n天前的时间

    <script type="text/javascript"> var strdate = new Date(); var begindate = strdate.ge ...

  9. 用apache配置多个tomcat webapp

    The Apache HTTP Server Project is an effort to develop and maintain an open-source HTTP server for m ...

  10. 前端开发chrome与fireFox浏览器都使用

    chrome查看元素的样式时,显示的很方便和准确,方便开发快速辨别结构. 而fireFox在css3上,我发现好像比chrome支持得更全面.