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

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

void visit(){
  int ima = 0;
  int mine = 100000000;
  
  
  for(int k=0;k<n-1;k++) {
    mine = 10000000;
    for(int i=0;i<n;i++) {
      if(flg[i] == 0 && mine > t[i]) {
	mine = t[i];
	ima = i;
      }
    }
    sum += mine;
    flg[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++) {
    flg[i] = 0;
    t[i] = 100000000;
  }
  
  t[0] = 0;
  flg[0] = 1;
  for(int i=0;i<n;i++) 
    if(a[0][i] != -1) t[i] = a[0][i];

  visit();

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

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

int n, M[100][100];

int prim()
{
	int i,u,v,min_v,sum;
	int d[100],p[100],c[100];
	
	for(i=0;i<=n-1;i++)
	{
		d[i]=(1<<21);
		p[i]=-1;
		c[i]=0;
	}
	
	d[0]=0;
	
	while(1)
	{
		min_v=(1<<21);
		u=-1;
		for(i=0;i<=n-1;i++)
		{
			if(min_v > d[i] && c[i] != 2)
			{
				u=i;
				min_v=d[i];
			}
		}
		if(u==-1) break;
		c[u]=2;
		for(v=0;v<=n-1;v++)
		{
			if(c[v] != 2 && M[u][v] != (1<<21))
			{
				if(d[v] > M[u][v])
				{
				    d[v]=M[u][v];
				    p[v]=u;
				    c[v]=1;
				}
			}
		}
	}
	sum=0;
	for(i=0;i<=n-1;i++)
	{
		if(p[i] != -1) sum+=M[i][p[i]];
	}
	return sum;
}

int main()
{
	int i,j,e;
	cin >> n;
	
	for(i=0;i<=n-1;i++)
	{
		for(j=0;j<=n-1;j++)
		{
			cin >> e;
			if(e == -1) M[i][j]=(1<<21);
			else M[i][j]=e;
		}
	}
	
	cout << prim() << endl;
	return 0;
}

C++: #include <iostream>
#include <cstdio>
using namespace std;
static const int  N = 100;
static const int  INFTY = (1<<21);
int M[N][N], color[N];
int n, d[N], p[N];
int ans = 0;

int prim()
{
    for (int i = 0; i < n; i++)
    {
        color[i] = -1;
        d[i] = INFTY;
        p[i] = -1;
    } 
    d[0] = 0;
    int u = -1;
    while (true)
    { 
        int mincost = INFTY;
        u = -1;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != 2 && d[i] < mincost)
            { 
                mincost = d[i];
                u = i;
            }  
        } 

        if (mincost == INFTY) break;
        color[u] = 2;
        for (int i = 0; i < n; i++)
        {
            if (color[i] != 2 && M[u][i] != INFTY)
            { 
                if (M[u][i] < d[i])
                { 
                    d[i] = M[u][i];
                    p[i] = u;
                    color[i] = 1;
                }  
            }  
        } 
    }

    for (int i = 0; i < n; i++)
    {
        if (color[i] == 2 && p[i] != -1) ans += M[i][p[i]];

    } 
    return ans;
}

int main()
{
    cin>>n;
    for (int i = 0; i < n; i++)
    {
        for (int j = 0; j < n; j++)
        {
            int a;
            cin>>a;
            if(a != -1) M[i][j] = a;
            else M[i][j] = INFTY;
        }  
    } 
    prim();
    cout<<ans<<endl;    
    return 0;
}

C++: #include<stdio.h>

static const int MAX = 500;
static const int INFTY = (1<<21);

main(){
  int n, i, j, e, sum,M[MAX][MAX]={0};
  int u,minV,INT_MAX,A[MAX]={INFTY},key[MAX]={INFTY};
  /*

    your code


   */

  scanf("%d", &n);
  
  for ( i = 0; i< n; i++ ){
    for ( j = 0; j < n; j++ ){
      scanf("%d", &e);
      M[i][j] = (e==-1)?INFTY:e;
    }
  }
  
  
  while(){
    minV = 
      for(i=1;i<=n;i++){
        if((i < minV) && 

  






















  sum = 0;

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

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

  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<algorithm>
#include<cmath>
#include<cstring>
#include<vector>
#include<queue>
using namespace std;
typedef long long ll;

static const int INF = 1e9;
static const int MAX = 100;

int n;
struct edge{int to, cost;};
typedef pair<int, int> P;

int mincost[MAX];
bool used[MAX];
vector< vector<edge> > A(MAX);

int prim(){
    int to, cost, res = 0;
    priority_queue<P, vector<P>, greater<P>> que;
    fill(mincost, mincost + n, INF);
    fill(used, used + n, false);
    mincost[0] = 0;
    que.push(P(0, 0));

    while(!que.empty()){
        P p = que.top(); que.pop();
        if (used[p.second]) continue;
        used[p.second] = true;

        for (int i = 0; i < A[p.second].size(); i++){
            to = A[p.second][i].to;
            cost = A[p.second][i].cost;
            if (used[to]) continue;
            if (mincost[to] > cost){
                mincost[to] = cost;
                que.push(P(mincost[to], to));
            }
        }
    }
    for (int i = 0; i < n; i++) res += mincost[i];
    return res;
}

int main(){
    cin >> n;
    edge x;
    for (int i = 0; i < n; i++){
        for (int j = 0; j < n; j++){
            scanf("%d", &x.cost);
            x.to = j;
            if (x.cost != -1) A[i].push_back(x);
        }
    }
    cout << prim() << endl;
    return 0;
}