只对最后一个点
  • 板块P1194 买礼物
  • 楼主syr1125
  • 当前回复0
  • 已保存回复0
  • 发布时间2024/4/28 21:00
  • 上次更新2024/4/29 00:33:53
查看原帖
只对最后一个点
561949
syr1125楼主2024/4/28 21:00
#include <bits/stdc++.h>
using namespace std;

int a[5005];

struct edge
{
	int x, y, z;
	
	bool operator < (const edge __) const
	{
		return z < __.z;
	}
}edge[200005];

int find(int x)
{
	if (a[x] != x)
	{
		a[x] = find(a[x]);
	}
	return a[x];
}

int n, m;
int main()
{
	scanf("%d%d", &n, &m);
	int cnt = 0;
	for (int i = 1; i <= n; i ++)
	{
		for (int j = 1; j <= n; j ++)
		{
			int x;
			cin >> x;
			if (x > m)
			{
				edge[++ cnt] = {i, j, m};
			}
			else if (x > 0)
			{
				edge[++ cnt] = {i, j, x};
			}
		}
	}
	sort(edge + 1, edge + 1 + cnt);
	for (int i = 1; i <= n; i ++) 
	{
		a[i] = i;
	}
	
	long long res = 0, ans = 0;
	for (int i = 1; i <= cnt; i ++)
	{
		int _x = edge[i].x, _y = edge[i].y, _z = edge[i].z;
		int a1 = find(_x), b1 = find(_y);
		if (a1 == b1)
		{
			continue;
		}
		a[a1] = b1;
		res ++;
		ans += _z;
	}
	cout << ans + m;
	return 0;
}

注意到ki,jk_{i, j}可能大于AA了

2024/4/28 21:00
加载中...