Go: package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

type Node struct {
	id     int
	weight int
}

func init() {
	sc.Split(bufio.ScanWords)
}

type NodeHeap []Node

func (h NodeHeap) Len() int           { return len(h) }
func (h NodeHeap) Less(i, j int) bool { return h[i].weight < h[j].weight }
func (h NodeHeap) Swap(i, j int)      { h[i], h[j] = h[j], h[i] }

func (h *NodeHeap) Push(x interface{}) {
	*h = append(*h, x.(Node))
}

func (h *NodeHeap) Pop() interface{} {
	old := *h
	n := len(old)
	x := old[n-1]
	*h = old[0 : n-1]
	return x
}

var sc = bufio.NewScanner(os.Stdin)

func scanInt() int {
	num, _ := strconv.Atoi(scanString())
	return num
}

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

func init() {
	sc.Split(bufio.ScanWords)
}

func main() {
	n := scanInt()

	list := make([][]int, n)
	seen := make([]bool, n)
	for i := range list {
		list[i] = make([]int, n)
		for j := range list[i] {
			list[i][j] = scanInt()
		}
	}

	start := Node{id: 0, weight: 0}
	h := &NodeHeap{start}
	heap.Init(h)

	sum := 0
	for h.Len() > 0 {
		node := heap.Pop(h).(Node)
		if seen[node.id] {
			continue
		}
		seen[node.id] = true
		sum += node.weight

		for i, w := range list[node.id] {
			if w < 0 {
				continue
			}
			heap.Push(h, Node{id: i, weight: w})
		}
	}

	fmt.Println(sum)
}

C++: #include <bits/stdc++.h>

using namespace std;

int n;
const int N = 110;
int A[N][N];
typedef pair<int, int> P;
vector<P> G[N];

struct Edge {
    int u, v, cost;
    
    bool operator<(const Edge& other) const {
        return cost < other.cost;
    }
};

vector<Edge> edges;
vector<int> parent(N), height(N);

int min_cost = 0;

void prim() {
    priority_queue<P, vector<P>, greater<P>> heap;
    heap.push({0, 1});
    vector<bool> vis(n + 1, false);
    vector<int> dis(n + 1, INT_MAX);

    while (!heap.empty()) {
        int u = heap.top().second, cost = heap.top().first;
        heap.pop();
        if (vis[u]) continue;
        vis[u] = true;
        min_cost += cost;
        for (int i = 0; i < G[u].size(); i++) {
            int v = G[u][i].second, len = G[u][i].first;
            if (!vis[v] && len < dis[v]) {
                dis[v] = len;
                heap.push({len, v});
            }
        }
    }    
}

void init() {
  for (int i = 1; i <= n; i++) {
    parent[i] = i;
    height[i] = 1;
  }
}

int find(int x) {
  if (parent[x] == x) return x;
  return parent[x] = find(parent[x]);
}

void merge(int x, int y) {
    x = find(x), y = find(y);
    if (x == y) return;
    if (height[x] < height[y]) {
        parent[x] = y;
    } else {
        parent[y] = x;
        if (height[x] == height[y]) height[x]++;
    }
}

void kruskal() {
    sort(edges.begin(), edges.end());
    init();
    for (auto &edge : edges) {
        int u = edge.u, v = edge.v, cost = edge.cost;
        
        if (find(u) != find(v)) {
            merge(u, v);
            min_cost += cost;
        }
    }
}

int main() {
    cin >> n;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            cin >> A[i][j];
        }
    }

    for (int i = 1; i <= n; i++) {
        for (int j = i + 1; j <= n; j++) {
            if (A[i][j] != -1)
            edges.push_back({i, j, A[i][j]});
        }
    }

    kruskal();
    // prim();
    cout << min_cost << endl;

    return 0;
}

C++: #include<iostream>
#include<cstring>
using namespace std;
#define maxn 110
#define INFTY (1<<30)
#define WHITE 0
#define GRAY 1
#define BLACK 2
int n,m[maxn][maxn];

int prim() //返回最小生成树各边权值总和
{
    int u,minv;
    int d[maxn],p[maxn],color[maxn];

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

    while(1)
    {
        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) //如果u和v之间存在边
            {
                if(m[u][v]<d[v]) //注意这里是d[v]不是d[u]
                {
                    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;
    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]=INFTY;
        }
    }
    cout<<prim()<<endl;
    return 0;
}

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

constexpr int INF = 1e9;

struct edge {
	int from, to, cost;
	bool operator>(const edge& other)const {
		return cost > other.cost;
	}
};

using edges = vector<edge>;
using graph = vector<edges>;

void add_edge(graph& g, int from, int to, int cost) {
	g[from].push_back(edge{ from,to,cost });
	g[to].push_back(edge{ to,from,cost });
}


edges prim(const graph& g) {
	const int N = g.size();
	vector<bool>used(N);
	used[0] = true;
	priority_queue<edge, vector<edge>, greater<edge>>p_que;
	for (auto e : g[0]) {
		p_que.push(e);
	}
	edges mst;
	while (!p_que.empty()) {
		auto e = p_que.top();
		p_que.pop();
		if (used[e.to]) {
			continue;
		}
		mst.push_back(e);
		used[e.to] = true;
		for (auto new_e : g[e.to]) {
			p_que.push(new_e);
		}

	}
	return mst;
}

int main() {
	int V, E;
	cin >> V >> E;
	graph g(V);
	for (int i = 0; i < E; i++) {
		int from, to, cost;
		cin >> from >> to >> cost;
		add_edge(g, from, to, cost);
	}
	const auto mst = prim(g);
	int ans = 0;
	for (auto& e : mst) {
		ans += e.cost;
	}
	cout << ans << endl;
	return 0;
}

C++: #include <iostream>
#include <utility>
#include <algorithm>
#define MAX 150
#define rep(i,p) for(int i=0;i<p;i++)
#define rep1(i,p) for(int i=1;i<=p;i++)
using namespace std;
int n,temp,a,b,size,ra,rb;
int A[MAX];
int B[MAX];
int W[MAX];
int V[MAX];
pair<int,int> edge[MAX];
int root (int child) {
  int a = child;
  while (V[a] != a) {
    a = V[a];
  }
  return a;
}
int main(){
  size = 0;
  cin >> n;
  rep(i,n) {
    rep(j,n) {
      cin >> W[size];
      if (i <= j && W[size] != -1) { 
        // cout <<  A[size] << " " << B[size] << " " << j << endl;        
        A[size] = i;
        B[size] = j;        
        edge[size].first = W[size];
        edge[size].second = size;
        size++;
      }
    }
  }
  // cout << endl;
  // rep(i,size) {
  //   cout << A[edge[i].second] << " " << B[edge[i].second]
  //        << " " << edge[i].first << endl;;
  // }
  // sort by lenght of edge
  sort(edge, edge+size+1);
  
  int sum = 0;
  rep(i,n) V[i] = i;
  rep(i,size) {
    if (edge[i].first == -1) continue;
    temp = edge[i].second;
    // a , b : vertex
    a = A[temp];
    b = B[temp];
    ra = root(a);
    rb = root(b);
    if (ra != rb) {
      // ?????????????????????
      sum += edge[i].first;
      V[ra] = rb;
      // if (ra != a && rb == b){     
      //   V[b] = ra;
      // } else if (ra == a && rb != b) {
      //   V[a] = rb;
      // } else if (ra == a && rb == b) {        
      //   V[a] = b;
      // } else if (ra != a && rb != b) {                
      //   V[ra] = rb;
      // }
    }
  }
  cout << sum << endl;
  return 0;
}