UVa 10034 - Freckles (最小生成树模板题)

2023-12-10 04:48

本文主要是介绍UVa 10034 - Freckles (最小生成树模板题),希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

链接:

http://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=975


题目:

Problem A: 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.

Input

The input begins with a single positive integer on a line by itself indicating the number of the cases following, each of them as described below. This line is followed by a blank line, and there is also a blank line between two consecutive inputs.

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.

Output

For each test case, the output must follow the description below. The outputs of two consecutive cases will be separated by a blank line.

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

Sample Input

13
1.0 1.0
2.0 2.0
2.0 4.0

Sample Output

3.41


分析与总结:

赤裸裸的最小生成树,模板题



代码:

1. Kruskal

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define N 105
double coord[N][2], w[N][N];
int n, pos;
int f[N*N], rank[N*N];struct Edge{int u, v;double val;friend bool operator<(const Edge&a,const Edge&b){return a.val < b.val;}
}arr[N*N];inline double getDist(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 
}void init(){for(int i=0; i<N*N; ++i)f[i]=i, rank[i]=0;
}
int find(int x){int i,j=x;while(j!=f[j]) j=f[j];while(x!=j){i=f[x]; f[x]=j; x=i;}return j;
}
bool Union(int x,int y){int a=find(x),b=find(y);if(a==b)return false;if(rank[a]>rank[b])f[b]=a;else{if(rank[a]==rank[b])++rank[b];f[a] = b;}return true;
}int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=1; i<=n; ++i)scanf("%lf%lf",&coord[i][0],&coord[i][1]);pos = 0;for(int i=1; i<=n; ++i){for(int j=i+1; j<=n; ++j){arr[pos].u=i, arr[pos].v=j;arr[pos++].val = getDist(coord[i][0],coord[i][1],coord[j][0],coord[j][1]);}}double ans=0;init();sort(arr, arr+pos);for(int i=0; i<pos; ++i){if(Union(arr[i].u, arr[i].v))ans += arr[i].val;}printf("%.2f\n", ans);if(T)puts("");} return 0;
}

2.Prim

#include<cstdio>
#include<cstring>
#include<cmath>
#define N 105
double coord[N][2], w[N][N], minCost[N];
int n, pre[N], hash[N];inline double getDist(double x1,double y1,double x2,double y2){return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2)); 
}double Prim(){memset(hash, 0, sizeof(hash));hash[1] = 1;for(int i=1; i<=n; ++i){minCost[i] = w[1][i];pre[i] = 1;}double sum=0;for(int i=1; i<n; ++i){int u=-1;for(int j=1; j<=n; ++j)if(!hash[j]){if(u==-1||minCost[j]<minCost[u])u=j;}sum += w[pre[u]][u];hash[u] = 1;for(int j=1; j<=n; ++j)if(!hash[j]){if(minCost[j]>w[u][j]){minCost[j] = w[u][j];pre[j] = u;}}}return sum;
}
int main(){int T;scanf("%d",&T);while(T--){scanf("%d",&n);for(int i=1; i<=n; ++i)scanf("%lf%lf",&coord[i][0],&coord[i][1]);memset(w, 0, sizeof(w));for(int i=1; i<=n; ++i)for(int j=1; j<=n; ++j)if(i!=j)w[i][j] = getDist(coord[i][0],coord[i][1],coord[j][0],coord[j][1]);printf("%.2f\n", Prim());if(T) printf("\n");} return 0;
}

——  生命的意义,在于赋予它意义。

          
     原创 http://blog.csdn.net/shuangde800 , By   D_Double  (转载请标明)




这篇关于UVa 10034 - Freckles (最小生成树模板题)的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/476057

相关文章

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财

如何在 Spring Boot 中实现 FreeMarker 模板

《如何在SpringBoot中实现FreeMarker模板》FreeMarker是一种功能强大、轻量级的模板引擎,用于在Java应用中生成动态文本输出(如HTML、XML、邮件内容等),本文... 目录什么是 FreeMarker 模板?在 Spring Boot 中实现 FreeMarker 模板1. 环

IDEA自动生成注释模板的配置教程

《IDEA自动生成注释模板的配置教程》本文介绍了如何在IntelliJIDEA中配置类和方法的注释模板,包括自动生成项目名称、包名、日期和时间等内容,以及如何定制参数和返回值的注释格式,需要的朋友可以... 目录项目场景配置方法类注释模板定义类开头的注释步骤类注释效果方法注释模板定义方法开头的注释步骤方法注

Python如何自动生成环境依赖包requirements

《Python如何自动生成环境依赖包requirements》:本文主要介绍Python如何自动生成环境依赖包requirements问题,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑... 目录生成当前 python 环境 安装的所有依赖包1、命令2、常见问题只生成当前 项目 的所有依赖包1、

MySQL中动态生成SQL语句去掉所有字段的空格的操作方法

《MySQL中动态生成SQL语句去掉所有字段的空格的操作方法》在数据库管理过程中,我们常常会遇到需要对表中字段进行清洗和整理的情况,本文将详细介绍如何在MySQL中动态生成SQL语句来去掉所有字段的空... 目录在mysql中动态生成SQL语句去掉所有字段的空格准备工作原理分析动态生成SQL语句在MySQL

Java利用docx4j+Freemarker生成word文档

《Java利用docx4j+Freemarker生成word文档》这篇文章主要为大家详细介绍了Java如何利用docx4j+Freemarker生成word文档,文中的示例代码讲解详细,感兴趣的小伙伴... 目录技术方案maven依赖创建模板文件实现代码技术方案Java 1.8 + docx4j + Fr

Java编译生成多个.class文件的原理和作用

《Java编译生成多个.class文件的原理和作用》作为一名经验丰富的开发者,在Java项目中执行编译后,可能会发现一个.java源文件有时会产生多个.class文件,从技术实现层面详细剖析这一现象... 目录一、内部类机制与.class文件生成成员内部类(常规内部类)局部内部类(方法内部类)匿名内部类二、

使用Jackson进行JSON生成与解析的新手指南

《使用Jackson进行JSON生成与解析的新手指南》这篇文章主要为大家详细介绍了如何使用Jackson进行JSON生成与解析处理,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 核心依赖2. 基础用法2.1 对象转 jsON(序列化)2.2 JSON 转对象(反序列化)3.

java中使用POI生成Excel并导出过程

《java中使用POI生成Excel并导出过程》:本文主要介绍java中使用POI生成Excel并导出过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录需求说明及实现方式需求完成通用代码版本1版本2结果展示type参数为atype参数为b总结注:本文章中代码均为

在java中如何将inputStream对象转换为File对象(不生成本地文件)

《在java中如何将inputStream对象转换为File对象(不生成本地文件)》:本文主要介绍在java中如何将inputStream对象转换为File对象(不生成本地文件),具有很好的参考价... 目录需求说明问题解决总结需求说明在后端中通过POI生成Excel文件流,将输出流(outputStre