题目描述

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.

输入

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.

输出

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.

样例输入

5
0 0
0 1
1 1
1 0
0.5 0.5
0

样例输出

Case #1:
The minimal distance is: 2.83

来源/分类

ZJU 2002, Preliminary

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
using namespace std;
double inf=9999999;
int n;
double x[110],y[110],e[110][110],dis[110];
bool book[110];
double prim()
{int i,k,u;double minn,sum;for(i=1;i<=n;i++)dis[i]=e[1][i];memset(book,0,sizeof(book));book[1]=1;sum=0;for(k=1;k<n;k++){minn=inf;for(i=1;i<=n;i++)if(book[i]==0&&dis[i]<minn){minn=dis[i];u=i;}book[u]=1;sum+=minn;for(i=1;i<=n;i++)if(book[i]==0&&dis[i]>e[u][i])dis[i]=e[u][i];}return sum;
}
int main()
{int i,j,T=0;while(cin>>n&&n){for(i=1;i<=n;i++)cin>>x[i]>>y[i];for(i=1;i<=n;i++)for(j=i;j<=n;j++)e[i][j]=e[j][i]=sqrt((x[i]-x[j])*(x[i]-x[j])*1.0+1.0*(y[i]-y[j])*(y[i]-y[j]));double res=prim();T++;printf("Case #%d:\nThe minimal distance is: %.2lf\n\n",T,res);}return 0;
}

Swordfish【prim算法】相关推荐

  1. 数据结构与算法(7-3)最小生成树(普里姆(Prim)算法和克鲁斯卡尔(Kruskal)算法)

    目录 一.最小生成树简介 二.普里姆算法(Prim) 1.原理 2.存储 2-1.图顶点和权: 2-3. 最小生成树: 3.Prim()函数 3-1.新顶点入树 3-2.保留最小权 3-3. 找到最小 ...

  2. 基本数据结构(图: 基本结构,DFS,prim算法, kruskal算法)

    #include <iostream> using namespace std; //约定: //1. 图是由很多节点(VERTEX)构成的, 因此图结构是由一个VERTEX的链表构成的, ...

  3. 【数据结构】最小生成树 Prim算法 Kruskal算法

    最小生成树应用场景: 假设以下场景,有一块木板,板上钉上一些钉子,这些钉子可以由一些细绳连接起来.假设每个钉子可以通过一根或者多根细绳连接起来,那么一定存在这样得情况,即用最少的细绳把所有的钉子连接起 ...

  4. HDU1863(Prim算法)

    方法一:Prim算法 #include<iostream> #include<algorithm> #include<cstring> #include<ve ...

  5. 生成树的概念,最小生成树Prim算法 Kruskal算法

    求解最小生成树可以用Prim算法 Kruskal算法

  6. 三十七、Prim算法--求解最小生成树

    一.Prim算法介绍 普利姆(Prim)算法求最小生成树,也就是在包含 n 个顶点的连通图中,找出只有(n-1)条边包含所有 n 个顶点的 连通子图,也就是所谓的极小连通子图 普利姆的算法如下: 设 ...

  7. POJ2728 Desert King ——01分数规划Dinkelbach迭代法+最小生成树prim算法

    首先,纪念我用Linux系统AC的第一题-   安装这个万恶的NOI Linux系统费了6小时的时间,不过好在最后终于装上了,但是因为我安装的Linux系统比较烂,还遭到了小花儿和js的鄙视,唉,本人 ...

  8. 一步一步写算法(之prim算法 中)

    原文:一步一步写算法(之prim算法 中) [ 声明:版权所有,欢迎转载,请勿用于商业用途.  联系信箱:feixiaoxing @163.com] C)编写最小生成树,涉及创建.挑选和添加过程 MI ...

  9. Prim算法生成迷宫

    初始化地图 function initMaze(r,c){let row = new Array(2 * r + 1)for(let i = 0; i < row.length; i++){le ...

  10. prim算法_最小生成树的本质是什么?Prim算法道破天机

    本文始发于个人公众号:TechFlow,原创不易,求个关注 今天是算法和数据结构专题20篇文章,我们继续最小生成树算法,来把它说完. 在上一篇文章当中,我们主要学习了最小生成树的Kruskal算法.今 ...

最新文章

  1. 玩转飞书日历,体验高效办公!
  2. 第 3 章 镜像 - 010 - base 镜像
  3. 【运筹学】线性规划 单纯形法 案例二 ( 案例解析 | 标准形转化 | 查找初始基可行解 | 最优解判定 | 查找入基变量与出基变量 | 第一次迭代 )
  4. CentOS 命令【备忘】
  5. JavaScript中的数组和字符串
  6. 成员变量的初始化和内存中的运行机制
  7. insert 多条数据 并且具有唯一标识符
  8. typedef struct和struct区别
  9. 基于JAVA+SpringBoot+Mybatis+MYSQL的校园二手交易平台
  10. PostgreSQL中如何得到一个随机的字符
  11. java中sping基础_Java回顾之Spring基础
  12. 关于 try catch 捕捉不到异常
  13. Java面向对象 网络编程 上
  14. PropertyGrid—添加属性Tab
  15. 光学efl_关于光学设计使用以及理解
  16. Java通过GeoLite2-City.mmdb进行IP信息查询地理定位和经纬度
  17. 玩转多元化主播打法,扶持达人红出圈,火山小视频的运营方法论
  18. https的包该怎么抓?
  19. Spring Boot 应用在 kubernetes 的 sidecar 设计与实战
  20. bugly android升级,android 新版本升级示例源码(bugly)

热门文章

  1. python中文分词统计_python 中文字数统计/分词
  2. spark任务常见错误
  3. C#与matlab混合编程,MATLAB生成dll出现的问题,请大家一起研究一下,谢谢
  4. 2023 微信红包封面整蛊网页源码
  5. 输入一行文字,找出其中大写字母、小写字母、空格、数字以及其他字符各有多少(使用指针)
  6. mysql 创建唯一约束表
  7. Office 2016系列下载地址
  8. 计算机组成原理调试程序,计算机组成原理实验调试系统调试系统.PDF
  9. SSM购物商城项目开发
  10. p2p mysql 数据的拆分 案例_浅析: P2P网贷系统数据库设计