Go: package main

import (
	"bufio"
	"os"
	"strconv"
	"fmt"
)

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		u = -1
		for i := 0; i < n; i++ {
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}
		if u == -1 {
			break
		}
		color[u] = BLACK
		for v := 0; v < n; v++ {
			if color[v] != BLACK && M[u][v] != INF {
				if d[v] > M[u][v] {
					d[v] = M[u][v]
					p[v] = u
					color[v] = GRAY
				}
			}
		}
	}
	sum := 0
	for i := 0; i < n; i++ {
		if p[i] != -1 {
			sum += M[i][p[i]]
		}
	}
	return sum
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: #include <bits/stdc++.h>
#define ll long long
#define INF 1000000005
#define MOD 1000000007
#define EPS 1e-10
#define rep(i,n) for(int i=0;i<(int)(n);++i)
#define rrep(i,n) for(int i=(int)(n)-1;i>=0;--i)
#define srep(i,s,t) for(int i=(int)(s);i<(int)(t);++i)
#define each(a,b) for(auto (a): (b))
#define all(v) (v).begin(),(v).end()
#define len(v) (int)(v).size()
#define zip(v) sort(all(v)),v.erase(unique(all(v)),v.end())
#define cmx(x,y) x=max(x,y)
#define cmn(x,y) x=min(x,y)
#define fi first
#define se second
#define pb push_back
#define show(x) cout<<#x<<" = "<<(x)<<endl
#define spair(p) cout<<#p<<": "<<p.fi<<" "<<p.se<<endl
#define svec(v) cout<<#v<<":";rep(kbrni,v.size())cout<<" "<<v[kbrni];cout<<endl
#define sset(s) cout<<#s<<":";each(kbrni,s)cout<<" "<<kbrni;cout<<endl
#define smap(m) cout<<#m<<":";each(kbrni,m)cout<<" {"<<kbrni.first<<":"<<kbrni.second<<"}";cout<<endl

using namespace std;

typedef pair<int,int> P;
typedef pair<ll,ll> pll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<double> vd;
typedef vector<P> vp;
typedef vector<string> vs;

const int MAX_N = 100005;

class UF {
private:
    int sz; vector<int> par,nrank;
public:
    UF(){}
    UF(int node_size){ sz = node_size; par.resize(sz),nrank.resize(sz); rep(i,sz){ par[i] = i; nrank[i] = 0; } }
    int find(int x){ if(par[x] == x){ return x; }else{ return par[x] = find(par[x]); } }
    void unite(int x,int y)
    { x = find(x),y = find(y); if(x == y) return;
    	if(nrank[x] < nrank[y]){ par[x] = y; }else{ par[y] = x; if(nrank[x] == nrank[y]) nrank[x]++; } }
    bool same(int x,int y){ return find(x) == find(y); }
};

struct edge{
    int u,v;
    int cost;
    bool operator<(const edge& another) const {
        return cost < another.cost;
    }
};

template<typename T> class Kruskal{
public:
    vector<edge>& es;
	vector<int> par,rrank;
	int V;
    Kruskal(vector<edge>& e) : es{e}{
        V = len(e);
        es = e;
		par.resize(V), rrank.resize(V);
	}
	void add_edge(int u,int v,T cost){
		es.push_back((edge){u,v,cost});
	}
	T make(){
		UF uf(V);
		T res = 0;
		int cnt = 0;
        sort(es.begin(),es.end());
		rep(i,es.size()){
			edge e = es[i];
			if(!uf.same(e.u,e.v)){
				uf.unite(e.u,e.v);
				res += e.cost;
				cnt++;
				if(cnt == V-1){
					break;
				}
			}
		}
		return res;
	}
};

int main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    int n;
    cin >> n;
    vector<edge> vec;
    rep(i,n){
        rep(j,n){
            int a;
            cin >> a;
            if(i < j && a >= 0){
                vec.pb((edge){i,j,a});
            }
        }
    }
    Kruskal<int> kr(vec);
    cout << kr.make() << "\n";
    return 0;
}

C++: //#define LOCAL

#include<cstdio>
#include<algorithm>
#include<vector>

using namespace std;

#define rep(a, b) for(int i = a; i <= b; i++)

const int MAX = 1e2 + 10;

struct Ufs
{
	int prt[MAX];

	void init(int n)
	{
		rep(1, n)
			prt[i] = i;
		return;
	}

	int findprt(int x)
	{
		while(x != prt[x])
			x = prt[x];
		return x;
	}

	bool unite(int x, int y)
	{
		int px = findprt(x);
		int py = findprt(y);
		prt[x] = prt[y] = prt[px] = prt[py] = min(px, py);
		return px != py;
	}
};

struct Kruskal
{
	struct Edge
	{
		int u, v, w;
		Edge(int uu, int vv, int ww):u(uu), v(vv), w(ww){}
		friend bool operator <(Edge a, Edge b)
		{
			return a.w < b.w;
		}
	};

	vector<Edge> e;
	Ufs u;

	void init(void)
	{
		e.clear();
		return;
	}

	void addedge(int u, int v, int w)
	{
		e.push_back(Edge(u, v, w));
		return;
	}

	int solve(int n)
	{
		u.init(n);
		int ans = 0, cnt = 0;
		int len = e.size();
		sort(e.begin(), e.end());
		rep(0, len - 1)
		{
			if(u.unite(e[i].u, e[i].v))
			{
				cnt++;
				ans += e[i].w;
			}
			if(cnt == n - 1)
				break;
		}
		return ans;
	}
} k;

int main()
{

#ifdef LOCAL
	freopen("in.txt", "r", stdin);
#endif

	int n;
	while(~scanf("%d", &n))
	{
		k.init();
		rep(1, n)
		{
			for(int j = 1; j <= n; j++)
			{
				int w;
				scanf("%d", &w);
				if(j > i && w != -1)
					k.addedge(i, j, w);
			}
		}
		printf("%d\n", k.solve(n));
	}
	return 0;
}

C++: #include<bits/stdc++.h>
using namespace std;

int main(){
  int n;
  int a[100][100],b[100]={},c[100]={};
  cin>>n;
  for(int i=0;i<n;i++)
   for(int j=0;j<n;j++)cin>>a[i][j];
  c[0]=1;

  int k=0.min=10000,min1=0,ans=0;
  while(1){
   if(k==n)break;
   for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
     if(a[b[i]][j]<min && a[b[i]][j]!=-1 &&c[j]!=1){
      min=a[b[i]][j];
      b[k]=j;
    }
   c[b[k]]=1
   ans+=min;
   k++;
  }
 cout<<ans<<endl;
 return 0;
}

C++: #include <bits/stdc++.h>
#define endl "\n"
using namespace std;

int n;
vector<vector<double>>v;
vector<double>w;
vector<int>pre;
double inf=1e7;
void solve(){
	for(int i=0;i<n;i++)w.push_back(inf);
	for(int i=0;i<n;i++)pre.push_back(-1);
	w[0]=0;
	int k=0;
	double res=0;
	while(k++!=n){
		// 选一个最小的 
		double mi=inf;
		int id=-1;
		for(int i=0;i<n;i++)
			if(w[i]!=-1&&w[i]<mi)mi=w[i],id=i;
		w[id]=-1;
		for(int i=0;i<n;i++){
			if(w[i]!=-1&&v[id][i]!=-1&&v[id][i]<w[i]){
				w[i]=v[id][i];
				pre[i]=id;
			}
		}
		res+=mi;
	}
	cout<<res<<endl;
}
int main(){
	int t;
	cin>>n;
	for(int i=0;i<n;i++){
		v.push_back(vector<double>());
		for(int j=0;j<n;j++){
			cin>>t;
			v[i].push_back(t);
		}
	}
	solve();	
	return 0;
}