题解 | #Freckles#

Freckles

https://www.nowcoder.com/practice/41b14b4cd0e5448fb071743e504063cf

//土尔逊Torson 编写于2023/07/06
#define _CRT_SECURE_NO_WARNINGS
#include <iostream>
#include <map>
#include <vector>
#include <algorithm>
#include <math.h>
#include <cstdio>
#include <stdlib.h>
#define N 101

using namespace std;

float res13601;

struct FrecklesPos {
	float x;
	float y;
};

struct FLine {
	int from;
	int to;
	float weight;
	inline bool operator <(const FLine &g)const {
		return weight < g.weight;
	}
};

vector<FLine> graph13601;
vector<FrecklesPos> vec13601;

int father13601[N];
int height13601[N];

void init13601(int n) {
	for (int i = 0; i <= n; ++i) {
		father13601[i] = i;
		height13601[i] = 0;
	}
	graph13601.clear();
	vec13601.clear();
	res13601 = 0;
}

float getdis(FrecklesPos a, FrecklesPos b) {
	return pow((a.x - b.x)*(a.x - b.x) + (a.y - b.y)*(a.y - b.y), 0.5);
}

int find13601(int x) {
	if (x != father13601[x]) {
		father13601[x] = find13601(father13601[x]);
	}
	return father13601[x];
}

void Union13601(int x, int y, float weight) {
	x = find13601(x);
	y = find13601(y);
	if (x != y) {
		res13601 += weight;
		if (height13601[x] > height13601[y]) {
			father13601[y] = x;
		}
		else if (height13601[x] < height13601[y]) {
			father13601[x] = y;
		}
		else {
			father13601[x] = y;
			height13601[y]++;
		}
	}
}

int main() {
	int n;
	while (scanf("%d", &n) != EOF) {
		init13601(n);
		FrecklesPos p;
		FLine g;
		for (int i = 0; i < n; ++i) {
			scanf("%f%f", &p.x, &p.y);
			vec13601.push_back(p);
		}

		for (int i = 0; i < vec13601.size() - 1; ++i) {
			for (int j = i + 1; j < vec13601.size(); ++j) {
				g.from = i;
				g.to = j;
				g.weight = getdis(vec13601[i], vec13601[j]);
				graph13601.push_back(g);
			}
		}
		sort(graph13601.begin(), graph13601.end());
		for (int i = 0; i < graph13601.size(); ++i) {
			Union13601(graph13601[i].from, graph13601[i].to, graph13601[i].weight);
		}
		printf("%.2f\n", res13601);
	}
	system("pause");
	return EXIT_SUCCESS;
}
// 64 位输出请用 printf("%lld")

全部评论

相关推荐

叶扰云倾:进度更新,现在阿里云面完3面了,感觉3面答得还行,基本都答上了,自己熟悉的地方也说的比较细致,但感觉面试官有点心不在焉不知道是不是不想要我了,求阿里收留,我直接秒到岗当阿里孝子,学校那边的房子都退租了,下学期都不回学校,全职猛猛实习半年。这种条件还不诱人吗难道 然后现在约到了字节的一面和淘天的复活赛,外加猿辅导。华为笔试完没动静。 美团那边之前投了个base广州的,把我流程卡麻了,应该是不怎么招人,我直接简历挂了,现在进了一个正常的后端流程,还在筛选,不知道还有没有hc。
点赞 评论 收藏
分享
评论
点赞
收藏
分享

创作者周榜

更多
牛客网
牛客网在线编程
牛客网题解
牛客企业服务