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 <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (int)(n); i++)
#define repr(i, n) for (int i = (int)(n); i >= 0; i--)
#define REP(i, m, n) for (int i = (int)(m); i <= (int)(n); i++)
#define all(v) v.begin(), v.end()
typedef long long ll;
typedef pair<int,int> pii;
#define fi first
#define se second
#define mp make_pair

template<class T> inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; }
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

//const long long INF = 1LL << 60;
const int INF = 1001001001;
const int MAX = 110;

//struct edge{
//  int to, cost;
//  edge(){}
//  edge(int to, int cost): to(to), cost(cost){}
//};

int G[MAX][MAX];
bool seen[MAX];
priority_queue<pii, vector<pii>, greater<pii> > pq;
int n;

int prim(){
  seen[0] = true;
  rep(i, n){
    if(G[0][i] == -1) continue;
    pq.push(mp(G[0][i], i));
  }
  
  int sum = 0;
  while(!pq.empty()){
    pii p = pq.top();
    pq.pop();
    if(seen[p.se]) continue;
    seen[p.se] = true;
    sum += p.fi;
    rep(i, n){
      if(G[p.se][i] == -1) continue;
      pq.push(mp(G[p.se][i], i));
    }
  }

  return sum;
}

int main(){
  cin >> n;
  rep(i, n) rep(j, n) cin >> G[i][j];

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

C++: 

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<stack>
#include<queue>
#include<vector>
#include<algorithm>
#include<string>
#include<iostream>
using namespace std;
typedef long long ll;
int mod(ll a){
    int c=a%1000000007;
    if(c>=0)return c;
    else return c+1000000007;
}
typedef pair<int,int> i_i;

#define inf 1<<21
/*
int map[5][5];
int p[5];
int d[5];
bool color[5];

*/
int main(){
    int n;cin>>n;
    int map[n][n];int p[n];int d[n];bool color[n];
    for(int i=0;i<=n-1;i++)for(int j=0;j<=n-1;j++){
        int k;cin>>k;
        map[i][j]=k==-1?inf:k;
    }
    for(int i=0;i<=n-1;i++)p[i]=-1;
    for(int i=0;i<=n-1;i++)d[i]=inf;
    for(int i=0;i<=n-1;i++)color[i]=false;
    
    /*color[0]=true;*/
    d[0]=0;
    
    while(1){
        int mincost=inf;
        int u=-1;
        for(int i=0;i<=n-1;i++){
            if(color[i]==false&&mincost>d[i]){
                mincost=d[i];
                u=i;
            }
        }
        if(u==-1)break;
        
        color[u]=true;
        for(int i=0;i<=n-1;i++){
            if(color[i]==false&&d[i]>map[u][i]){
                d[i]=map[u][i];
                p[i]=u;
            }
        }
        
    }
    ll sum=0;
    for(int i=1;i<=n-1;i++){
        sum+=map[i][p[i]];
    }
    cout<<sum<<endl;
    return 0;
}

C++: #include <stdio.h>

#define MAX_N (105)
#define INF (10000)
int G[MAX_N][MAX_N];
int N;

int dist[MAX_N];
bool arrived[MAX_N];

int prim()
{
    int ret = 0;
    for ( int i = 0; i < N; i++ ) {
	arrived[i] = false;
	dist[i] = INF;
    }

    dist[0] = 0;

    while ( true ) {
	int min_dist = INF;
	int u = 0;
	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && dist[i] < min_dist ) {
		min_dist = dist[i];
		u = i;
	    }
	}
	
	if ( min_dist == INF ) break;

	ret += dist[u];
	arrived[u] = true;

	for ( int i = 0; i < N; i++ ) {
	    if ( !arrived[i] && (G[u][i] != -1) && (dist[i] > G[u][i]) ) {
		dist[i] = G[u][i];
	    }
	}
    }

    return ret;
}

int main()
{
    int ans = 0;
    scanf("%d", &N);

    for ( int i = 0; i < N; i++ ) {
	for ( int j = 0; j < N; j++ ) {
	    scanf("%d", &G[i][j]);
	}
    }

    ans = prim();

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

    return 0;
}

C++: #include<iostream>
#include<cstdio>
#define N 101
using namespace std;

int A[N][N];
int n;
int father[N];

int getparent(int i){
    while(father[i]!=i){
        i=father[i];
    }
    return i;
}
void mergeGroup(int a,int b){
    int fa=getparent(a);
    int fb=getparent(b);
    father[fb]=fa;
}

int prim(){
    int ans=0;
    int c=0;
    int minval=4000;
    int ab,ae;
    while(c<n-1){
        int b,e;
        minval=4000;
        for(b=0;b<n;b++){
            for(e=b+1;e<n;e++){
                if(A[b][e]!=-1 && A[b][e]<minval &&getparent(b)!=getparent(e)){
                    minval=A[b][e];
                    ab=b;
                    ae=e;
                    //printf("b= %d  e= %d \n",b,e);
                }
            }
        }
        ans+=A[ab][ae];
        //printf("ab= %d  ae= %d  mincost=%d \n ",ab,ae,minval);
        c++;
        mergeGroup(ab,ae);
    }
    return ans;
}

int main(){

    int k,i,j;
    cin>>n;
    for(int i=0;i<n;i++){
        father[i]=i;
        for(int j=0;j<n;j++){
            cin>>A[i][j];
        }
    }
    cout<<prim()<<endl;
    return 0;
}