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 ...
随机推荐
- android 33 对话框控件
对话框控件:最多3个按钮. mainActivity.java package com.sxt.day05_09; import android.app.Activity; import androi ...
- Realm Configuration HOW-TO--官方
来源:https://secure.gettinglegaldone.com/docs/realm-howto.html Quick Start This document describes how ...
- Swift: Initialization-1
初始化的过程包括为每一个存储属性设置一个初始值和其他步骤.通过定义构造函数来实现初始化的过程,跟oc的初始化函数不同,Swift的构造函数不返回一个值.它们的主要角色是确保一个类型的实例在初次使用前被 ...
- app 的内存优化
这篇文章是笔者在开发App过程中发现的一些内存问题, 然后学习了YYKit框架时候也发现了图片的缓存处理 (YYKit 作者联系了我, 说明了YYKit重写imageNamed:的目的不是为了内存管理 ...
- ArcEngine:栅格分级渲染
ArcEngine对矢量数据进行风格化实在是得心应手,同样的对于栅格图像也能进行风格化!以前没接触过,今天正好需要,做出了栅格图像的渲染!下面实现的思路: 1.定义渲染的一系列接口 2.判断图像是否建 ...
- json、xml ---- 数据格式生成类
自己写的一个生成json/xml 格式数据的类,可用于api数据传输: <?php class Response{ /** *生成指定数据格式 *@param intval $code 状态码 ...
- oracle中的层级递归查询操作
oracle中的层级操作非常方便,在使用之后爱不释手,以前要实现该种数据查询操作,需要非常复杂的实现过程.在oracle中通过connect by可以实现前面的目的,通常情况下层级查询基本都能实现递归 ...
- Java-Hibernate官方英文文档地址
Hibernate官方英文文档地址 http://docs.jboss.org/hibernate/orm/4.3/manual/en-US/html/
- IEqualityComparer 去重
1.去除list里某重复字段值的数据(相当于group by) public class CorrController { //方法 public void DoGet() { List<tes ...
- vim字符串替换
vi/vim 中可以使用 :s 命令来替换字符串.以前只会使用一种格式来全文替换,今天发现该命令有很多种写法(vi 真是强大啊,还有很多需要学习),记录几种在此,方便以后查询. :s/vivian/s ...