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. CTS FAIL(一)

    首先简单介绍下CTS:全称Compatibility Test Suite,通过CTS测试,来检测android apk与android系统的兼容性. 最近公司release一版新的Image,但在新 ...

  2. 分布式应用框架Akka快速入门

    转自:http://blog.csdn.net/jmppok/article/details/17264495 本文结合网上一些资料,对他们进行整理,摘选和翻译而成,对Akka进行简要的说明.引用资料 ...

  3. erlang局域网内节点通信——艰难四步曲 (转)

    http://blog.chinaunix.net/uid-22566367-id-382011.html 在Programming Erlang这本书中,在写到第十章中,主要实现的是不同节点之间的通 ...

  4. 查看Linux里某文件的前面/后面几行中的某一行

    如,我想看/etc/profile文件的前5行里的第5行. 则,   head -5 /etc/profile  |  tail -1  管道|啊,很简单,就是把左边命令的结果,作为右边的输入. 如, ...

  5. ZOJ 3911 Prime Query(线段树)

    Prime Query Time Limit: 1 Second      Memory Limit: 196608 KB You are given a simple task. Given a s ...

  6. 转载 从Http到Https

    转载自 http://www.cnblogs.com/silin6/p/5928503.html HTTP 当你在浏览器输入一个网址 (例如 http://tasaid.com)的时候,浏览器发起一个 ...

  7. #pragma comment使用

    编程经常碰到,理解的总不是很透彻,在这里查阅资料总结一下! 在编写程序的时候,我们常用到#pragma指令来设定编译器的状态或者是指示编译器完成一些特定的动作. #pragma once : 这是一个 ...

  8. hibernate[版本四]知识总结

    1.hibernate是orm对象关系映射,是对jdbc的封装 2.hibernate版helloworld 2.1导入jar <dependencies> <dependency& ...

  9. zookeeper的配置项

    1 tickTime:CS通信心跳数 Zookeeper 服务器之间或客户端与服务器之间维持心跳的时间间隔,也就是每个 tickTime 时间就会发送一个心跳.tickTime以毫秒为单位. tick ...

  10. 漫谈云计算与SOA (1)

    SOA是什么? 英语直译是基于服务的架构,就是一种技术框架,促使企业内部与外部所有相关的系统公开和访问定义良好的服务和绑定于服务的信息,进一步抽象成流程层和组合应用,从而构成解决方案. 说人话:重用服 ...