Swordfish
zoj1203:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203
题意:给定平面上N个城市的位置,计算连接这N个城市所需线路长度总和的最小值
题解:每两个点之间建一条边,然后求这一棵最小生成树。
#include<cstring>
#include<cstdio>
#include<algorithm>
#include<iostream>
#include<cmath>
using namespace std;
int n,pa[],cnt,num;//分别记录点的个数 ,并查集,边的个数,以及处理的边的个数
struct Node{
double x;
double y;
}node[];//记录每个点
struct Edge{
int u;
int v;
double w;
bool operator<(const Edge &a)const{
return w<a.w;}
}edge[];//记录每一条边,以及定义排序规则
void UFset(){
for(int i=;i<=n;i++)
pa[i]=-;
}//初始化
int Find(int x){//查找
int s;
for(s=x;pa[s]>=;s=pa[s]);
while(s!=x){
int temp=pa[x];
pa[x]=s;
x=temp;
}//路径压缩,便于后面的查找
return s;
}
void Union(int R1,int R2){//合并,把个数的集合作为子树加到另一棵树上
int r1=Find(R1);
int r2=Find(R2);
int temp=pa[r1]+pa[r2];
if(pa[r1]>pa[r2]){
pa[r1]=r2;
pa[r2]=temp;
}
else{
pa[r2]=r1;
pa[r1]=temp;
}
}
double dist(Node a,Node b){//计算每两个点之间距离
return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
double kruska(){//克鲁斯卡尔算法
UFset();//初始化
num=;
double sum=0.0;
for(int i=;i<cnt;i++){
int u=edge[i].u;
int v=edge[i].v;
if(Find(u)!=Find(v)){
sum+=edge[i].w;
num++;
Union(u,v);
}
if(num>=n-)break;
}
return sum;
}
int main(){
int t=;
while(~scanf("%d",&n)&&n){
cnt=;//初始化
for(int i=;i<=n;i++)
scanf("%lf%lf",&node[i].x,&node[i].y);//存边
for(int i=;i<=n;i++){
for(int j=i+;j<=n;j++){
edge[cnt].u=i;
edge[cnt].v=j;
edge[cnt++].w=dist(node[i],node[j]);
}
}//建边,每两个点之间建一条边
sort(edge,edge+cnt);//排序
if(t)printf("\n");
printf("Case #%d:\n",++t);
printf("The minimal distance is: %.2f\n",kruska());
}
}
Swordfish的更多相关文章
- Kruskal算法 Swordfish
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=203 Swordfish Time Limit: 2 Seconds ...
- ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...
- ZOJ 1203 Swordfish
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- ZOJ 1203 Swordfish(Prim算法求解MST)
题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...
- zoj 1203 Swordfish prim算法
#include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...
- ZOJ 1203 Swordfish MST
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203 大意: 给出一些点,求MST 把这几天的MST一口气发上来. kru ...
- 比较详细Python正则表达式操作指南(re使用)
比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...
- Python天天美味(15) - Python正则表达式操作指南(re使用)(转)
http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...
- POJ题目细究
acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP: 1011 NTA 简单题 1013 Great Equipment 简单题 102 ...
随机推荐
- Hibernate的游离态与持久态转换
在Hibernate中,一个PO可能经过长时间的操作,session已过时关闭,此时PO已经是一个游离态的对象,这时要转换为持久战态,有下面几种方法: 1.session.saveOrUpdate(o ...
- SqlServer快捷键整理
一:sp_helptext 对象 1.Ctrl+D 表格显示 2.Ctrl+T 文本显示(含格式)
- svn命令的使用
1.检出svn co http://路径(目录或文件的全路径) [本地目录全路径] --username 用户名 --password 密码svn co svn://路径(目录或文件的全路径) ...
- windows10UWP:如何判断一个文件或者文件夹是否存在?
使用 StorageFolder.TryGetItemAsync 方法,尝试按名称获取文件或文件夹,不需将错误捕捉逻辑添加到代码(就像使用 StorageFolder.GetItemAsync 一样) ...
- IDL计算儒略日
遥感数据还有一些文章中使用数据的时候,经常使用儒略日(Julian day),即计算该天是一年中的第几天.正好有时间,就用IDL写了段儿小代码,方便使用. ;+ ; :Author: caoz ...
- 调试exynos4412—ARM嵌入式Linux—LEDS/GPIO驱动之二
/** ****************************************************************************** * @author 暴走的小 ...
- 10.30 afternoon
P76竞赛时间: ????年??月??日??:??-??:?? 题目名称 他 她 它 名称 he she it 输入 he.in she.in it.in 输出 he.out she.out it.o ...
- 用WebStorm调试本地html(含嵌入的javascript).
题外话: 以前很少能调试,甚至以为不能调试(好笨),后来我看了一本叫做<<Learning Three.js>>的一本书后,里面推荐有几种javascript的编辑工具,都蛮好 ...
- LENGTH和LENGTHB函数,substrb截取也是同一个道理。
oracle 利用 LENGTH和LENGTHB函数区分中英文(2009-02-07 10:49:29) 转载▼ 标签: it 分类: oracle 前一段时间,我一朋友问我怎么得出这个字符串是中文还 ...
- oracle备份表
oracle与sql单表备份的区别 ( oracle中备份表: create table 备份表名 as select * from 原表 sql server中备份表: select * i ...