90 pts 求助
查看原帖
90 pts 求助
910419
_32bit_Silentnight楼主2024/8/15 10:57

#10 WA

100 行 die 码,个人认为马蜂还可以,求助

#include <cstdio>
#include <algorithm>

using namespace std;

const int M = 3e5;
const int N = 1e5;

struct Node
{
	int x, y, z, cst;
}node[N];

struct edge
{
	int u, v, cst;
}graph[M];

bool x_cmp (Node X, Node Y)
{
	return X.x < Y.x;
}

bool y_cmp (Node X, Node Y)
{
	return X.y < Y.y;
}

bool z_cmp (Node X, Node Y)
{
	return X.z < Y.z;
}

bool cst_cmp (edge X, edge Y)
{
	return X.cst < Y.cst;
}

int n, m, father[N], ans;

int find_root (int x)
{
	if (father[x] == x)
		return x;
	return father[x] = find_root (father[x]);
}

void join (int x, int y)
{
	father[find_root (x)] = find_root(y);
}

void add_edge (int u, int v, int cst)
{
	graph[++ m].u = u;
	graph[m].v = v;
	graph[m].cst = cst;
}

void kruskal ()
{
	for (int i = 1; i <= n; i ++)
		father[i] = i;
	int edges = 0;
	sort (graph + 1, graph + m + 1, cst_cmp);
	for (int i = 1; i <= m; i ++)
	{
		edge curr = graph[i];
		int u = curr.u;
		int v = curr.v;
		if (find_root (u) == find_root (v))
			continue;
		join (u, v);
		ans += graph[i].cst;
		edges ++;
		if (edges == n - 1)
			return;
	}
}

int main ()
{
	scanf ("%d", &n);
	for (int i = 1; i <= n; i ++)
	{
		scanf ("%d %d %d", &node[i].x, &node[i].y, &node[i].z);
		node[i].cst = i;
	}
	sort (node + 1, node + n + 1, x_cmp);
	for (int i = 1; i < n; i ++) 
		add_edge (node[i].cst, node[i + 1].cst, node[i + 1].x - node[i].x);
	sort (node + 1, node + n + 1, y_cmp);
	for (int i = 1; i < n; i ++) 
		add_edge (node[i].cst, node[i + 1].cst, node[i + 1].y - node[i].y);
	sort (node + 1, node + n + 1, z_cmp);
	for (int i = 1; i < n; i ++) 
		add_edge (node[i].cst, node[i + 1].cst, node[i + 1].z - node[i].z);
	kruskal ();
	printf ("%d", ans);
}```
2024/8/15 10:57
加载中...