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的更多相关文章

  1. Kruskal算法 Swordfish

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=203 Swordfish Time Limit: 2 Seconds      ...

  2. ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法

    主题链接:problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼 Swordfish Time Limit: 2 ...

  3. ZOJ 1203 Swordfish

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  4. ZOJ 1203 Swordfish(Prim算法求解MST)

    题目: There exists a world within our world A world beneath what we call cyberspace. A world protected ...

  5. zoj 1203 Swordfish prim算法

    #include "stdio.h". #include <iostream> #include<math.h> using namespace std; ...

  6. ZOJ 1203 Swordfish MST

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1203 大意: 给出一些点,求MST 把这几天的MST一口气发上来. kru ...

  7. 比较详细Python正则表达式操作指南(re使用)

    比较详细Python正则表达式操作指南(re使用) Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式.Python 1.5之前版本则是通过 regex 模块提供 E ...

  8. Python天天美味(15) - Python正则表达式操作指南(re使用)(转)

    http://www.cnblogs.com/coderzh/archive/2008/05/06/1185755.html 简介 Python 自1.5版本起增加了re 模块,它提供 Perl 风格 ...

  9. POJ题目细究

    acm之pku题目分类 对ACM有兴趣的同学们可以看看 DP:  1011   NTA                 简单题  1013   Great Equipment     简单题  102 ...

随机推荐

  1. SDUT2608(Alice and Bob)

    题目描述 Alice and Bob like playing games very much.Today, they introduce a new game. There is a polynom ...

  2. Android GridView 一行显示数据(包括图片和文本),解决的办法是计算数据占该行的宽度是多少

    最近在做图片的浏览功能,开始是使用Gallery做,但是,达不到我想要的效果,关于使用Gallery显示缩略图的缺点和优点,不在详述了.以下是一个完整的Demo代码,注意我的模拟器是640*960. ...

  3. Java 自带MD5加密 Demo

    package demo; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; pub ...

  4. 你的网站为什么会慢?——用YSlow为你的网站提速

    在前面的文章我翻译的文章中分别从内容.服务器.JavaScript和CSS.图片.Coockies和移动应用等几个方面总结了34条提高网站性能的黄金守则,但是这些守则中,有一些是不常用到的,若非有实力 ...

  5. 安装zookeeper时候,可以查看进程启动,但是状态显示报错:Error contacting service. It is probably not running

    安装zookeeper-3.3.2的时候,启动正常没报错,但zkServer.sh status查看状态的时候却出现错误,如下: JMX enabled by defaultUsing config: ...

  6. 表达式:使用API创建表达式树(3)

    一.DebugInfoExpression:发出或清除调试信息的序列点. 这允许调试器在调试时突出显示正确的源代码. static void Main(string[] args) { var asm ...

  7. (转)基于PHP的cURL快速入门

    1. 原文:基于PHP的cURL快速入门 英文原文:http://net.tutsplus.com/tutorial ... for-mastering-curl/ 原文作者:Burak Guzel ...

  8. 关于百度 UEditor的使用

    1.文件路径的配置: 注意:在页面上需要指定editor文件所在的路径,否则报错 后面有时间,再说说 kindEditor和  bootstrap3的summernote的  Editor,  fck ...

  9. Typescript 团队合作的利器

    前言 在介绍Typescript 之前,我需要隆重介绍一个人: 安德斯·海尔斯伯格(Anders Hejlsberg,1960.12~),丹麦人,Turbo Pascal编译器的主要作者,Delphi ...

  10. java 迭代器iterator

    对于如ArrayList<E>类的数据,常用iterator遍历. ArrayList<String> list = new ArrayList<String>() ...