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>
#define REP(i,n) for (int i = 0; i <(n); ++i)
#define REP2(i,x,n) for (int i = x; i <(n); ++i)
#define ALL(v) v.begin(), v.end()
#define RALL(v) v.rbegin(), v.rend()
using namespace std;
using ll = long long;
using P = pair<int,int>;
static const double PI = acos(-1);
static const int INF = 1e9+7;

/*
  最小全域木

  重み付きグラフの最小全域木の辺の重みの総和を計算する．
  最小全域木とは？：グラフで，すべての頂点に到達できるものの中で，　辺の重みが最小のもの
  プリムのアルゴリズム

*/

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 d[MAX], p[MAX], color[MAX];
  //d: MSTに属する頂点とそれ以外の頂点の中で，重みが最小の辺の重み
  //p: p[v]  MSTにおける頂点vの親
  //color 訪問状態

  //初期化
  for(int i = 0; i < n; i++){
    d[i] = INFTY;
    p[i] = -1;
    color[i] = WHITE;
  }

  d[0] = 0;

  while( true ){
    minv = INFTY;
    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 && M[u][v] != INFTY){
        if( d[v] > M[u][v] ){
          d[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;

  //入力
  REP(i,n)REP(j,n){
    int e;
    cin >> e;
    if(e == -1) M[i][j] = INFTY;
    else M[i][j] = e;
  }

  cout << prim() << endl;

  return 0;
}

C++: #include<iostream>
#include<algorithm>
#define MAX 120
#define INFTY (1 << 21)
#define WHITE -1
#define GRAY 0
#define BLACK 1

using namespace std;

int A[MAX], M[MAX][MAX];
int n;
int color[MAX], d[MAX], p[MAX];

void prim(){
    int u, mini;
    
    for(int i=0; i<n; i++){
        color[i] = WHITE;
        d[i] = INFTY;
        p[i] = -1;
    }//すべて初期化する
    
    d[0] = 0;
    
    while(1){
        mini = INFTY;
        
        for(int i=0; i<n; i++){
            if( color[i] != BLACK && d[i] < mini){
                mini = d[i];
                u=i;
            }
        }
        
        if(mini == 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++){
        if(p[i] != -1){
            sum = sum + M[i][p[i]];
        }
    }
    cout << sum << endl;
}

int main(){
    
    cin >> n;
    
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            M[i][j] = -1;
        }
    }
    
    for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cin >> M[i][j];
        }
    }
    
    prim();
    
    /*for(int i=0; i<n; i++){
        for(int j=0; j<n; j++){
            cout << M[i][j] << " ";
        }
        cout << endl;
        
    }*/
    
    return 0;
}

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

typedef int Weight;
struct Edge{
	int src,dst;
	Weight weight;
};

bool asc(const Edge &e,const Edge &f){
	return e.weight < f.weight;
}

vector<Edge> Edges;
vector<int> L(100005,-1);

int check(Edge &e,int n){
	int j;
	if (L[e.src]!=L[e.dst]){
		int t = L[e.dst];
		for (j=0;j<n;j++){
			if (L[j]==t){
				L[j]=L[e.src];
			}
		}
		return 1;
	}else{
		return 0;
	}
}

int main(){
	int n,m,i,j,w;
	cin >> n ;
	
	for (i=0;i<n;i++){
		for(j=0;j<n;j++){
			cin >> w;
			if ((w!=-1)&&(i>j)){
				Edge edge={i,j,w};
				Edges.push_back(edge);
			}
		}
	}
	
	sort(Edges.begin(),Edges.end(),asc);
	
	m = Edges.size();
	
	for (j=0;j<n;j++){
		L[j]=j;
	}
	
	int TotalWeight=0;
	int count=0;
	int k;
	for (i=0;i<m;i++){
		if (count==n-1){break;}
		if (check(Edges[i],n)){
			TotalWeight+=Edges[i].weight;
			count++;
		}
	}
	cout << TotalWeight;
	return 0;
}

C++: #include <bits/stdc++.h>
#define INF 1000000000
#define MAX_N 100
using namespace std;
typedef pair<int, int> P;
struct edge {
	int to, cost;
	edge(int to_, int cost_) : to(to_), cost(cost_) {}
};

int n;
vector<edge> G[MAX_N];

int Prim() {
	int res = 0;
	vector<bool> used(n, false);
	priority_queue<P, vector<P>, greater<P>> que;
	que.push(P(0, 0));
	while (!que.empty()) {
		P p = que.top(); que.pop();
		int v = p.second;
		if (used[v]) continue;
		used[v] = true;
		res += p.first;
		for (int i = 0; i < G[v].size(); i++) {
			edge e = G[v][i];
			que.push(P(e.cost, e.to));
		}
	}
	return res;
}

int main()
{
	cin >> n;
	for (int i = 0, c; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> c;
			if (c != -1)
				G[i].push_back(edge(j, c));
		}
	}
	cout << Prim() << endl;
	return 0;
}