主题链接:

problemId=203" target="_blank">ZOJ 1203 Swordfish 旗鱼

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


题意:给定平面上N个城市的位置,计算连接这N个城市所需线路长度总和的最小值。

分析:最小生成树,Kruskal问题求解。注意两个城市之间都有一条边相连。还有每两组输出之间空一行。

代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <cstdlib>
using namespace std; #define maxn 5055
double ans;
int cnt;
int parent[110];
struct edge
{
int u, v;
double w;
}EG[maxn]; bool cmp(edge a, edge b)
{
return a.w < b.w;
}
int cmp2(const void *a, const void *b)
{
edge aa = *(const edge*)a;
edge bb = *(const edge*)b;
if(aa.w > bb.w) return 1;
return -1;
}
int Find(int x)
{
if(parent[x] == -1) return x;
return Find(parent[x]);
}
void Kruskal()
{
memset(parent, -1, sizeof(parent));
sort(EG, EG+cnt, cmp);
//qsort(EG, cnt, sizeof(EG[0]), cmp2);
ans = 0;
for(int i = 0; i < cnt; i++)
{
int t1 = Find(EG[i].u), t2 = Find(EG[i].v);
if(t1 != t2)
{
ans += EG[i].w;
parent[t1] = t2;
}
}
}
int main()
{
int n, cas = 0;
double x[110], y[110];
while(scanf("%d", &n), n)
{
for(int i = 0; i < n; i++)
scanf("%lf%lf", &x[i], &y[i]);
cnt = 0;
for(int i = 0; i < n; i++)
for(int j = i+1; j < n; j++)
{
EG[cnt].u = i;
EG[cnt].v = j;
EG[cnt].w = sqrt((x[i]-x[j])*(x[i]-x[j])+(y[i]-y[j])*(y[i]-y[j]));
cnt++;
}
Kruskal();
if(cas > 0)
printf("\n");
printf("Case #%d:\nThe minimal distance is: %.2f\n", ++cas, ans);
//++cas;
}
return 0;
}

版权声明:本文博主原创文章,博客,未经同意不得转载。

ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法的更多相关文章

  1. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

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

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

  3. 最小生成树Kruskal算法

    Kruskal算法就是把图中的所有边权值排序,然后从最小的边权值开始查找,连接图中的点,当该边的权值较小,但是连接在途中后会形成回路时就舍弃该边,寻找下一边,以此类推,假设有n个点,则只需要查找n-1 ...

  4. 最小生成树------Kruskal算法

    Kruskal最小生成树算法的概略描述:1 T=Φ:2 while(T的边少于n-1条) {3 从E中选取一条最小成本的边(v,w):4 从E中删去(v,w):5 if((v,w)在T中不生成环) { ...

  5. 求最小生成树——Kruskal算法

    给定一个带权值的无向图,要求权值之和最小的生成树,常用的算法有Kruskal算法和Prim算法.这篇文章先介绍Kruskal算法. Kruskal算法的基本思想:先将所有边按权值从小到大排序,然后按顺 ...

  6. 最小生成树 kruskal算法&prim算法

    (先更新到这,后面有时间再补,嘤嘤嘤) 今天给大家简单的讲一下最小生成树的问题吧!(ps:本人目前还比较菜,所以最小生成树最后的结果只能输出最小的权值,不能打印最小生成树的路径) 本Tianc在刚学的 ...

  7. 算法实践--最小生成树(Kruskal算法)

    什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...

  8. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  9. 数据结构之最小生成树Kruskal算法

    1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...

随机推荐

  1. 解决windows下的mysql匿名登陆无法使用mysql数据库的问题

    原文:解决windows下的mysql匿名登陆无法使用mysql数据库的问题 我在windows下安装了mysql,但是不用密码就能登进去,而root明明是有密码的,我用select user()命令 ...

  2. ARM架构和编程-4

    ARM中断异常处理: ARM系统中止品种:按中断处理降序排列优先级:重置.数据访问中止.高速中断请求.外部中断请求.预取中止.令.软件中断. ARM体系中的异常中断向量表: 0x0 复位 0x4 没有 ...

  3. API拾遗录之Fragment

    Fragment必须内嵌到activity中,它不能单独使用,并且它的生命周期受到activity生命周期的制约——当activity暂停时,所有的fragment暂停,当activity停止时,所有 ...

  4. 让window命令行支持自己主动补全[相似Linux的Tab键]

    打开注冊表,找到HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Command Processor下 项"CompletionChar"(REG_DWO ...

  5. dell服务器从硬盘导入阵列信息

    前几天去南京客户那里更新新的业务系统,客户要求将服务器上的旧的硬盘拆下来,换上新的硬盘,重新做raid,客户自己要插入旧的硬盘读取旧数据,昨天做了几个实验,两台Dell R710服务器各4块硬盘,一台 ...

  6. SQL Server 2008备份数据库失败,拒绝访问的原因

    原文:SQL Server 2008备份数据库失败,拒绝访问的原因 备份数据到特定目录是出现拒绝访问,然后测试备份到C盘根目录正常. 查了下原因: 是因为那个目录没有Authenticated Use ...

  7. 【译】ASP.NET MVC 5 教程 - 2:添加控制器

    原文:[译]ASP.NET MVC 5 教程 - 2:添加控制器 MVC 表示 模型-视图-控制器.MVC 是一种用于开发应用程序的模式,具备良好架构,可测试和易于维护.基于 MVC 应用程序中包含: ...

  8. hdu3804(树链剖分)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=3804 题意:给定一棵n个结点的树及边权,回答m个询问(x,y)满足以下条件的边权: 1)该边在结点1~ ...

  9. Android Fragment使用

                     通常地 fragment做为宿主activity UI的一部分, 被作为activity整个view hierarchy的一部分被嵌入. 有2种方法你能够加入一个fr ...

  10. 写代码质量改善java计划151建议——导航开始

    2014-05-16 09:08 by Jeff Li 前言 系列文章:[传送门] 下个星期度过这几天的奋战,会抓紧java的进阶学习.听过一句话,大哥说过,你一个月前的代码去看下,慘不忍睹是吧.确实 ...