首页 > 其他分享 >uva 10034(最小生成树)

uva 10034(最小生成树)

时间:2023-06-28 19:32:23浏览次数:50  
标签:return parent int freckles 最小 ++ 10034 uva line


题目:

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


1


3


1.0 1.0


2.0 2.0


2.0 4.0


Sample Output


3.41




题意:kruskal算法水过。。


#include <stdio.h>
#include <string.h>
#include <math.h>
#include <algorithm>
using namespace std;
const int N = 5055;
struct Edge{
	int u, v;
	double w;
}e[N];
int parent[N];
double x[N], y[N];
int cmp(Edge x, Edge y){
	return x.w < y.w ? 1 : 0;
}
int get_parent(int x){
	return x == parent[x] ? x : get_parent(parent[x]);
}
int Union(int x, int y){
	int px = get_parent(x);
	int py = get_parent(y);
	if(px != py){
		parent[px] = py;
		return 1;
	}
	return 0;
}
int main(){
	int num, n, i, j;
	scanf("%d", &num);
	while(num--){
        memset(e, 0, sizeof(e));
		scanf("%d", &n);
		for(i = 0; i < n; i++)
			scanf("%lf%lf", &x[i], &y[i]);
		int a = 0;
		for(i = 0; i < n; i++)
			for(j = i + 1; j < n; j++){
				e[a].u = i;
				e[a].v = j;
				e[a].w = sqrt((x[i] - x[j]) * (x[i] - x[j]) + (y[i] - y[j]) * (y[i] - y[j]));
				a++;
			}
		for(i = 0; i < a; i++)
			parent[i] = i;
		double ans = 0;
		sort(e, e + a, cmp);
		for(i = 0; i < a; i++)
			if(Union(e[i].u, e[i].v))
				ans += e[i].w;
		printf("%.2lf\n", ans);
		if(num)  
           puts("");  

	}
	return 0;
}






标签:return,parent,int,freckles,最小,++,10034,uva,line
From: https://blog.51cto.com/u_10729926/6575703

相关文章

  • uva 10878(字符串)
    题目:"Machinestakemebysurprisewithgreatfrequency."AlanTuringYourbosshasjustunearthedarollofoldcomputertapes.Thetapeshaveholesinthemandmightcontainsomesortofusefulinformation.Itfallstoyoutofigureoutwhatisw......
  • uva 113(数学)
    题目:Currentworkincryptographyinvolves(amongotherthings)largeprimenumbersandcomputingpowersofnumbersmodulofunctionsoftheseprimes.Workinthisareahasresultedinthepracticaluseofresultsfromnumbertheoryandotherbranchesofmat......
  • 最小的linux发行版本
     TinyCoreLinux    TinyCoreLinux,MicroCoreLinux,17MBLinuxGUI桌面,live,节俭,可扩展    SliTaz         SliTazGNU/Linux(en)  适用于老旧设备的七款轻量级Linux发行版-系统极客(sysgeek.cn) ......
  • Prim算法 最小值生成树
    前言:给定一个无向图,如果它的某个子图中任意两个顶点都互相连通并且是一棵树,那么这棵树就叫做生成树(SpanningTree)。如果边上有权值,那么使得边权和最小的生成树叫做最小生成树(MST,MinimumSpanningTree)。例如我们假设有这样一个图:把顶点看作村庄,边看作计划要修建的道路。......
  • 最小生成树(普里姆算法)
    试实现普里姆最小生成树算法。函数接口定义: voidPrim(AMGraphG,charu); 其中 G 是基于邻接矩阵存储表示的无向图,u表示起点裁判测试程序样例: #include<iostream>#defineMVNum10#defineMaxInt32767usingnamespacestd;structedge{charadjvex;......
  • 6-1 最小生成树(普里姆算法)
    试实现普里姆最小生成树算法。函数接口定义: voidPrim(AMGraphG,charu); 其中 G 是基于邻接矩阵存储表示的无向图,u表示起点裁判测试程序样例: #include<iostream>#defineMVNum10#defineMaxInt32767usingnamespacestd;structedge{charadjvex;......
  • hive最小化部署 生产部署 hiveserver2 代理对象 和metastore服务
    自带的derbe的数据库,建表后就是在路径下新建了一个文件,映射成表的概念,同时在yarn会去执行,但是很多数据量很小的操作不会提交到yarn从stu表读数据的时候用的inputformat写数据的时候用的outputformat   metastore服务保存表名和文件路径之间的映射关系  嵌入......
  • Linux之CentOS 7 安装-最小
    感谢原博主:https://blog.csdn.net/qq_44737094/article/details/1166517901.安装vmware安装很简单这里提供安装包常用编程安装包阿里云盘2.下载centos7镜像链接:https://pan.baidu.com/s/1L0SPwYxmYwFjogRPbhXHSw提取码:50j43.在vmware中安装(最小化)centos71.文件–>......
  • 二分查找法lowerCeil版(找某个重复值的最小下标)利用二分upper法实现
    也是利用二分的upper法实现的,不知道什么是upper?看这里->二分查找法upper版(找大于某个值的最小下标)递归+非递归版-翰林猿-博客园(cnblogs.com)思路:先利用upper找到上界的index拿着index-1的下标(也就是重复值的最大下标)向前遍历,一直到遍历到发现不相等的元素即可。......
  • 二分查找法upper版(找大于某个值的最小下标)递归+非递归版
    需求:比如说查询一个班级大于60分的最低分等等。思路与二分法基本相同,只不过是对比的逻辑发生了一些小变化,这里所说的上界就是指大于某个值的最小下标。当mid<target:说明target的上界还在mid的右边,所以要去找比mid大的当mid>target:说明mid有可能是target的上界,所以......