**伪代码**
S:当前已经在联通块中的所有点的集合
dist[i] = inf
for n 次
t<-S外离S最近的点
利用t更新S外点到S的距离
st[t] = true
n次迭代之后所有点都已加入到S中
Dijkstra算法是更新到起始点的距离,Prim是更新到集合S的距离
#include <iostream>
#include <cstring>
using namespace std;
const int INF=0x3f3f3f3f;
int n,m;
int res;
int dist[510];
bool st[510];
int g[510][510];
int prime(){
memset(dist,0x3f,sizeof(dist));
for(int i=0;i<n;i++){
int t=-1;
for(int j=1;j<=n;j++){
if(!st[j]&&(t==-1||dist[t]>dist[j])){
t=j;
}
}
if(i&&dist[t]==INF){
return INF;
}
if(i){
res+=dist[t];
}
st[t]=true;
for(int j=1;j<=n;j++){
dist[j]=min(dist[j],g[t][j]);
}
}
return res;
}
int main(){
memset(g,0x3f,sizeof(g));
cin>>n>>m;
while(m--){
int a,b,c;
cin>>a>>b>>c;
g[a][b]=g[b][a]=min(g[a][b],c);
}
int t=prime();
if(t==INF){
cout<<"impossible"<<endl;
}
else{
cout<<t<<endl;
}
return 0;
}