Go: package main

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

const (
	WHITE int = iota
	GRAY
	BLACK
)

type graph struct {
	G [][]int
	C []int
	D []int
	P []int
}

var INFTY = 1 << 32

func constructGraph() *graph {
	sc := bufio.NewScanner(os.Stdin)
	sc.Scan()
	n, _ := strconv.Atoi(sc.Text())

	g := make([][]int, n)
	c := make([]int, n)
	d := make([]int, n)
	p := make([]int, n)

	for i := 0; i < n; i++ {
		sc.Scan()
		fields := strings.Fields(sc.Text())
		g[i] = toIntSlice(fields)
	}

	return &graph{g, c, d, p}
}

func (g *graph) initMst() {
	for i := 0; i < len(g.G); i++ {
		g.C[i] = WHITE
		g.D[i] = INFTY
	}
	g.D[0] = 0
	g.P[0] = -1
}

func (g *graph) primMst() int {
	g.initMst()

	var u int
	for {
		mincost := INFTY
		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.D[i] < mincost {
				mincost = g.D[i]
				u = i
			}
		}

		if mincost == INFTY {
			break
		}

		g.C[u] = BLACK

		for i := 0; i < len(g.G); i++ {
			if g.C[i] != BLACK && g.existEdge(u, i) {
				if g.G[u][i] < g.D[i] {
					g.D[i] = g.G[u][i]
					g.P[i] = u
				}
			}
		}
	}

	totalCost := 0
	for _, cost := range g.D {
		totalCost += cost
	}
	return totalCost
}

func (g *graph) existEdge(u, v int) bool {
	return g.G[u][v] != INFTY && g.G[v][u] != INFTY
}

func toIntSlice(s []string) []int {
	a := make([]int, len(s))
	for i, v := range s {
		num, _ := strconv.Atoi(v)
		if num == -1 {
			num = INFTY
		}
		a[i] = num
	}
	return a
}

func main() {
	g := constructGraph()
	fmt.Println(g.primMst())
}

C++: #include<bits/stdc++.h>

#define int long long int
#define rep(a,b,c) for(int a=b;a<c;a++)
#define repm(a,b,c) for(int a=(b-1);a>=c;a--)
#define pb push_back
#define str string
#define sf(a) scanfs("%d",&a)
#define pb push_back
#define mp make_pair
#define Fi first
#define Se second
#define ALL(v) (v).begin(), (v).end()
using namespace std;
const int INF = 1e18 + 9;
const int Mod = 1e9 + 7;
inline int replac(str s){double ans=0;rep(i,0,s.length()){ans+=(s[i]-'0')*pow(10,s.length()-i-1);}return (int)ans;}
inline string numstr(int m){str s="";while(m>0){char c;int a=m/10;if(a>0)a=m%(a*10);else a=m;c=(char)('0'+a);s+=c;m/=10;}str st="";for(int i=s.size()-1;i>=0;i--){st+=s[i];}return st;}
typedef vector<int> vi;
typedef pair<int,int> pii;
typedef vector<pii> vii;
int cost[1001][1001];///i->j=cost;
int mincost[1001];///shougoX->minimum-cost
bool used[1001];///inX?
int N;///ten-kazu
int prim(){
    rep(i,0,N){
        mincost[i]=INF;
        used[i]=false;
    }
    mincost[0] = 0;
    int res = 0;

    while(true){
        int v = -1;
        rep(u,0,N){
            if(!used[u]&&(v == -1 || mincost[u] < mincost[v]))v=u;
        }
        if(v == -1)break;
        used[v] = true;
        res += mincost[v];

        rep(i,0,N){
            mincost[i] = min(mincost[i], cost[v][i]);
        }
    }
    return res;
}

signed main()
{
    cin.tie(0);
    ios::sync_with_stdio(false);
    map<int,int> mx,my;
    int n;
    cin >> n;
    N=n;
    rep(i,0,1001){
        rep(j,0,1001){
            cost[i][j]=INF;
        }
    }
    rep(i,0,n){
        rep(j,0,n){
            int x;cin >> x;
            if(x==-1)continue;
            cost[i][j]=x;
            cost[j][i]=x;
        }
    }
    cout << prim() << endl;
    return 0;
}

C++: #include<iostream>
using namespace std;
static const int MAX=100;
static const int INF=(1<<21);
static const int APE=0;
static const int MID=1;
static const int BOT=2;

int n, M[MAX][MAX];

int prim(){
  int u, mv, i, v, sum=0;
  int d[MAX], p[MAX], c[MAX];
  for(i=0;i<n;i++){
    d[i]=INF;
    p[i]=-1;
    c[i]=APE;
  }
  d[0]=0;

  while(1){
    mv=INF;
    u=-1;
    for(i=0;i<n;i++){
      if(mv>d[i]&&c[i]!=BOT){
        u=i;
        mv=d[i];
      }
    }
    if(u==-1)break;
    c[u]=BOT;

    for(v=0;v<n;v++){
      if(c[v]!=BOT&&M[u][v]!=INF){
        if(d[v]>M[u][v]){
          d[v]=M[u][v];
          p[v]=u;
          c[v]=MID;
        }
      }
    }
  }
  for(i=0;i<n;i++){
    if(p[i]!=-1){
      sum+=M[i][p[i]];
    }
  }
  return sum;
}

int main(){
  int i, j, e;
  cin>>n;

  for(i=0;i<n;i++){
    for(j=0;j<n;j++){
      cin>>e;
      M[i][j]=(e==-1) ? INF : e;
    }
  }
  cout<<prim()<<endl;
  return 0;
}

C++: //MST
#if 0

#endif // 0
#include<iostream>
#include<vector>
#include<algorithm>
#define MAX 10010
using namespace std;
vector<int> s;
int root(int x)
{
    if(s[x]==x)return x;
    return root(s[x]);
}

int main()
{
    int cost[MAX],src[MAX],dst[MAX];
    pair<int,int> pos[MAX];
    int n,sum=0;
    cin>>n;
    s.resize(n);
    for(int i=0;i<n;i++)
    {
        s[i]=i;
    }
    int m=0;
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int a;
            cin>>a;
            if(a>=0&&j>i)
            {
                cost[m]=a;
                src[m]=i;
                dst[m]=j;
                pos[m].first=cost[m];
                pos[m].second=m;
                m++;
            }
        }
    }
    sort(pos,pos+m);
    int c=0;
    for(int i=0;i<m;i++)
    {
        int d=pos[i].second;
        if(root(src[d])!=root(dst[d]))
        {
            sum+=cost[d];
            s[root(src[d])]=root(dst[d]);
            c++;
        }
        if(c>=n-1)
            break;
    }

    cout<<sum<<endl;
    return 0;
}

C++: #include <iostream>
#include <vector>
#include <string>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <algorithm>	// require sort next_permutation count __gcd reverse etc.
#include <cstdlib>	// require abs exit atof atoi 
#include <cstdio>		// require scanf printf
#include <functional>
#include <numeric>	// require accumulate
#include <cmath>		// require fabs
#include <climits>
#include <limits>
#include <cfloat>
#include <iomanip>	// require setw
#include <sstream>	// require stringstream 
#include <cstring>	// require memset
#include <cctype>		// require tolower, toupper
#include <fstream>	// require freopen
#include <ctime>		// require srand
#define rep(i,n) for(int i=0;i<(n);i++)
#define ALL(A) A.begin(), A.end()
#define INF 1<<20

using namespace std;

typedef long long ll;
typedef pair<int, int> P;

int cost[105][105];
int mincost[105];
bool used[105];

int prim (int n )
{
	memset (used, false, sizeof (used ) );
	rep (i, n ) mincost[i] = INF;

	mincost[0] = 0;
	int res = 0;

	while (true ){
		int v = -1;
		// X  に属さない頂点のうち X からの辺のコストが最小になる頂点を探す
		rep (u, n )
			if (!used[u] && (v == -1 || mincost[u] < mincost[v] ) ) v = u;

		if (v == -1 ) break;

		used[v] = true;
		res += mincost[v];

		rep (u, n ) mincost[u] = min (mincost[u], cost[v][u] );
	} // end while

	return res;
}

int main()
{
	memset (cost, -1, sizeof (cost ) );
	ios_base::sync_with_stdio(0);
	int n; cin >> n;
	rep (i, n ) rep (j, n ) cost[i][j] = INF;

	rep (i, n ) rep (j, n ){
		int in; cin >> in;
		if (in == -1 ) in = INF;
		cost[i][j] = in;
	} // end rep

	int res = prim(n );
	cout << res << endl;
		
	return 0;
}