http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=203

Swordfish


Time Limit: 2 Seconds      Memory Limit: 65536 KB

There exists a world within our world
A world beneath what we call
cyberspace.
A world protected by firewalls,
passwords and the most
advanced
security systems.
In this world we hide
our deepest
secrets,
our most incriminating information,
and of course, a shole lot of
money.
This is the world of Swordfish.

We all remember that
in the movie Swordfish, Gabriel broke into the World Bank Investors Group in
West Los Angeles, to rob $9.5 billion. And he needed Stanley, the best hacker in
the world, to help him break into the password protecting the bank system.
Stanley's lovely daughter Holly was seized by Gabriel, so he had to work for
him. But at the last moment, Stanley made some little trick in his hacker
mission: he injected a trojan horse in the bank system, so the money would jump
from one account to another account every 60 seconds, and would continue jumping
in the next 10 years. Only Stanley knew when and where to get the money. If
Gabriel killed Stanley, he would never get a single dollar. Stanley wanted
Gabriel to release all these hostages and he would help him to find the money
back.
  You who has watched the movie know that Gabriel at last got the money
by threatening to hang Ginger to death. Why not Gabriel go get the money
himself? Because these money keep jumping, and these accounts are scattered in
different cities. In order to gather up these money Gabriel would need to build
money transfering tunnels to connect all these cities. Surely it will be really
expensive to construct such a transfering tunnel, so Gabriel wants to find out
the minimal total length of the tunnel required to connect all these cites. Now
he asks you to write a computer program to find out the minimal length. Since
Gabriel will get caught at the end of it anyway, so you can go ahead and write
the program without feeling guilty about helping a
criminal.

Input:
The input contains several test cases. Each
test case begins with a line contains only one integer N (0 <= N <=100),
which indicates the number of cities you have to connect. The next N lines each
contains two real numbers X and Y(-10000 <= X,Y <= 10000), which are the
citie's Cartesian coordinates (to make the problem simple, we can assume that we
live in a flat world). The input is terminated by a case with N=0 and you must
not print any output for this case.

Output:
You need to help
Gabriel calculate the minimal length of tunnel needed to connect all these
cites. You can saftly assume that such a tunnel can be built directly from one
city to another. For each of the input cases, the output shall consist of two
lines: the first line contains "Case #n:", where n is the case number (starting
from 1); and the next line contains "The minimal distance is: d", where d is the
minimal distance, rounded to 2 decimal places. Output a blank line between two
test cases.

Sample Input:

5
0 0
0 1
1 1
1 0
0.5 0.5
0

Sample Output:

Case #1:
The minimal distance is: 2.83
 #include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<string>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<cmath>
using namespace std; const int MAXN=;
const int MAXM=;
struct edge
{
int u,v;
double w;
} edges[MAXM];
int fa[MAXN];
int n,m;
double X[MAXN],Y[MAXN];
double sum; void fa_set()
{
for(int i=;i<n;i++)
fa[i]=-;
return ;
} int find(int x)
{
int s;
for(s=x;fa[s]>=;s=fa[s]);
while(s!=x)
{
int tmp=fa[x];
fa[x]=s;
x=tmp;
}
return s;
} void make_union(int x,int y)
{
int f1=find(x);
int f2=find(y);
int tmp=fa[f1]+fa[f2];
if(fa[f1]>fa[f2])
{
fa[f1]=f2;
fa[f2]=tmp;
}
else
{
fa[f2]=f1;
fa[f1]=tmp;
}
return ;
}
/*
int cmp(const void *a,const void *b)
{
// return ((edge*)a)->w-((edge*)b)->w;
if(((edge*)a)->w>((edge *)b)->w)
return 1;
return -1;
}*/ bool cmp(const edge &a,const edge &b)
{
return a.w<=b.w;
}
void kruskal()
{
int num=;
int u,v;
fa_set();
sum=;
for(int i=;i<m;i++)
{
u=edges[i].u;
v=edges[i].v;
if(find(u)!=find(v))
{
sum+=edges[i].w;
num++;
make_union(u,v);
}
if(num>=n-)
break;
}
return ;
} int main()
{
double d;
int cas=,i,j;
while()
{
scanf("%d",&n);
if(n==)
break;
for(i=;i<n;i++)
scanf("%lf%lf",&X[i],&Y[i]);
int mi=; //mi=n*(n-1)/2;
for(i=;i<n;i++)
for(j=i+;j<n;j++)
{
d=sqrt((X[i]-X[j])*(X[i]-X[j])+(Y[i]-Y[j])*(Y[i]-Y[j]));
edges[mi].u=i;
edges[mi].v=j;
edges[mi].w=d;
mi++;
}
m=mi;
//qsort(edges,m,sizeof(edges[0]),cmp);
sort(edges,edges+m,cmp);
kruskal();
if(cas>)
printf("\n");
printf("Case #%d:\n",cas);
printf("The minimal distance is: %.2lf\n",sum);
cas++;
}
return ;
}

Kruskal算法 Swordfish的更多相关文章

  1. 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用

    图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...

  2. 最小生成树---Prim算法和Kruskal算法

    Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...

  3. 最小生成树的Kruskal算法实现

    最近在复习数据结构,所以想起了之前做的一个最小生成树算法.用Kruskal算法实现的,结合堆排序可以复习回顾数据结构.现在写出来与大家分享. 最小生成树算法思想:书上说的是在一给定的无向图G = (V ...

  4. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  5. Kruskal算法(三)之 Java详解

    前面分别通过C和C++实现了克鲁斯卡尔,本文介绍克鲁斯卡尔的Java实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的 ...

  6. Kruskal算法(二)之 C++详解

    本章是克鲁斯卡尔算法的C++实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的代码说明 6. 克鲁斯卡尔算法的源码 转 ...

  7. Kruskal算法(一)之 C语言详解

    本章介绍克鲁斯卡尔算法.和以往一样,本文会先对克鲁斯卡尔算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3 ...

  8. 最小生成树问题---Prim算法与Kruskal算法实现(MATLAB语言实现)

    2015-12-17晚,复习,甚是无聊,阅<复杂网络算法与应用>一书,得知最小生成树问题(Minimum spanning tree)问题.记之. 何为树:连通且不含圈的图称为树. 图T= ...

  9. 学习笔记之 prim算法和kruskal算法

    ~. 最近数据结构课讲到了prim算法,然而一直使用kruskal算法的我还不知prim的思想,实在是寝食难安,于此灯火通明之时写此随笔,以祭奠我睡过去的数 据结构课. 一,最小生成树之prim pr ...

随机推荐

  1. SQL中以count及sum为条件的查询

    在开发时,我们经常会遇到以“累计(count)”或是“累加(sum)”为条件的查询.比如user_num表: id user num 1 a 3 2 a 4 3 b 5 4 b 7   例1:查询出现 ...

  2. 进程通信之一 使用WM_COPYDATA C++及C#实现(转)

    进程间通信最简单的方式就是发送WM_COPYDATA消息.本文提供C++及C#程序相互通信的二种实现方式.这样消息的接收端可以用C++实现,发送端可以用C++或C#实现.     发送WM_COPYD ...

  3. EmEditor正则表达式例子

    正则表达式中 单词指的是由字母.数字.下划线组合而成的字符串,用符号表示为\w(小写). 空白符包括单字节空格.双字节空格.制表符,用符号表示为\s(小写). 1.匹配被双引号包含的所有字符串(str ...

  4. (转载)Linux启动过程详解

    启动第一步--加载BIOS当你打开计算机电源,计算机会首先加载BIOS信息,BIOS信息是如此的重要,以至于计算机必须在最开始就找到它.这是因为BIOS中包含了CPU的相关信息.设备启动顺序信息.硬盘 ...

  5. DelphiXE7中创建WebService(服务端+客户端)

    相关资料: http://www.2ccc.com/news/Html/?1507.html http://www.dfwlt.com/forum.php?mod=viewthread&tid ...

  6. C++中#include包含头文件带 .h 和不带 .h 的区别

    C++中#include包含头文件带 .h 和不带 .h 的区别? 如 #include <iostream> 和 #include <iostream.h> 包含的东西有哪些 ...

  7. C#经典面试题 C# 中 Struct 与 Class 的区别,以及两者的适用场合

    在一家公司面试时,第一个问题就是问到这个 转载 文章 http://www.cnblogs.com/waitrabbit/archive/2008/05/18/1202064.html  来解释此问题 ...

  8. ucGUI 12864 从打点起

      ucGUI是纯C写的的,移植需要定义点阵数,颜色数,和画点函数 以下是ucGUI 12864下的移植 基于ST7920控制的12864液晶用于字符显示很方便的,但网友说用它显示图形并不合适,原因就 ...

  9. 调用DEDE日期时间格式整理大全

    dedecms 日期时间格式大全,大家可以根据需要选择.DEDECMS利用strftime()函数格式化时间的所有参数详解,包括年份日期进制.小时格式等,大家收藏吧,呵. 日期时间格式 (利用strf ...

  10. AlertView with password

    1. setAlertViewStyle:UIAlertViewStyleSecureTextInput UIAlertView *alertView = [[UIAlertView alloc] i ...