《ybtoj高效进阶》第三部分第二章例题2 新的开始

题目大意

PRIM模板

思路

模板要什么思路
code:

#include<cstring>
#include<algorithm>
#include<cmath>
#include<iostream>
#include<cstdio>
#include<queue>
using namespace std;
struct f{
	int x,id;
} x;
bool operator <(const f &x,const f &y)
{
	return x.x>y.x;
}
priority_queue<f> o;
int p[2001][2001],n,s;
bool u[2001];
int main()
{
	cin>>n;
	for (int i=1;i<=n;i++)
	{
		cin>>p[0][i];
		p[i][0]=p[0][i];
	}
	for (int j=1;j<=n;j++)
	{
		for (int k=1;k<=n;k++) cin>>p[j][k];
	}
	for (int i=1;i<=n;i++)
	{
		x.id=i,x.x=p[0][i];
		o.push(x);
	}
	int tot=0;
	while (tot<=n&&o.size()!=0)
	{
		if (u[o.top().id]==1)
		{
			o.pop();
			continue;
		}
		u[o.top().id]=1;
		int xx=o.top().id;
		s+=o.top().x;
		o.pop();
		for (int i=1;i<=n;i++)
		{
			if (u[i]) continue;
			x.x=p[xx][i];
			x.id=i;
			o.push(x);
		}
	}
	cout<<s;
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值