Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			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] != INFTY {
				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() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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>
using namespace std;
static const int Max = 100;
static const int Infty = (1<<21);
static const int White = 0;
static const int Gray = 1;
static const int Black = 2;

int n,M[Max][Max];

int prim(){
    int u,minv;
    int a[Max],p[Max],color[Max];

    for(int i=0;i<n;i++){
        a[i]=Infty;
        p[i]=-1;
        color[i]=White;
    }
    a[0]=0;

    while(1){
        minv=Infty;
        u=-1;
        for(int i=0;i<n;i++){
            if(minv>a[i]&&color[i]!=Black){
                u=i;
                minv=a[i];
            }
        }
        if(u==-1)break;
        color[u]=Black;
        for(int v=0;v<n;v++){
            if(color[v]!=Black&&M[u][v]!=Infty){
                if(a[v]>M[u][v]){
                    a[v]=M[u][v];
                    p[v]=u;
                    color[v]=Gray;
                }
            }
        }
    }

    int sum =0;
    for(int i=0;i<n;i++){
        if(p[i]!=-1)sum += M[i][p[i]];

    }
    return sum;
}

int main(){
    cin>>n;

    for(int i=0 ;i<n; i++){
        for(int j=0;j<n;j++){
            int e;cin>>e;
            M[i][j]=(e==-1)? Infty : e;

        }
    }
    cout << prim()<<endl;

    return 0;
}

C++: /*
* @Author: Samson
* @Date:   2018-06-12 18:24:44
* @Last Modified by:   Samson
* @Last Modified time: 2018-06-13 11:59:44
*/
//   @URL : http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=ALDS1_12_A
#include<bits/stdc++.h>
#include<algorithm>
#include <cstdlib>
#define white 0
#define gray 1
#define black 2
#define INF 0x3f3f3f3f
using namespace std;
typedef long long LL;
const int MAXN = 1e3+10;

int p[MAXN],color[MAXN],a[MAXN][MAXN],t,n,d[MAXN];

int prim()
{
	int minv,u,v;
	for(int i = 0; i < n; ++i)
	{
		d[i] = INF;
		p[i] = -1;
		color[i] = white;
	}
	
	d[0] = 0;

	while(1)
	{
		minv = INF;
		u = -1;
		for(int i = 0; i < n; ++i)
		{
			if(minv > d[i] && color[i] != black)
				minv = d[i],u = i;
		}
		if(u == -1)	break;
		color[u] = black;
		for(int v = 0; v < n; ++v)
		{
			if(color[v] != black && a[u][v] != INF)
			{
				if(d[v] > a[u][v])
				{
					d[v] = a[u][v];
					p[v] = u;
					color[v] = gray;
				}
			}
		}
	}
	int ans = 0;
	for(int i = 0; i < n; ++i)
		if(p[i] != -1)
			ans += a[i][p[i]];
	return ans;
}

int main(void)
{
	ios::sync_with_stdio(false); 
	//cin.tie(0);
	cin>>n;
	for(int i = 0; i < n; ++i)
	{
		for(int j = 0; j < n; ++j)
		{
			cin>>t;
			a[i][j] = t==-1?INF:t;
		}
	}
	cout<<prim()<<'\n';
	return 0;
}

C++: 
// O(|E||V|)

#include <vector>
#include <cassert>
#include <algorithm>

using namespace std;

class Prim {
#define MAX_V 101

private:
  const int INF = 1e9 + 10;
  struct Edge {
    int to, cost;
    Edge(int to, int cost): to(to), cost(cost) { }
  };

  int V;
  vector<Edge> G[MAX_V];
  int mincost[MAX_V];
  bool used[MAX_V];

public:

  Prim(int V): V(V) { 
    if (V >= MAX_V) {
      assert(false);
    }
  }

  void AddEdge(int from, int to, int cost) {
    G[from].push_back(Edge(to, cost));
  }

  int Solve() {
    for (int i = 0; i < V; i++) {
      mincost[i] = INF;
      used[i]    = false;
    }

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

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

      for (auto &e : G[v]) {
        mincost[e.to] = min(mincost[e.to], e.cost);
      }
    }

    return res;
  }
};

#include <iostream>

int main() {
  int n;
  cin >> n;
  Prim prim(n);

  for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
      int c; cin >> c;
      if (c != -1) {
        prim.AddEdge(i, j, c);
      }
    }
  }

  int ans = prim.Solve();

  cout << ans << endl;

  return 0;
}

C++: #include<iostream>
using namespace std;
#include<queue>
#include<vector>
#include<string>
#include<string.h>
#include<algorithm>
bool b[2003];

#define REP(i, a) for(int i=0;i<a;i++)

int main()
{
	int min,b,a,m[101][101],n ,e,p[101],d[101],u,v; //u ?????????v????????¨
	bool color[101];
	cin>>n;
	REP(i, n) {
		REP(j, n) {
			cin>>m[i][j];
			if(m[i][j]==-1) m[i][j]=2001;
		}
		color[i]=0;
		d[i]=2001;
		p[i]=2001;
	}
	a=0;
	d[0]=0;
	while(1){
		min=2001;
		u=-1;
		REP(i,n){
			if(min>d[i]&&color[i]==0){
				min=d[i];
				u=i;
			}
		}
		if(u==-1) break;
		color[u]=1;
		REP(i,n){
			if(color[i]==0&&m[u][i]!=2001){
				if(d[i]>m[u][i]){
					d[i]=m[u][i];
					p[i]=u;
				}
			}
		}

	}
	min=0;
	REP(i,n){
		if(p[i]!=2001){
		min=min+m[i][p[i]];
		}
	}
	cout<<min<<endl;
	return 0;
}