ZOJ 1203 Swordfish 旗鱼 最小生成树,Kruskal算法
主题链接: 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算法的更多相关文章
- 【转】最小生成树——Kruskal算法
[转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...
- 最小生成树——kruskal算法
kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...
- 最小生成树Kruskal算法
Kruskal算法就是把图中的所有边权值排序,然后从最小的边权值开始查找,连接图中的点,当该边的权值较小,但是连接在途中后会形成回路时就舍弃该边,寻找下一边,以此类推,假设有n个点,则只需要查找n-1 ...
- 最小生成树------Kruskal算法
Kruskal最小生成树算法的概略描述:1 T=Φ:2 while(T的边少于n-1条) {3 从E中选取一条最小成本的边(v,w):4 从E中删去(v,w):5 if((v,w)在T中不生成环) { ...
- 求最小生成树——Kruskal算法
给定一个带权值的无向图,要求权值之和最小的生成树,常用的算法有Kruskal算法和Prim算法.这篇文章先介绍Kruskal算法. Kruskal算法的基本思想:先将所有边按权值从小到大排序,然后按顺 ...
- 最小生成树 kruskal算法&prim算法
(先更新到这,后面有时间再补,嘤嘤嘤) 今天给大家简单的讲一下最小生成树的问题吧!(ps:本人目前还比较菜,所以最小生成树最后的结果只能输出最小的权值,不能打印最小生成树的路径) 本Tianc在刚学的 ...
- 算法实践--最小生成树(Kruskal算法)
什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...
- 模板——最小生成树kruskal算法+并查集数据结构
并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...
- 数据结构之最小生成树Kruskal算法
1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...
随机推荐
- 就是这么简单(续)!使用 RestAssuredMockMvc 测试 Spring MVC Controllers(转)
接我前面一篇文章关于RestAssured测试Restful web service的, RestAssured还有一个功能, 使用RestAssuredMockMvc 单元测试你的Spring MV ...
- 我写过的软件之FileExpert
公司要做一个项目,跟MP4有点关系.到网上找了规范文档看了看,理解还是不够深入.干脆花点时间做一个Parser.取名FileExpert.眼下仅仅支持解析ISO_IEC_14496-12的文件格式.取 ...
- Application to find the maximum temperature in the weather dataset
import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.IntWritable; import org.apache.hadoop. ...
- 陈一舟《情系人人》:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区
陈一舟<情系人人>:先搞钱,再搞人才_DoNews-IT门户-移动互联网新闻-电子商务新闻-游戏新闻-风险投资新闻-IT社交网络社区 陈一舟<情系人人>:先搞钱,再搞人才
- 启动、停止、重启 MySQL 常见的操作方法:
启动.停止.重启 MySQL 常见的操作方法: 简单罗列 一.启动方式 1.使用 service 启动:service mysqld start 2.使用 mysqld 脚本启动:/etc/inint ...
- A Game of Thrones(7) -Arya
Arya’s stitches were crooked again. She frowned down at them with dismay and glanced over to where h ...
- js实现的侧边栏展开收缩效果
原文地址:http://www.softwhy.com/forum.php?mod=viewthread&tid=12246 <!DOCTYPE html> <html> ...
- 【PLSQL】package包的使用
************************************************************************ ****原文:blog.csdn.net/clar ...
- 【转】C# String.Format数字格式化输出各种转换{0:N2} {0:D2} {0:C2}...
; //格式为sring输出 // Label1.Text = string.Format("asdfadsf{0}adsfasdf",a); // Label2.Text = & ...
- 强大的PropertyGrid
PropertyGrid, 做工具一定要用这东西..... 把要编辑的对象看成类的话, 全部要编辑的属性就是成员 嗯嗯, 近期看了几眼Ogitor, 它对于PropertyGrid的使用就非常不错 全 ...