题目大意
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;
}