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

《算法笔记》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;
}

相关文章:

  • TTL、RS-232 和 RS-485 串行通信电平标准区别解析
  • 如何对多维样本进行KS检验
  • Nerfstudio 环境配置与自有数据集(图片和视频)测试全方位全流程实战【2025最新版!!】
  • Linux 权限管理
  • 【浙江大学DeepSeek公开课】DeepSeek的本地化部署与AI通识教育之未来
  • x86架构-k8s设置openebs的hostpath作为默认存储类的部署记录
  • LeetCode每日一题4.29
  • 省科学技术奖申报答辩PPT设计制作美化
  • 信息检索系统的组成部分和基本架构
  • 4月29日日记
  • jenkins slave节点打包报错Failed to create a temp file on
  • AI国学智慧语录视频,条条视频10W+播放量
  • 数据处理方式 之 对数变换 和Box-Cox变换以及对应逆变换【深度学习】
  • Vue2+Vue3学习笔记
  • 什么是Amazon S3
  • 农村供水智能化远程监控解决方案
  • php学习笔记(全面且适合新手)
  • 接入层架构演变
  • Android ProcessState init
  • 初中九年级学生体测准考证照片采集软件使用说明
  • “五一”假期逛上海车展请提前购票,展会现场不售当日票
  • 王毅:时代不容倒退,公道自在人心
  • 万科:一季度营收近380亿元,销售回款率超100%
  • 临港迎来鸿蒙智行“尚界”整车及电池配套项目,首款车型今秋上市
  • 市场监管总局:2024年查办商标、专利等领域违法案件4.4万件
  • 习近平访问金砖国家新开发银行