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. 【转】Maven实战(八)---模块划分

    本博文出自于:http://blog.csdn.net/liutengteng130/article/details/47000217    感谢! 为了防止传递依赖,我们各个模块之间尽量用直接依赖的 ...

  2. 关于Aazure 使用以前保留的vhd创建虚拟机的基本步骤

    1. 删除vm保留vhd(只删除虚拟机记录,不删除磁盘)2. 拷贝vhd以及status文件到指定的存储账号3. 使用拷贝的VHD创建disk4. 从disk创建vm,指定指定vnet以及cloud ...

  3. UVA 796 - Critical Links (求桥)

    Critical Links  In a computer network a link L, which interconnects two servers, is considered criti ...

  4. BestCoder Round #68 (div.2) geometry(hdu 5605)

    geometry Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Su ...

  5. WCF序列化与反序列化问题

    转自:http://www.cnblogs.com/wangweimutou/p/4505447.html WCF包含很多封装的内部机制,这些是我们在编写程序时不会经常看到的.比如上一篇讲解的Mess ...

  6. ckeditor异常问题

    上传图片时点击上传按钮时,图片不能上传,有两种可能 1:采用ssh框架 , 上传图片对应的struts.xml没有配置<constant name="struts.action.exc ...

  7. linux 查看当前路径命令:pwd

    查看当前路径命令:pwd pwd命令能够显示当前所处的路径. 这个命令比较简单,如果有时在操作过程中忘记了当前的路径,则可以通过此命令来查看路径,其执行方式为: # pwd /home/samlee ...

  8. Oracle RMAN 清除归档日志

    在开发环境及UAT环境经常碰到需要清除归档日志的情形,对于这个问题方法有很多.可以直接使用rm方式清除归档日志,也可以使用find命令来查找符合条件的记录来清除归档日志,或者直接写个shell脚本来搞 ...

  9. 在Android Eclipse 开发如何 使用 (*.aar)文件

    http://www.cnblogs.com/shortboy/p/4424944.html 开场白:其实这篇文章有点白费心机. 详细说明是:http://blog.csdn.net/qiujuer/ ...

  10. eclipse中异常的快捷键

    选中你要try的代码,alt+shift+z 就会弹出一个菜单,里面有个try 选项