prim算法很难,但是我也把他写出来了。usaco3.1.1

#include <iostream>
#include <cstring>
using namespace std;
int main()
{
	int n,a[101][101],d[101],ans=0;//距离ok点的距离 
	bool v[101]={0};
	cin >> n;
	for(int i=1;i<=n;i++)
		for(int j=1;j<=n;j++)
			cin >> a[i][j];
	memset(d,0x3f,sizeof(d));
	d[1]=0;
	for(int i=1;i<=n;i++)
	{
		int x=0x3f3f3f3f,pos=0;
		for(int j=1;j<=n;j++)
			if(d[j]<x&&v[j]==0)
			{
				x=d[j];
				pos=j;
			}
		v[pos]=true;
		ans+=x;
		for(int j=1;j<=n;j++)
			if(d[j]>a[pos][j]&&v[j]==0)
				d[j]=a[pos][j];
	}
	cout << ans << endl;
	return 0;
}

 

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-09-16
猜你喜欢
  • 2021-05-26
  • 2022-01-08
  • 2022-12-23
  • 2022-01-03
相关资源
相似解决方案