Kruskal算法 Swordfish
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的更多相关文章
- 图的生成树(森林)(克鲁斯卡尔Kruskal算法和普里姆Prim算法)、以及并查集的使用
图的连通性问题:无向图的连通分量和生成树,所有顶点均由边连接在一起,但不存在回路的图. 设图 G=(V, E) 是个连通图,当从图任一顶点出发遍历图G 时,将边集 E(G) 分成两个集合 T(G) 和 ...
- 最小生成树---Prim算法和Kruskal算法
Prim算法 1.概览 普里姆算法(Prim算法),图论中的一种算法,可在加权连通图里搜索最小生成树.意即由此算法搜索到的边子集所构成的树中,不但包括了连通图里的所有顶点(英语:Vertex (gra ...
- 最小生成树的Kruskal算法实现
最近在复习数据结构,所以想起了之前做的一个最小生成树算法.用Kruskal算法实现的,结合堆排序可以复习回顾数据结构.现在写出来与大家分享. 最小生成树算法思想:书上说的是在一给定的无向图G = (V ...
- 最小生成树——kruskal算法
kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...
- Kruskal算法(三)之 Java详解
前面分别通过C和C++实现了克鲁斯卡尔,本文介绍克鲁斯卡尔的Java实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的 ...
- Kruskal算法(二)之 C++详解
本章是克鲁斯卡尔算法的C++实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3. 克鲁斯卡尔算法图解 4. 克鲁斯卡尔算法分析 5. 克鲁斯卡尔算法的代码说明 6. 克鲁斯卡尔算法的源码 转 ...
- Kruskal算法(一)之 C语言详解
本章介绍克鲁斯卡尔算法.和以往一样,本文会先对克鲁斯卡尔算法的理论论知识进行介绍,然后给出C语言的实现.后续再分别给出C++和Java版本的实现. 目录 1. 最小生成树 2. 克鲁斯卡尔算法介绍 3 ...
- 最小生成树问题---Prim算法与Kruskal算法实现(MATLAB语言实现)
2015-12-17晚,复习,甚是无聊,阅<复杂网络算法与应用>一书,得知最小生成树问题(Minimum spanning tree)问题.记之. 何为树:连通且不含圈的图称为树. 图T= ...
- 学习笔记之 prim算法和kruskal算法
~. 最近数据结构课讲到了prim算法,然而一直使用kruskal算法的我还不知prim的思想,实在是寝食难安,于此灯火通明之时写此随笔,以祭奠我睡过去的数 据结构课. 一,最小生成树之prim pr ...
随机推荐
- Canvas入门(2):图形渐变和图像形变换
来源:http://www.ido321.com/986.html 一.图形渐变(均在最新版Google中测试) 1.绘制线性渐变 1: // 获取canvas 的ID 2: var canvas = ...
- Chapter5:语句
表达式语句:一个表达式+一个分号 表达式语句的作用是执行表达式并丢弃掉求值结果. 空语句:单独一个分号 Best Practice:使用空语句时应该加上注释,从而令读这段代码的人知道该语句是有意省略的 ...
- C#调用dll(C++(Win32))时的类型转换总结(转)
http://www.cnblogs.com/lidabo/archive/2012/06/05/2536737.html C++(Win 32) C# char** 作为输入参数转为char ...
- [HIve - LanguageManual] Union
Union Syntax select_statement UNION ALL select_statement UNION ALL select_statement ... UNION is use ...
- 第二百五十天 how can I 坚持
html排版,好烦心. 我以为我会哭,但是我没有.---<领悟> 确实是搞不懂自己,到底想要什么?弟弟最近也不知道咋的了,感觉有点不对劲呢. 最近雾霾好严重,希望我们的后代不会知道雾霾是什 ...
- 第二百四十九天 how can I 坚持
竟然让我跟着他们去旅游..泡温泉,滑雪..西北坡,不去白不去. 罗娜,你别把我拉黑啊.哎,不知道咋办. 晚上玩了四局LOL,全输了,伤心. 还有今天抢票没抢到. 该怎么破,12点半正好在吃饭啊. 睡觉 ...
- 问题-某个程序改了ICO图标后编译后还是显示老图标?
问题现象:某个程序改了ICO图标后编译后还是显示老图标? 问题原原:可能是因为系统的缓存问题. 问题处理:把程序的EXE放在别的路径下打开就可以了. 问题相关人员:QQ253120114(朋友) Q ...
- NHibernate - ICriteria 查询
http://blog.knowsky.com/213234.htm http://blog.chinaunix.net/uid-20463341-id-1673509.html http://www ...
- linux信号量超过系统限制
部署一台新服务器,信号量报错,观察也没有key冲突,错误分析及解决如下: 创建一个不存在的信号量集返回参数错误的报错,因为信号量集的信号量数量超过了系统限制. 系统默认 /home/poc#ipcs ...
- 解决mysql导入导出数据乱码问题
最近在linux上面用mysqldump导出数据,放在windows系统中导入就会出现中文乱码,然后就会导致出现: Unknown MySQL server host和Can't connect to ...