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>
#include<cstdio>
#include<cstring>
#define white 0
#define black -1
#define grey 1
#define maxn 100
#define infly 1<<21
using namespace std;

int n,m[maxn+5][maxn+5],d[maxn+5],p[maxn+5],color[maxn+5];

int prim()
{
    int u,minv;

    for(int i=0;i<n;i++)
    {
        d[i]=infly;
        p[i]=-1;
        color[i]=white;
    }

    d[0]=0;

    while(1)
    {
        minv=infly;
        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]!=infly)
            {
                if(d[v]>m[u][v])
                {
                    d[v]=m[u][v];
                    p[v]=u;
                    color[v]=grey;
                }
            }
        }
    }

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

    return sum;
}

int main()
{
    scanf("%d",&n);
    for(int i=0;i<n;i++)
    {
        for(int j=0;j<n;j++)
        {
            int x;
            scanf("%d",&x);
            if(x==-1) m[i][j]=infly;
            else m[i][j]=x;
        }
    }
    printf("%d\n",prim());
    return 0;
}

C++: #include <iostream>
#include <algorithm>
#include <string>
#include <stack>
#include <cstdlib>
#include <queue>
#include <vector>
#include <set>
#include <stdio.h>
#include <cstdio>
using namespace std;
int p[100001],s[100001],t[100001],cost[100001];
int root(int a) {
	if (p[a] == a) return a;
	return (p[a] = root(p[a]));
}
void unite(int a,int b){
	p[root(a)] = root(b);
}
bool same(int a ,int b){
	if (root(a) == root(b)) return 1;
	else return 0;
}
bool rcheck(int a){
	if (root(a) == a) return 1;
	else return 0;
}
pair <int,int> path[100001];
int main() {
	int n,coun = 0;
	cin >> n;
	for (int i = 0; i < n; i++){
		p[i] = i;
		for (int j = 0;j<n;j++) {
			s[n*i+j] = i; t[n*i+j]=j; 
			cin >> cost[n*i+j];
		}
	}
	for (int i = 0;i < n*n;i++) {
		path[i].first = cost[i];
		path[i].second = i;
	}
	sort(path,path+n*n);
	for (int i = 0; i < n*n;i++) {
		if (path[i].first >= 0) {
			if(same(s[path[i].second],t[path[i].second]) == 0) {
				unite(s[path[i].second],t[path[i].second]);
				coun += path[i].first;
			}
		}
	}
	cout << coun << endl;
}

C++: #include <iostream>
#include <algorithm>
#define N 100
#define INF 2000000000
#define WHITE 0
#define GRAY 1
#define BLACK 2
using namespace std;

int n;
int M[N][N],color[N],d[N],p[N];
   //M??????????????????color????¨??????¶???(0,1,2),d????????????????°???????????¨????
   //p???MST???????°???¨?????¨??????????????????????????????

void Prim() {
	for (int i = 0;i < n;i++) d[i] = INF;
	d[0] = 0;  //?????????????????????????????§???????????§????´???????????????????????????????
	p[0] = -1;  //??????????????????????????????????????????

	int u;
	while (true) {
		int min = INF;
		for (int i = 0;i < n;i++) {
			if(color[i]!=BLACK&&min>d[i]){
				min = d[i];
				u = i;
			}
		}

	if (min == INF) 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++) sum += d[i];
	cout << sum << endl;
		
}

int main() {
	cin >> n;
	for (int i = 0;i < n;i++)
		for (int j = 0;j < n;j++) {
			cin >> M[i][j];
			if (M[i][j] == -1) M[i][j] = INF;
		}

	Prim();

}

C++: #include <iostream>

using namespace std;

static const int N = 100;
static const int INF = 2001;

struct Vertex {
    bool is_visit;
    int min_w;
    int parent;
};

class MST {
private:
    int matrix[N][N];
    Vertex vertex[N];
    int n;

public:
    MST()
    {
        cin >> n;
        for(int i = 0; i < n; i++)
        {
            for(int j = 0; j < n; j++)
            {
                cin >> matrix[i][j];
                if(matrix[i][j] == -1)
                    matrix[i][j] = INF;
            }
        }
    }

    void prim()
    {
        for(int i = 0; i < n; i++)
        {
            vertex[i].is_visit = false;
            vertex[i].min_w = INF;
        }
        vertex[0].min_w = 0;
        vertex[0].parent = -1;

        while(true)
        {
            int u = -1;
            int mincost = INF;
            for(int i = 0; i < n; i++)
            {
                if(!vertex[i].is_visit && vertex[i].min_w < mincost)
                {
                    mincost = vertex[i].min_w;
                    u = i;
                }
            }

            if(mincost == INF)
                break;

            vertex[u].is_visit = true;

            for(int v = 0; v < n; v++)
            {
                if(!vertex[v].is_visit && matrix[u][v] != INF)
                {
                    if(matrix[u][v] < vertex[v].min_w)
                    {
                        vertex[v].min_w = matrix[u][v];
                        vertex[v].parent = u;
                    }
                }
            }
        }
    }

    void print()
    {
        int sum = 0;
        for(int i = 0; i < n; i++)
            if(vertex[i].parent != -1)
                sum += matrix[i][vertex[i].parent];
        cout << sum << endl;
    }
};

int main()
{
    MST mst;
    mst.prim();
    mst.print();
}

C++: #include<bits/stdc++.h>
#define INF 0x3f3f3f3f
using namespace std;
const int MAXN = 105;

int m[105][105];
int n,e;

int prim(int s)
{
    int minv,cnt[MAXN],k,ans = 0;
    for(int i = 1; i <= n; ++i)
        cnt[i] = m[s][i];
    cnt[s] = 0;
    for(int i = 1; i < n; ++i)
    {
        minv = INF;
        for(int j = 1; j <= n; ++j)
        {
            if(cnt[j] && cnt[j]<minv)
                minv = cnt[j],k = j;
        }
        cnt[k] = 0;
        ans += minv;
        for(int j = 1; j <= n; ++j)
            if(m[k][j] < cnt[j])
                cnt[j] = m[k][j];
    }
    return ans;
}

int main(void)
{
    cin>>n;
    for(int i = 1; i <= n; ++i)
    {
        for(int j = 1; j <= n; ++j)
        {
            cin>>e;
            m[i][j] = e==-1?INF:e;
        }
    }
    cout<<prim(1)<<'\n';
    return 0;
}