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 <iostream>
#include <vector>
#include <numeric>

using namespace std;

const int N = 100;



enum {
    WHITE,
    GRAY,
    BLACK,
};

int main() {
    int A[N][N];

    int n;
    cin >> n;
    for (int i = 0; i < n; ++i) {
        for (int j = 0; j < n; ++j) {
            int a;
            cin >> a;
            A[i][j] = a == -1 ? a : numeric_limits<int>::max();
        }
    }

    // ????????????
    vector<int> color(n, WHITE); // ??????????¨??????¶???
    vector<int> d(n, numeric_limits<int>::max());
    vector<int> p(n, -1);

    d[0] = 0;
    while (1) {
        int minv = numeric_limits<int>::min();
        int u = -1;
        for (int 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 (int v = 0; v < n; ++v) {
            if (color[v] != BLACK && A[u][v] != numeric_limits<int>::max()) {
                if (d[v] > A[u][v]) {
                    d[v] = A[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    int sum;
    for (int i =0; i < n; ++i) {
        if (p[i] != -1)
            sum += A[i][p[i]];
    }
    cout << sum << endl;

    return 0;
}

C++: #include <iostream>
#include <queue>
using namespace std;
#define MAX 105

int n, choice[MAX];

struct MyPower {
	int v, p1, p2;
	void set(int value, int point1, int point2) {
		v = value;
		p1 = point1;
		p2 = point2;
	}
};

bool operator<(MyPower a, MyPower b) {
	return (a.v > b.v);
}

int main(void) {
	int i, j, x;
	priority_queue<MyPower> PQ;
	MyPower MP;
	for (i = 0; i < MAX; i++) {
		choice[i] = 0;
	}
	cin >> n;
	for (i = 0; i < n; i++) {
		for (j = 0; j < n; j++) {
			cin >> x;
			if (x == -1) continue;
			MP.set(x, i, j);
			PQ.push(MP);
		}
	}
	/*
	int siz = PQ.size();
	for (i = 0; i<siz; i++) {
		MP = PQ.top();
		PQ.pop();
		cout << MP.v<< " "<<MP.p1<<" "<<MP.p2<<endl;
	}
	*/
	
	int c = 1, total = 0;
	while (c < n) {
		MP = PQ.top();
		PQ.pop();
		if (choice[MP.p1] == 0 || choice[MP.p2] == 0) {
			total += MP.v;
			choice[MP.p1] = 1;
			choice[MP.p2] = 1;
			c++;
		}
	}
	cout << total << endl;
	
}

C++: #include <cstdio>

const int MAX_N = 100;
const int INFINITY = 2000 + 1;

enum
{
  WHITE,
  GRAY,
  BLACK
};

int color[MAX_N];
int M[MAX_N][MAX_N];
int d[MAX_N];
int p[MAX_N];
int n;

void prim()
{
  for (int i = 0; i < n; ++i)
  {
    color[i] = WHITE;
    d[i] = INFINITY;
    p[i] = -1;
  }

  d[0] = 0;

  while (true)
  {
    int mincost = INFINITY;
    int u = -1;

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

    if (u == -1)
      break;

    color[u] = BLACK;

    for (int v = 0; v < n; ++v)
    {
      if (color[v] != BLACK && M[u][v] != INFINITY)
      {
        if (d[v] > M[u][v])
        {
          d[v] = M[u][v];
          p[v] = u;
          color[v] = GRAY;
        }
      }
    }
  }
}

int main()
{
  scanf("%d", &n);

  for (int i = 0; i < n; ++i)
  {
    for (int j = 0; j < n; ++j)
    {
      int m;
      scanf("%d", &m);
      M[i][j] = (m == -1) ? INFINITY : m;
    }
  }

  prim();

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

  printf("%d\n", count);

  return 0;
}

C++: #include <stdio.h>
using namespace std;
static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 0;
static const int BLACK = 0;

int n, M[MAX][MAX];

int prim() {
    int d[n], p[n];
    int color[n];
    
    for(int i=0; i<n; i++) {
        color[i] = WHITE;
        d[i] = INFTY;
    }
    d[0] = 0;
    p[0] = -1;
    
    while(1) {
        int u;
        int mincost = INFTY;
        for (int i=0; i<n; i++) {
            if (color[i] != BLACK && d[i] < mincost) {
                mincost = d[i];
                u = i;
            }
        }
        
        if (mincost == INFTY) break;
        
        color[u] = BLACK;
        
        for (int v=0; v<=n; v++) {
            if (color[v] != BLACK && M[u][v] != -1) {
                if(M[u][v] < d[v]) {
                    d[v] = M[u][v];
                    p[v] = u;
                    color[v] = GRAY;
                }
            }
        }
    }
    
    int sum=0;
    for (int i=0; i<n; i++) {
        sum += d[i];
    }
    
    return sum;
}

int main() {
    scanf("%d", n);
    for (int i=0; i<n; i++) {
        for (int j=0; j<n; j++) {
            scanf("%d", &M[i][j]);
        }
    }
    
    int sum = prim();
    printf("%d\n", sum);
    
    return 0;
}

C++: // AOJ ALDS 1_12_A
#include<cstdio>
#include<algorithm>
#include<vector>
#define rep(i,a) for( int i = 0; i != (a); ++i )

struct edge
{
	int from, to, cost;

	edge( int from, int to, int cost )
	:	from( from ), to( to ), cost( cost )
	{}
};

int n;
std::vector<edge> es;

int par[100], rank[100];

void init()
{
	rep( i, n )
		par[i] = i, rank[i] = 0;

	return;
}

int find( int x )
{
	if( x == par[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( rank[x] < rank[y] )
		par[x] = y;
	else
	{
		par[y] = x;
		if( rank[x] == rank[y] )
			++rank[x];
	}

	return;
}

bool same( int x, int y )
{
	return find( x ) == find( y );
}

int main()
{
	scanf( "%d", &n );
	rep( i, n )
	{
		rep( j, n )
		{
			int a;
			scanf( "%d", &a );
			if( ~a )
				es.push_back( edge( i, j, a ) );
		}
	}

	std::sort( es.begin(), es.end(), []( const edge& e1, const edge& e2 ){ return e1.cost < e2.cost; } );

	init();

	int ans = 0;
	rep( i, es.size() )
	{
		if( !same( es[i].from, es[i].to ) )
		{
			ans += es[i].cost;
			unite( es[i].from, es[i].to );
		}
	}

	printf( "%d\n", ans );

	return 0;
}