走廊泼水节

最小生成树问题

题目链接
题解链接

解决这道题的思想体现了对Kruskal算法的深刻理解。

每当我们从排序好的边的数组中取出一条边准备连接时,这条边一定比它两个端点所对应的联通图的边集合中最小的边还要小。。基于这样的思想我们就能够求解出这道题。

  • 这其中有一点需要注意的是,原题中所说的添加的新的边是并不存在的。这也就是说这条添加的边的长度是我们可以人为随意决定的
#include<iostream>
#include<map>
#include<vector>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cstring>
#include<cmath>
#include<set>
#include<bitset>
#include<stack>
#include<cstring>
using namespace std;
#define MAX 10000

struct Edge
{
	int from, to, w;
};
vector<Edge>edges;
int fa[MAX];
int de[MAX];
void init(int n)
{
	for (int i = 1; i <= n; i++)
	{
		fa[i] = i;
		de[i] = 1;
	}
}
int find(int x)
{
	if (x == fa[x])
		return x;
	else
	{
		return fa[x] = find(fa[x]);
	}
	return 0;
}

void merge(int l, int r)
{
	int lf = find(l);
	int rf = find(r);
	if (lf == rf)return;
	else
	{
		fa[lf] =rf;
		de[rf] += de[lf];
	}
}

int main()
{
#ifndef ONLINE_JUDGE
	freopen("sb.txt", "r", stdin);
#endif // !ONLINE_JUDGE
	int N;
	cin >> N;

	for (int i = 0; i < N; i++)
	{
		int t;
		cin >> t;
		t--;
		edges.clear();
		for (int j = 0; j < t; j++)
		{
			int from, to, w;
			cin >> from >> to >> w;
			edges.push_back({ from,to,w });
		}
		sort(edges.begin(), edges.end(), [](Edge l, Edge r) {return l.w < r.w; });
		long long ans = 0;
		init(t+1);
		for (auto c : edges)
		{
			int from = c.from, to = c.to, w = c.w;

			int lf = find(from);
			int rf = find(to);

			if (lf == rf)continue;
			else
			{
				//我们能够保证现在正在填加进去的这条边一定是对应两个边集合的最小边

				ans += (de[lf] * de[rf] - 1) * (w + 1);
				merge(from,to);
			}

		}
		cout << ans << endl;
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值