Go: package main

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

//util
var sc = bufio.NewScanner(os.Stdin)

func nextStr() string {
	sc.Scan()
	return sc.Text()
}

func nextInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func printList(list []int) {
	n := len(list)
	buf := bufio.NewWriter(os.Stdout)
	for _, v := range list[0 : n-1] {
		buf.Write([]byte(fmt.Sprintf("%d ", v)))
	}
	buf.Write([]byte(fmt.Sprintf("%d\n", list[n-1])))
	buf.Flush()
}

func max(a, b int) int {
	if a > b {
		return a
	} else {
		return b
	}
}

func min(a, b int) int {
	if a > b {
		return b
	} else {
		return a
	}
}

//solve
const (
	MAX   = 100
	INFTY = (1 << 21)
	WHITE = 0
	GRAY  = 1
	BLACK = 2
)

//V: グラフの頂点全体の集合
//T: MSTに属する頂点の集合
var (
	n     int
	M     [MAX][MAX]int //重みを記録する隣接行列
	color [MAX]int
	p     [MAX]int //MSTにおける頂点の親を記録
	d     [MAX]int //Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを記録
)

func prim() int {
	var u, minv int
	//初期化
	for i := 0; i < n; i++ {
		d[i] = INFTY
		p[i] = -1
		color[i] = WHITE
	}

	d[0] = 0

	for {
		//頂点uを決めていく
		//つまり、Tに属する頂点と、V−Tに属する頂点を繋ぐ辺の中で、重みが最小のものを決める
		minv = INFTY
		u = -1
		for i := 0; i < n; i++ {
			//未訪問かつ、辺の重みが最小となるuを選ぶ
			if minv > d[i] && color[i] != BLACK {
				u = i
				minv = d[i]
			}
		}

		if u == -1 {
			break
		}

		color[u] = BLACK
		for 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
				}
			}
		}
	}

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

func main() {
	sc.Split(bufio.ScanWords)
	n = nextInt()

	for i := 0; i < n; i++ {

		for j := 0; j < n; j++ {
			tmp := nextInt()
			if tmp == -1 {
				tmp = INFTY
			}
			M[i][j] = tmp
		}

	}

	fmt.Println(prim())

}

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;
}