kruskal不知道哪里爆了
查看原帖
kruskal不知道哪里爆了
117192
无产者万岁楼主2020/7/24 16:36
#include <bits/stdc++.h>
using namespace std;
long long n,price,table,f[501],cnt=0,d=1,re=1,ans;
struct data
{
	int from,to,cost;
}mp[501];
bool cmp(data a,data b)
{
	return a.cost<b.cost;
}
int find(int x)
{
	if(f[x]==x)
		return x;
	return f[x]=find(f[x]);
}
int main()
{
	cin>>price>>n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
		{
			cin>>table;
			if(i<j&&table)
			{
				mp[++cnt].from=i;
				mp[cnt].to=j;
				mp[cnt].cost=table;
			}
		}
	sort(mp+1,mp+cnt+1,cmp);
	while(d<n)
	{
		int p=find(mp[re].from);
		int q=find(mp[re].to);
		if(p!=q)
		{
			if(mp[d].cost<price)//坑点2
				ans+=mp[d].cost;
			else 
				ans+=price;
			f[p]=q;
			d++;
		}
		re++;
	}
	cout<<ans+price;
	return 0;
}
2020/7/24 16:36
加载中...