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 <iostream>
using namespace std;

int n;
int a[101][101];
int flag[101];
int t[101];
int sum = 0;

void prim(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n-1;k++) {
    mine = 10000000;
    for(int i=0;i<n;i++) {
      if(flag[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flag[ima] = 1;
    
    for(int i=0;i<n;i++) 
      if(a[ima][i] != -1) 
	if(t[i] > a[ima][i] ) t[i] = a[ima][i];
    
    
  }
}

int main() {

  cin >> n;

  for(int i=0;i<n;i++) 
    for(int j=0;j<n;j++) 
      cin >> a[i][j];
  
  for(int i=0;i<n;i++) {
    flag[i] = 0;
    t[i] = 100000000;
  }
  
  t[0] = 0;
  flag[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  prim();

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

C++: #include<iostream>
#include<vector>
#include<queue>
using namespace std;

struct Edge{
  int weight;
  int to;
  Edge(int w,int t):weight(w),to(t){}
};

const int inf = 1<<28;

bool operator>(Edge e1,Edge e2){
  if(e1.weight != e2.weight) return (e1.weight > e2.weight);
  else return (e1.to > e2.to);
}

int main(){
  int n;
  cin>>n;
  vector<vector<Edge> > Graph(n);
  for(int i=0;i<n;i++){
    for(int j=0;j<n;j++){
      int temp;
      cin>>temp;
      if(temp != -1){
	Graph[i].push_back(Edge(temp,j));
      }
    }
  }
  priority_queue<Edge,vector<Edge>,greater<Edge> > pq;
  pq.push(Edge(0,0));
  vector<bool> used(n,false);
  vector<int> cost(n,inf);
  int ans = 0;
  while(pq.size()){
    Edge e = pq.top();
    pq.pop();
    if(used[e.to]) continue;
    used[e.to] = true;
    ans += e.weight;
    for(int i=0;i<Graph[e.to].size();i++){
      Edge &edge = Graph[e.to][i]; 
      if(cost[edge.to] > edge.weight){
	cost[edge.to] = edge.weight;
	pq.push(edge);
      }
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #if 0
????????????????§£????????????????????????????????????????????????AOJ????????????????????????????????????wrong???answer?????????????????????
????????§?????????????????\????????????????????£??????????????§??????????????????????????????????????¨???????????????
???????????????????????? Union Find Tree ???????????????????????????????????????????????????????????????????????????Root?????¢?????°??????????????¨??§??????
??????????????§???????????????????????????????????????????????¢??°???????????¢??°?????????????????£??????????????°???????????°???????\???????????????¨???????????¨???
????????????????????????
Minimum Spanning Tree ???????????§???????????¶??????????????????????????¨????????????????????¨?????????????????????????????????????????????????????£?????????
???????????????????????????????????????
#endif

#include <iostream>
#include<algorithm>
using namespace std;


struct edge
{
   int cost;
   int src;
   int dst;
};

edge e[5000];
int A[100][100];
int M=0;
int P[100];

void init(int N)
{
  for (int i=0; i<=N; ++i) P[i] = i;
}

int root(int a)
{
  if (P[a] == a) return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b)
{
  return root(a) == root(b);
}

void unite(int a, int b)
{
  P[root(a)] = root(b);
}

void checkEdges(int n)
{
    for(int r=0;r<n;r++)
    {
        for(int c=r+1;c<n;c++)
            {
                if(A[r][c]>-1)
                    {
                        e[M].cost=A[r][c];
                        e[M].src=r;
                        e[M].dst=c;
                        M+=1;
                    }
            }
    }
}

bool cmp(edge e1,edge e2)
{
    return e1.cost<=e2.cost;
}
int main()
{
    int n,count=0;
    cin>>n;
    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
                cin>>A[i][j];
        }
    }
    checkEdges(n);
    sort(e,e+M,cmp);
    for (int i=0; i<M; ++i)
    {
        if(is_same_set(e[i].src,e[i].dst))
        {
            continue;
        }
        unite(e[i].dst,e[i].src);
        count+=e[i].cost;
    }
    cout<<count<<endl;
    return 0;
}

C++: #include <iostream>
#include<algorithm>
using namespace std;


struct edge
{
   int cost;
   int src;
   int dst;
};

edge e[5000];
int A[100][100];
int M=0;
int P[100];

void init(int N)
{
  for (int i=0; i<=N; ++i) P[i] = i;
}

int root(int a)
{
  if (P[a] == a) return a;
  return (P[a] = root(P[a]));
}

bool is_same_set(int a, int b)
{
  return root(a) == root(b);
}

void unite(int a, int b)
{
  P[root(a)] = root(b);
}

void checkEdges(int n)
{
    for(int r=0;r<n;r++)
        for(int c=r+1;c<n;r++)
    {
        if(A[r][c]>-1)
        {
            e[M].cost=A[r][c];
            e[M].src=r;
            e[M].dst=c;
            M+=1;
        }
    }
}

bool cmp(edge e1,edge e2)
{
    return e1.cost<=e2.cost;
}
int main()
{
    int n,count=0;
    cin>>n;
    init(n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++){
                cin>>A[i][j];
        }
    }
    checkEdges(n);
    sort(e,e+M,cmp);
    for (int i=0; i<M; ++i)
    {
        if(is_same_set(e[i].src,e[i].dst))
        {
            continue;
        }
        unite(e[i].dst,e[i].src);
        count+=e[i].cost;
    }
    cout<<count<<endl;
    //system("pause");
    return 0;
}