Go: package main

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

const (
	MAX_N      = 200
	MAX_WEIGHT = 2000
)

type Edge struct {
	start  int
	end    int
	weight int
}

type ShortestInfo struct {
	edges    []Edge
	shortest int
}

func main() {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)

	n, err := nextInt(sc)
	if err != nil {
		panic(err)
	}

	shortestList := make([]ShortestInfo, n)
	for i := 1; i < n; i++ {
		shortestList[i] = ShortestInfo{edges: []Edge{}, shortest: MAX_N*MAX_WEIGHT + 1}
	}

	adjacentMatrix := make([][]int, n)
	for i := 0; i < n; i++ {
		adjacentMatrix[i] = make([]int, n)
		for j := 0; j < n; j++ {
			v, err := nextInt(sc)
			if err != nil {
				panic(err)
			}
			adjacentMatrix[i][j] = v
		}
	}

	mst([]int{1}, shortestList, adjacentMatrix)
	fmt.Println(shortestList)
	fmt.Println(calculateMinWeight(shortestList))
}

func calculateMinWeight(shortestList []ShortestInfo) int {
	visitedList := make([]bool, len(shortestList))
	visitedList[0] = true
	sumWeight := 0
	for !visitedAll(visitedList) {
		shortestInfo := shortestList[0]
		shortestList = shortestList[1:]
		edges := shortestInfo.edges
		for _, edge := range edges {
			if !visitedList[edge.end-1] {
				visitedList[edge.end-1] = true
				sumWeight += edge.weight
			}
		}
	}
	return sumWeight
}

func visitedAll(visitedList []bool) bool {
	for _, v := range visitedList {
		if !v {
			return false
		}
	}
	return true
}

func mst(nodeStack []int, shortestList []ShortestInfo, adjacentMatrix [][]int) {
	for len(nodeStack) > 0 {
		currentNode := nodeStack[0]
		nodeStack = nodeStack[1:]
		currentShortest := getShortest(currentNode, shortestList)
		adjacents := findAdjacents(currentNode, adjacentMatrix)
		for i := 0; i < len(adjacents); i++ {
			adj := adjacents[i]
			s := getShortest(adj.end, shortestList)
			if currentShortest.shortest+adj.weight < s.shortest || (currentShortest.shortest+adj.weight == s.shortest && len(currentShortest.edges)+1 > len(s.edges)) {
				setShortest(adj.end, adj, currentShortest, shortestList)
				nodeStack = append(nodeStack, adj.end)
			}
		}
	}
}

func getShortest(node int, shortestList []ShortestInfo) ShortestInfo {
	return shortestList[node-1]
}

func setShortest(node int, edge Edge, currentShortest ShortestInfo, shortestList []ShortestInfo) {
	newEdges := make([]Edge, len(currentShortest.edges)+1)
	for i := 0; i < len(newEdges)-1; i++ {
		newEdges[i] = currentShortest.edges[i]
	}
	newEdges[len(newEdges)-1] = edge
	shortestInfo := ShortestInfo{edges: newEdges, shortest: currentShortest.shortest + edge.weight}
	shortestList[node-1] = shortestInfo
}

func findAdjacents(currentNode int, adjacentMatrix [][]int) []Edge {
	adjacents := adjacentMatrix[currentNode-1]
	edges := []Edge{}
	for i, v := range adjacents {
		if v > 0 {
			edges = append(edges, Edge{start: currentNode, end: i + 1, weight: v})
		}
	}
	return edges
}

func nextInt(sc *bufio.Scanner) (int, error) {
	sc.Scan()
	return strconv.Atoi(sc.Text())
}

C++: #include <algorithm>
#include <vector>
#include <iostream>

typedef std::vector<int> TTree;
struct TPath
{
    int Id1;
    int Id2;
    int Cost;
    TPath(int AId1, int AId2, int ACost)
     : Id1(AId1), Id2(AId2), Cost(ACost)
    {}
};
bool Compare(const TPath& Left, const TPath& Right)
{
    if(Left.Cost  < 0) return false;
    if(Right.Cost < 0) return true;
    return Left.Cost < Right.Cost;
}


int main()
{
    int n;
    std::cin >> n;
    std::vector<TTree*> Table(n, NULL);
    for(std::size_t i = 0; i < n; ++i){
        Table[i] = new std::vector<int>(1, i);
    }
    std::vector<TPath> PathList;
    PathList.reserve(n*n);
    
    for(int i = 0; i < n; ++i){
        for(int j = 0; j < n; ++j){
            int Cost;
            std::cin >> Cost;
            PathList.push_back(TPath(i, j, Cost));
        }
    }
    std::sort(PathList.begin(), PathList.end(), Compare);
    
    int Total = 0;
    for(std::vector<TPath>::const_iterator It = PathList.begin();
        It != PathList.end(); ++It)
    {
        const TPath& Path = *It;
        TTree* Tree1 = Table[Path.Id1];
        TTree* Tree2 = Table[Path.Id2];
        if(Tree1 == Tree2){
            continue;
        }
        Tree1->insert(Tree1->end(), Tree2->begin(), Tree2->end());
        for(std::size_t j = 0; j < Tree2->size(); ++j){
            int Id = (*Tree2)[j];
            Table[Id] = Tree1;
        }
        delete Tree2;
        Total += Path.Cost;
        // std::cout << "[" << Path.Id1 << "]" << "[" << Path.Id2 << "] = " << Path.Cost << std::endl;
    }
    std::cout << Total << std::endl;
    return 0;
}

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

int root(int a,int P[])
{
    if(P[a] == a)
        return a;
    else    return (P[a] = root(P[a],P));
}
int is_same_set(int a,int b,int P[])
{
    return root(a,P) == root(b,P);
}

int main()
{
    int n,i,j,M=0,minsum=0;
    cin >> n;
    //init(n);
    int a[n][n],cost[n*n],src[n*n],dst[n*n];
    for(i=0;i<n;i++)
        for(j=0;j<n;j++)
            cin >> a[i][j];
    for(int r=0;r<n;r++)
        for(int c=r+1;c<n;c++)
            if(a[r][c] > -1)
            {
                cost[M] = a[r][c];
                src[M] = r;
                dst[M] = c;
                M++;
            }
    pair<int,int> edeges[M];
    int P[n];
    for(i=0;i<n;i++)
        P[i] = i;
    for(i=0;i<M;i++)
    {
        edeges[i].first = cost[i];
        edeges[i].second = i;
    }
    sort(edeges,edeges+M);
    for(i=0;i<M;i++)
    {
        if(is_same_set(src[edeges[i].second], dst[edeges[i].second], P))
            continue;
        else    {
                    P[root(src[edeges[i].second],P)] = root(dst[edeges[i].second],P);
                    minsum += edeges[i].first;
                }
    }
    cout << minsum << endl;
    return 0;
}

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

vector< vector < int > > adjMatrix;

int findMSTCost(int n) {
  vector< bool > visited (n, false);
  visited[0] = true;
  int cost = 0;
  for (int numberVertices = 1; numberVertices < n; numberVertices++) {
    int minEdge = 3000, minVertex = n;
    for (int i = 0; i < n; i++) {
      for (int j = 0; j < n; j++) {
	if ((i != j) &&
	    (visited[i] == true) &&
	    (visited[j] == false) &&
	    (adjMatrix[i][j] != -1) &&
	    (adjMatrix[i][j] < minEdge)) {
	  minEdge = adjMatrix[i][j];
	  minVertex = j;
	}
      }
    }
    cost += minEdge;
    visited[minVertex] = true;
  }
  return cost;
}

int main(int argc, char **argv) {
  ios_base::sync_with_stdio(false);
  int n;
  cin >> n;
  adjMatrix = vector< vector < int > > (n, vector< int > (n, 0));
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      cin >> adjMatrix[i][j];
  cout << findMSTCost(n) << endl;
  return 0;
}