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条边不构成回路. 具体做法:首先构造一个 ...
随机推荐
- hdu4738(双连通分量)
传送门:Caocao's Bridges 题意:n个岛,曹操在一些岛之间建了一些桥,每个桥上有一些士兵把守,周瑜只有一个炸弹只能炸掉一个桥,并能使一些岛被孤立出来,炸弹需要士兵带过去,士兵的数量不能小 ...
- 【C语言疯狂讲义】(八)C语言一维数组
1.数组的基本概念: 同样类型 若干个 有序 由若干个同样类型的数据组成的有序的集合 有序:存储地址连续 下标连续 数组名:用来存放数组首地址的变量 数组元素:构成数组的每个数据 数组的 ...
- OpenCV-Python教程(9、使用霍夫变换检测直线)
相比C++而言,Python适合做原型.本系列的文章介绍如何在Python中用OpenCV图形库,以及与C++调用相应OpenCV函数的不同之处.这篇文章介绍在Python中使用OpenCV的霍夫变换 ...
- poj-2195-Going Home最小费用最大流
重新切一遍最小费用最大流~~~ 这到题目的数据范围有问题,尽量开大就好了~~ #include<stdio.h> #include<iostream> #include< ...
- vim配置(vimplus)
vim配置(vimplus) vimplus vimplus是vim的超级配置安装程序 github地址:https://github.com/chxuan/vimplus.git,欢迎star和fo ...
- hdu1876(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1876 题意:问机器人到达终点的过程中最多有几次完全消耗完能量,消耗完这么多次能量的方式有几种. 分析: ...
- jersey client上传下载文件
jersey client上传文件demo File file = new File("/tmp/test.jpg"); System.out.println(file.exist ...
- 用Java写个ftp传输类实现文件的上传和下载,用ikvmc转成dll
1.Java类: package com.wjy.ftp.transmission; import java.io.File; import java.io.FileOutputStream; imp ...
- 开源一个适用iOS的数据库表结构更新机制的代码
将前段时间开源的代码.公布一下: ARDBConfig On the iOS, provide a database table structure update mechanism, ensure ...
- STM32串口乱码
库函数默认8MHz晶振,应根据实际硬件选择 # CMSIS/stm32f10x.h #define HSE_VALUE ((uint32_t)12000000) #if !defined HSE_VA ...