玄关求调
查看原帖
玄关求调
787888
Channel_Choo楼主2024/9/12 17:57
#include <iostream>
#include <algorithm>
#include <string.h>
using namespace std;
int n, m, cot = 0, ans = 0, pre[300005], a[300005], sum = 0;
struct EDG{int u, v, w;
}edg[300005];
int pot = 0;
bool cmp(const EDG &x, const EDG &y)
{
	return x.w < y.w;
}
int find(int x)
{
	if (pre[x] == x)
	{
		return x;
	}
	return pre[x] = find(pre[x]);
}
void kruskal()
{
	sort(edg + 1, edg + cot + 1, cmp);
	/*for (int i = 1;i <= cot;i++)
	{
		cout << edg[i].u << "->" << edg[i].v << "=" << edg[i].w << "\n";
	}*/
	int k = 0;
	for (int i = 1;i <= n;i++)
	{
		pre[i] = i;
	}
	//cout << cot << "\n";
	for (int i = 1;i <= cot;i++)
	{
		int u = find(edg[i].u);
		int v = find(edg[i].v);
		if (u == v && edg[i].w > m)
		{
			//cout << 1 <<endl;
			//cout << edg[i].u << " " << edg[i].v << "\n";
			//cout << u << " " << v << "\n\n";
			continue;
		}
		ans -= (m - edg[i].w);
		pre[u] = v;
		k++;
		//cout << i << " " << edg[i].w << " " << ans << "\n";
		if (k == m - 1)
		{
			cout << ans;
			return ;
		}
	}
	//cout << ans;
	//cout << k;
}
int main()
{	
	cin >> n >> m;
	if (m == 1)
	{
		cout << n;
		return 0;
	}
	getchar();
	string s;
	getline(cin, s);
	for (int i = 0;i < s.length();i++)
	{
		if (s[i] >= '0' && s[i] <= '9')
		{
			pot++;
			a[pot] = s[i] - '0';
		}
	}
	//cout << pot << endl;
	/*for (int i = 1;i <= pot;i++)
	{
		cout << a[i] << " ";
	}
	cout << endl;*/
	//cout << pot;
	for (int i = 1;i <= pot;i++)
	{
		cot++;
		edg[cot].u = 0;
		edg[cot].v = i;
		edg[cot].w = n;
	}
	for (int i = 1;i <= pot;i++)
	{
		if (a[i] != 0)
		{
			cot++;
			edg[cot].u = 1;
			edg[cot].v = i;
			edg[cot].w = a[i];
		}
	}
	for (int i = 2;i <= pot;i++)
	{
		for (int j = 1;j <= pot;j++)
		{
			int num;
			cin >> num;
			if (i < j || num != 0)
			{
				cot++;
				edg[cot].u = i;
				edg[cot].v = j;
				edg[cot].w = num;
				if (num > m)
				{
					sum++;
				}
			}
		}
	}
	/*cout << cot << "\n";
	for (int i = 1;i <= cot;i++)
	{
		cout << edg[i].u << "->" << edg[i].v << "=" << edg[i].w << "\n";
	}*/
	ans = m * n;
	kruskal();
	return 0;
}

应该是kruskal主体打错了,希望有大犇帮帮忙,orz

2024/9/12 17:57
加载中...