Go: package main

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

type color int

const (
	white color = iota // 未訪問
	mst                // MSTとして確定
)

// 頂点
type node struct {
	matrix   []int // 隣接行列
	minToMst int   // MSTへの重み最小値
	parent   int   // 最小値の相手
	color    color
}

func main() {
	scanner := bufio.NewScanner(os.Stdin)
	n := eGetInt(scanner)

	nodes := make([]node, n)
	for i := 0; i < n; i++ {
		nodes[i].matrix = eAtoi((strings.Fields(eGetline(scanner))))
		nodes[i].parent = -1
		nodes[i].minToMst = math.MaxInt16
		nodes[i].color = white
	}

	// 初回はnode[0]がMSTに入るように,minToMstを設定
	nodes[0].minToMst = 0
	for { // 全部チェックするまで

		// どの頂点を新たにMSTに取り込むか
		min := math.MaxInt16
		u := -1
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].minToMst < min {
				u = i
				min = nodes[i].minToMst
			}
		}

		if u == -1 {
			break
		}
		// MSTに追加する頂点決定
		nodes[u].color = mst

		// MSTに属していない頂点それぞれについて,MSTとの最小の重みを更新
		for i := 0; i < n; i++ {
			if nodes[i].color == mst {
				continue
			}
			if nodes[i].matrix[u] == -1 {
				// 接してない
				continue
			}
			if nodes[i].matrix[u] < nodes[i].minToMst {
				// node[u]との重みが最小
				nodes[i].minToMst = nodes[i].matrix[u]
				nodes[i].parent = u
			}
		}
	}

	sum := 0
	for _, v := range nodes {
		// node[0]はparentが入っていない
		if v.parent != -1 {
			sum += v.matrix[v.parent]
		}
	}
	fmt.Println(sum)
}

func eGetInt(scanner *bufio.Scanner) int {
	t := eAtoi(strings.Fields(eGetline(scanner)))
	return t[0]
}

func eGetline(p *bufio.Scanner) string {
	if !p.Scan() {
		if err := p.Err(); err != nil {
			log.Fatal(err.Error())
		}
		return ""
	}
	return p.Text()
}

func eAtoi(s []string) []int {
	ints := make([]int, len(s))
	var err error
	for i, e := range s {
		ints[i], err = strconv.Atoi(e)
		if err != nil {
			log.Fatal(err.Error())
		}
	}
	return ints
}

C++: //
// Created by tattsun on 2017/02/16.
//

#include <iostream>

#define MAX_N 101
#define INFTY 20000
#define NIL -1

enum Color {
    White,
    Glay,
    Black,
};

using namespace std;

int n;
int w[MAX_N][MAX_N];
int d[MAX_N], p[MAX_N];
Color color[MAX_N];

int min(int x, int y) {
    if(x < y) return x;
    return y;
}

int solve() {
    d[0] = 0;

    while(true) {
        int u = NIL, c = INFTY;

        for(int i=0; i<n; i++) {
            if(d[i] < c && color[i] != Black) {
                u = i;
                c = d[i];
            }
        }

        if(u == NIL)
            break;

        color[u] = Black;

        for(int i=0; i<n; i++) {
            if(color[i] != Black && w[u][i] != INFTY) {
                if(d[i] > w[u][i]) {
                    d[i] = w[u][i];
                    p[i] = u;
                    color[i] = Glay;
                }
            }
        }
    }


    int tc = 0;
    for(int i=0; i<n; i++) {
        if(color[i] == Black) {
            tc += w[i][p[i]];
        }
    }
    return tc;
}

int main() {
    for (int i = 0; i < MAX_N; i++) {
        d[i] = INFTY;
        p[i] = INFTY;
        color[i] = White;
        for (int j = 0; j < MAX_N; j++)
            w[i][j] = INFTY;
    }

    int p;
    cin >> n;
    for (int i = 0; i < n; i++) {
        for (int j = 0; j < n; j++) {
            cin >> p;
            w[i][j] = (p == -1) ? INFTY : p;
        }
    }

    cout << solve() << endl;
}

C++: #include <stdio.h>
#define rep(i, n) for(int i = 0; i < n; ++i)

int main(void) {
  int n, i, j, ans = 0;
  scanf("%d", &n);
  int a[n][n];
  char f[n];
  rep(i, n) rep(j, n) scanf("%d", &a[i][j]);
  rep(i, n) f[i] = 0;
  f[0] = 1;
  rep(i, n - 1) {
    int min = 10000, minx, miny;
    rep(j, n) {
      rep(k, n) {
        if(f[j] && !f[k] && a[j][k] >= 0 && a[j][k] < min) min = a[j][k], minx = j, miny = k;
      }
    }
    ans += min;
    f[miny] = 1;
  }
  printf("%d\n", ans);
  return 0;
}

C++: #include<iostream>
using namespace std;
static const int INF = 0x7fffffff;
static const int MAXN = 101;
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;
int n, M[MAXN][MAXN];

int prim()
{
	int node, minn;  //记录当前状态权值最小的边和对应的节点
	int d[MAXN], p[MAXN], color[MAXN]; //最小边权，父节点，访问状态
	/*初始化*/
	for (int i = 0; i < n; i++)
	{
		d[i] = INF;
		p[i] = -1;
		color[i] = WHITE; 
	}
	d[0] = 0;  //初始节点加入MST
	while (true)
	{
		node = -1;
		minn = INF;
		for (int i = 0; i < n; i++)
		{
			if (d[i] < minn && color[i] != BLACK)
			{
				minn = d[i];
				node = i;
			}
		}
		if (node == -1)	break; //图遍历完毕，没有可以新加入MST的节点
		color[node] = BLACK;   //把新节点加入MST

		/*更新状态*/
		for (int i = 0; i < n; i++) 
		{
			if (color[i] != BLACK && M[node][i] < INF)
			{
				if (d[i] > M[node][i])
				{
					d[i] = M[node][i];
					p[i] = node;
				}
			}
		}
	}
	int sum = 0;
	for (int i = 0; i < n; i++)
	{
		if (p[i] != -1)	sum += M[i][p[i]];  //求MST每条边的权和
	}
	return sum;
}

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

C++: #include<stdio.h>
using namespace std;
const int sss=1<<21;
int n;
int a[1000][1000];
int d[1000],p[10000],color[1000];
int prim(){
	int u,minv;
	for(int i=0;i<n;i++){
		d[i]=sss;
		p[i]=-1;
	}
	d[0]=0;
	while(1){
		minv=sss;
		u=-1;
		for(int i=0;i<n;i++){
			if(minv>d[i]&&color[i]!=1){
				u=i;
				minv=d[i];
			}
		}
		if(u==-1) break;
		color[u]=1;
		for(int v=0;v<n;v++){
			if(color[v]!=1&&a[u][v]!=sss){
				if(d[v]>a[u][v]){
					d[v]=a[u][v];
					p[v]=u;
				}
			}
		}
	}
	int sum=0;
	for(int i=0;i<n;i++){
		if(p[i]!=-1) {
			sum+=a[p[i]][i];
		}
	}
	return sum;
}
int main(){
	scanf("%d",&n);
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
			int e;scanf("%d",&e);
			a[i][j]=e==-1?sss:e;
		}
	}
	printf("%d\n",prim());
	return 0;
}

C++: #include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
bool f[100];
int n;
int tr[100];

bool ch(){
  for(int i=0;i<n;i++)
    if(f[i]==0) return true;
  return false;
}

int trmin(){
  int res=10000,resi;;
  for(int i=0;i<n;i++)
    if(f[i]==0 && tr[i]<res) res = tr[i],resi=i;
  return resi;
}


int main (){

  int mp[100][100];

  cin >> n;
  for(int i=0;i<n;i++)
    for(int j=0;j<n;j++)
      cin >> mp[i][j];

  for(int i=0;i<n;i++) f[i]=0;
  for(int i=0;i<n;i++) tr[i]=10000;
  tr[0] = 0;
  while(ch()){
    int r=trmin();
    f[r]=1;
    for(int i=0;i<n;i++) 
      if(mp[r][i]!=-1)
	tr[i] = min(tr[i],mp[r][i]);
  }
  int trsum=0;
  for(int i=0;i<n;i++) trsum+=tr[i];
  cout<<trsum<<endl;

  return 0;
}

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

using namespace std;
#define fordebug int hoge;cin>>hoge;
#define DEKAI 1000000007
#define INF (1<<28)
#define lp(i,n) for(int i=0;i<n;i++)
#define lps(i,j,n) for(int i=j;i<n;i++)
#define floot10 cout<<fixed<<setprecision(10);
#define int long long
int n,m[1000][1000],col[1000],d[1000],cent[1000];

void prim(){
	lp(i,n){
		d[i]=INF;
	}
	d[0]=0;
	cent[0]=-1;
	while(1){
		int mini=INF,hoge;
		lp(i,n){
			if(col[i]!=2&&d[i]<mini){
				mini=d[i];
				hoge=i;
			}
		}
		if(mini==INF) return;
		col[hoge]=2;
		lp(i,n){
			if(col[i]!=2){
				if(m[hoge][i]==INF) continue;
				if(m[hoge][i]<d[i]){
					d[i]=m[hoge][i];
					cent[i]=hoge;
					col[i]=1;
				}
			}
		}
	}
}

signed main(){
	cin>>n;
	lp(i,n){
		lp(j,n){
			cin>>m[i][j];
			if(m[i][j]==-1) m[i][j]=INF;
		}
	}
	prim();
	int ans=0;
	lp(i,n){
		if(cent[i]==-1) continue;
		ans+=m[i][cent[i]];
	}
	cout<<ans<<endl;
    return 0;
}