Go: package main

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

// I/O
type Scanner struct {
	sc *bufio.Scanner
}

func NewScanner() *Scanner {
	sc := bufio.NewScanner(os.Stdin)
	sc.Split(bufio.ScanWords)
	sc.Buffer(make([]byte, 1024), int(1e+9))
	return &Scanner{sc}
}

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

func (s *Scanner) nextInt() int {
	i, e := strconv.Atoi(s.nextStr())
	if e != nil {
		panic(e)
	}
	return i
}

func (s *Scanner) nextFloat() float64 {
	f, e := strconv.ParseFloat(s.nextStr(), 64)
	if e != nil {
		panic(e)
	}
	return f
}

func (s *Scanner) nextRuneSlice() []rune {
	return []rune(s.nextStr())
}

func (s *Scanner) nextIntSlice(n int) []int {
	res := make([]int, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextInt()
	}
	return res
}

func (s *Scanner) nextFloatSlice(n int) []float64 {
	res := make([]float64, n)
	for i := 0; i < n; i++ {
		res[i] = s.nextFloat()
	}
	return res
}

// Arithmetic
func max(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m < i {
			m = i
		}
	}
	return m
}

func min(nums ...int) int {
	m := nums[0]
	for _, i := range nums {
		if m > i {
			m = i
		}
	}
	return m
}

func abs(x int) int {
	if x > 0 {
		return x
	}
	return -x
}

func pow(x, y int) int {
	res := 1
	for i := 0; i < y; i++ {
		res *= x
	}
	return res
}

func ceil(a, b int) int {
	if a%b == 0 {
		return a / b
	} else {
		return a/b + 1
	}
}

// Sort
type RuneSlice []rune

func (a RuneSlice) Len() int           { return len(a) }
func (a RuneSlice) Less(i, j int) bool { return a[i] < a[j] }
func (a RuneSlice) Swap(i, j int)      { a[i], a[j] = a[j], a[i] }

const INF = 1 << 60

// Main
func main() {
	sc := NewScanner()
	wtr := bufio.NewWriter(os.Stdout)
	N := sc.nextInt()
	G := make([][]int, N)
	for i := 0; i < N; i++ {
		G[i] = make([]int, N)
		for j := 0; j < N; j++ {
			G[i][j] = sc.nextInt()
		}
	}
	d := make([]int, N)
	for i := 0; i < N; i++ {
		d[i] = INF
	}
	seen := make([]bool, N)
	d[0] = 0
	ans := 0
	for {
		cur := 0
		minCost := INF
		for i := 0; i < N; i++ {
			if !seen[i] && d[i] < minCost {
				minCost = d[i]
				cur = i
			}
		}
		if minCost == INF {
			break
		}
		seen[cur] = true
		ans += d[cur]
		for i := 0; i < N; i++ {
			cost := G[cur][i]
			if cost != -1 && cost < d[i] {
				d[i] = cost
			}
		}
	}

	fmt.Fprintln(wtr, ans)
	wtr.Flush()
}

C++: #include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> P;
const int INF = 1e9;
const int MAX_V = 1000;

int cost[MAX_V][MAX_V];
bool used[MAX_V];
int n;

int prim(){
    int res = 0;
    priority_queue<P, vector<P>, greater<P> > q;
    memset(used, false, sizeof(used));
    q.push(P(0, 0));
    while(!q.empty()){
        P p = q.top(); q.pop();
        int v = p.second;
        int d = p.first;
        if(used[v]) continue;
        used[v] = true;
        res += d;
        for(int i = 0; i < n; i++){
            q.push(P(cost[v][i], i));
        }
    }
    return res;
}

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

/* verified
https://onlinejudge.u-aizu.ac.jp/#/courses/lesson/1/ALDS1/12/ALDS1_12_A
*/

C++: #include <bits/stdc++.h>
using namespace std;

struct UF{
   vector<int> par;
   vector<int> sz;
   UF(){}
   UF(int n){
       par.resize(n);
       sz.resize(n, 1);
       for(int i = 0; i < n; i++) par[i] = i;
   }
   int find(int x){
       if(x == par[x]) return x;
       return par[x] = find(par[x]);
   }
   void unite(int x, int y){
       x = find(x); y = find(y);
       if(x == y) return;
       if(sz[x] < sz[y]) swap(x, y);
       sz[x] += sz[y];
       par[y] = x;
   }
   bool same(int x, int y){
       return find(x) == find(y);
   }
};

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

int n, m;
vector<edge> es; 

int kruskal(){
    int res = 0;    
    UF uf(n);
    sort(es.begin(), es.end());
    for(int i = 0; i < m; i++){
        edge e = es[i];
        if(!uf.same(e.u, e.v)){
            uf.unite(e.u, e.v);
            res += e.cost;
        }
    }
    return res;
}

int main(){
    cin >> n >> m;
    for(int i = 0; i < m; i++){
        int u, v, w;
        cin >> u >> v >> w;
        //u--; v--;
        es.push_back({u, v, w});
    }
    cout << kruskal() << endl;
}

/* verified
https://onlinejudge.u-aizu.ac.jp/#/courses/library/5/GRL/2/GRL_2_A
*/

C++: #include <bits/stdc++.h>
using namespace std;

typedef long long ll;

struct Edge {
    ll s,t;
    ll w;
    bool operator < (const Edge& tgt) const {
	return w > tgt.w;
    }
    void dump(){
	cout << s << " --> " << t << " : " << w << endl; 
    }
};

ll Prim(vector<vector<Edge>>& G) {
    const ll V = G.size();
    priority_queue<Edge> edges;
    
    vector<int> used(V,0);
    //??????0??????????????????
    used[0] = 1;
    for(auto& e : G[0]) edges.push(e);

    ll ans = 0;
    while( !edges.empty() ) {
	Edge e = edges.top(); edges.pop();
	if( used[e.t] == 1 ) continue;
	used[e.t] = 1;
	ans += e.w;
	assert(used[e.s == 1]);
	for(auto& nxt : G[e.t]) {
	    if( used[nxt.t] == 1 ) continue;
	    edges.push(nxt);
	}
    }
    return ans;
}


int main () {

    ll V;
    cin >> V;
    vector<vector<Edge>> G(V);

    for(int i=0; i<V; i++) {
	for(int j=0; j<V; j++) {
	    ll w; cin >> w;
	    if( w != -1 and w != 0) {
		G[i].push_back( Edge{i,j,w} );
		G[j].push_back( Edge{j,i,w} );
//		cout << i << " <--> " << j << " : " << w << endl; 
	    }
	}
    }

    ll ans = Prim(G);
    cout << ans << endl;
        
    return 0;
}

C++: #include<bits/stdc++.h>
//#include"UnionFind.hpp"
using namespace std;



class UnionFind{
    int N;
    vector<int> par;
    vector<int> rank;
    public:
    UnionFind(int N):N(N),par(N),rank(N,0){iota(par.begin(),par.end(),0);}
    int find(int x);
    void unite(int x,int y);
    bool same(int x,int y){return find(x)==find(y);}
};

int UnionFind::find(int x){
    if(par[x]==x) return x;
    return par[x]=find(par[x]);
}
void UnionFind::unite(int x,int y){
    x=find(x),y=find(y);
    if(x==y) return;
    if(rank[x]>=rank[y]){
        if(rank[x]==rank[y]) rank[x]++;
        par[y]=x;
    }else{
        par[x]=y;
    }
}

using Weight=long long;
struct Edge{
    int from,to;
    Weight weight;
};
bool operator<(const Edge& lhs,const Edge& rhs){
    return lhs.weight<rhs.weight;
}

using Edges=vector<Edge>;
using Graph=vector<Edges>;


Edges MinimumSpanningTree(int n,Edges edges){
    Edges res;
    UnionFind u(n);
    for(int i=0;i<edges.size();i++){
        if(!u.same(edges[i].from,edges[i].to)){
            res.push_back(edges[i]);
            u.unite(edges[i].from,edges[i].to);
        }
    }
    return res;
}

Edges MinimumSpanningTree(const Graph& g){
    Edges edges;
    for(int i=0;i<g.size();i++){
        copy(g[i].begin(),g[i].end(),back_inserter(edges));
    }
    auto res=MinimumSpanningTree(g.size(),edges);
    return res;
}

int main(){
    int n;
    cin>>n;
    Graph g(n);
    for(int i=0;i<n;i++){
        for(int j=0;j<n;j++){
            Weight c;
            cin>>c;
            if(i==j) continue;
            g[i].push_back({i,j,c});
        }
    }
    auto mst=MinimumSpanningTree(g);
    cout<<accumulate(mst.begin(),mst.end(),Weight(0),[](const Weight& lhs,const Edge& rhs){return lhs+rhs.weight;})<<endl;
    return 0;
}