当前位置: 首页 > ai >正文

《算法笔记》10.5小节——图算法专题->最小生成树 问题 B: Freckles

题目描述

In an episode of the Dick Van Dyke show, little Richie connects the freckles on his Dad's back to form a picture of the Liberty Bell. Alas, one of the freckles turns out to be a scar, so his Ripley's engagement falls through. 
Consider Dick's back to be a plane with freckles at various (x,y) locations. Your job is to tell Richie how to connect the dots so as to minimize the amount of ink used. Richie connects the dots by drawing straight lines between pairs, possibly lifting the pen between lines. When Richie is done there must be a sequence of connected lines from any freckle to any other freckle.

输入

The first line contains 0 < n <= 100, the number of freckles on Dick's back. For each freckle, a line follows; each following line contains two real numbers indicating the (x,y) coordinates of the freckle.

输出

Your program prints a single real number to two decimal places: the minimum total length of ink lines that can connect all the freckles.

样例输入
3
2723.62 7940.81
8242.67 11395.00
4935.54 6761.32
9
10519.52 11593.66
12102.35 2453.15
7235.61 10010.83
211.13 4283.23
5135.06 1287.85
2337.48 2075.61
6279.72 13928.13
65.79 1677.86
5324.26 125.56
0
样例输出
8199.56
32713.73

题目大意:给出 n 个点的坐标 (x,y),问将这 n 个点组成的图连通起来的最小代价是多少。保留 2 位小数输出。

分析:本题实际上是一个 n 个点,n*(n-1) 条边的图,给出的是点的坐标,点之间的距离是隐含的欧几里得距离。转化为这样求最小生成树即可。

#include<algorithm>
#include <iostream>
#include  <cstdlib>
#include  <cstring>
#include   <string>
#include   <vector>
#include   <cstdio>
#include    <queue>
#include    <stack>
#include    <ctime>
#include    <cmath>
#include      <map>
#include      <set>
#define INF 0x3fffffff
#define db1(x) cout<<#x<<"="<<(x)<<endl
#define db2(x,y) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<endl
#define db3(x,y,z) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<endl
#define db4(x,y,z,r) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<endl
#define db5(x,y,z,r,w) cout<<#x<<"="<<(x)<<", "<<#y<<"="<<(y)<<", "<<#z<<"="<<(z)<<", "<<#r<<"="<<(r)<<", "<<#w<<"="<<(w)<<endl
using namespace std;typedef struct node
{double x,y;
}node;double graph[110][110];double prim(int n)
{int s=0;double ans=0.0;double d[n+5];for(int i=0;i<n;++i)d[i]=INF;d[s]=0;bool vis[n+5]={0};for(int times=0;times<n;++times){int u=-1;double mini=INF;for(int i=0;i<n;++i){if(!vis[i]&&d[i]<mini)u=i,mini=d[i];}if(u==-1)return -1;vis[u]=1,ans+=mini;for(int i=0;i<n;++i){if(!vis[i]&&graph[u][i]!=INF&&d[i]>graph[u][i])d[i]=graph[u][i];}}return ans;
}int main(void)
{#ifdef testfreopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);clock_t start=clock();#endif //testint n;while(scanf("%d",&n),n){for(int i=0;i<n;++i)for(int j=0;j<n;++j)graph[i][j]=INF;vector<node>freckles[n+5];for(int i=0;i<n;++i){double a,b;scanf("%lf%lf",&a,&b);node temp;temp.x=a,temp.y=b;freckles[i].push_back(temp);if(i){for(int j=0;j<i;++j){graph[i][j]=graph[j][i]=sqrt((freckles[i][0].x-freckles[j][0].x)*(freckles[i][0].x-freckles[j][0].x)+(freckles[i][0].y-freckles[j][0].y)*(freckles[i][0].y-freckles[j][0].y));}}}double ans=prim(n);printf("%.2f\n",ans);}#ifdef testclockid_t end=clock();double endtime=(double)(end-start)/CLOCKS_PER_SEC;printf("\n\n\n\n\n");cout<<"Total time:"<<endtime<<"s"<<endl;        //s为单位cout<<"Total time:"<<endtime*1000<<"ms"<<endl;    //ms为单位#endif //testreturn 0;
}

http://www.xdnf.cn/news/2929.html

相关文章:

  • 当前HPLC载波无法满足全量数据分钟级采集需求的主要原因
  • STM32 SPI通信协议
  • 从整体上把握操作系统的作用,以及理解进程状态是什么
  • EtherCAT转Profinet网关,包装产线的“语言翻译器”
  • python:练习:2
  • 查看Mysql版本
  • c/c++之信号处理<signal.h>
  • 【vue3】黑马程序员前端Vue3小兔鲜电商项目【五】
  • 问题排查:calss extends 后页面加载不出来(忘记加super),打包后不报错;遇到问题可以适当出去走一下,让脑子休息一下
  • AimRT 从零到一:官方示例精讲 —— 五、Parameter示例.md
  • WPF(Windows Presentation Foundation)的内容模型
  • 可视化图解算法: 判断是不是二叉搜索树(验证二叉搜索树)
  • SEO优化指南与实战技巧
  • centos安装部署配置kafka
  • Vue常用的修饰符有哪些有什么应用场景(含deep seek讲解)
  • 通用事件库IO多路复用技术选型与设计
  • 常见位运算总结
  • 塑料材料工程师简历模板
  • C#进阶学习(十七)PriorityQueue<TElement, TPriority>优先级队列的介绍
  • 阿里云服务器 篇十二:加入 Project Honey Pot 和使用 http:BL
  • 万象生鲜配送系统代码2025年4月29日更新日志
  • Java练习3
  • c语言的常用的预处理指令和条件编译
  • __proto__与prototype
  • 误在非开发分支上开发解决方案
  • LabVIEW实验室项目中使用类模块与仿真
  • Linux 怎么安装 Oracle Java 8
  • 通过logrotate和cronolog对日志进行切割
  • 什么是DNS缓存?怎么清理DNS缓存?
  • 网络安全攻防演练实训室建设方案