求助
查看原帖
求助
716916
jackooo楼主2022/12/25 00:37
#include <iostream>
#include <algorithm>
#include <vector>

using namespace std;

const int N = 220;
int p[N], n, ans;

struct Edge
{
	int i, x;
	
	bool operator < (const Edge &t)const
	{
		return x < t.x;
	}
};
vector<Edge> h[N];
int find(int x)
{
	if(p[x] != x)p[x] = find(p[x]);
	return p[x];
}

int main()
{
	cin >> n;
	for(int i = 1; i <= n; i++)p[i] = i;
	for(int i = 1; i <= n; i++)
	{
		for(int j = 1; j <= n; j++)
		{
		 	int x = 0;
		  	cin >> x;	   
		    if(i != j)h[i].push_back({j, x});
		}		
		sort(h[i].begin(), h[i].end());		
	}
	
	for(int i = 1; i < n; i++)
	{
		for(int j = 0; j < n; j++)
		{
		 	if(find(i) != find(h[i][j].i))
  			{
  			 	ans += h[i][j].x;
				p[find(i)] = find(h[i][j].i);   	
				break;    		   	
		    }
		}
	}
	cout << ans << endl;
	
	return 0;
}
2022/12/25 00:37
加载中...