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. 生成N个不重复的随机数(转)

    有25幅作品拿去投票,一次投票需要选16幅,单个作品一次投票只能选择一次.前面有个程序员捅了漏子,忘了把投票入库,有200个用户产生的投票序列为空.那么你会如何填补这个漏子? 当然向上级反映情况.但是 ...

  2. LDAP7卸载

    3 Uninstalling Directory Server Enterprise Edition This chapter provides instructions for uninstalli ...

  3. windows下jboss启动、配置、访问

    window 下的jboss启动.配置.访问 1.进入jboss\server\default\deploy\jboss-web.deployer 执行run命令 2.jboss访问地址:http:/ ...

  4. 什么是html,什么是php

    学了这么长时间的网站建设,好像对这两个概念说以来还是语无伦次的,所以就来写一写了.html是什么呢,官方解释:超文本标记语言,超文本就是指页面可以包含图片,连接等非文字元素.超文本标记语言也是一种规范 ...

  5. mysql数据库优化日志(更)-howyue

    1)记一次首页查询优化 优化前: 优化后: 主要优化: 1.select查询只查询需要字段: 2.where条件字段添加索引:

  6. 解决UITabeleViewCell的分割线不能铺满问题

    -(void)viewDidLayoutSubviews { if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)] ...

  7. 【转】 IOS,objective_C中用@interface和 @property 方式声明变量的区别

    原文: http://blog.csdn.net/ganlijianstyle/article/details/7924446 1.在  @interface :NSObject{} 的括号中,当然N ...

  8. MATLAB中mexFunction函数的接口规范

    MEX文件的调用极为方便,其调用方式与MATALAB的内建函数完全相同,只需要在命令窗口内输入对应的文件名称即可. C语言MEX程序代码文件有计算子例程(Computational routine)和 ...

  9. qsort 函数用法

    用 法: void qsort(void *base, int nelem, int width, int (*fcmp)(const void *,const void *)); 各参数: 1 待排 ...

  10. ccui.ScrollView 扩展

    大多数游戏都有背包这个东西. 道具列表通常用 ScrollView 来实现. 这个ScrollView内部有一个Layout, 滑动都是由移动这个Layout来实现. 道具摆放通常从上往下, 从左到右 ...