Go: package main

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

const (
	MAX = 100
	INF = 1 << 21
	WHITE = 0
	GRAY = 1
	BLACK = 2
)

var (
	n int
	M [MAX][MAX]int
)

func prim() int {
	var u, minv int
	var d [MAX]int
	var p [MAX]int
	var color [MAX]int

	for i := 0; i < n; i++ {
		d[i] = INF
		p[i] = -1
		color[i] = WHITE
	}
	d[0] = 0
	for {
		minv = INF
		u = -1
		for 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 v := 0; v < n; v++ {
			if color[v] != BLACK && M[u][v] != INF {
				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() {
	scanner := bufio.NewScanner(os.Stdin)
	scanner.Split(bufio.ScanWords)
	scanner.Scan()
	n, _ = strconv.Atoi(scanner.Text())
	for i := 0; i < n; i++ {
		for j := 0; j < n; j++ {
			scanner.Scan()
			e, _ := strconv.Atoi(scanner.Text())
			if e == -1 {
				M[i][j] = INF
			} else {
				M[i][j] = e
			}
		}
	}
	fmt.Println(prim())
}

C++: #include<iostream>
#define MAX 100
#define INF (2<<29)
using namespace std;
int main(){
  int n;
  cin>>n;
  int graph[MAX][MAX];
  
  for(int i=1;i<n+1;i++){
    for(int j=1;j<n+1;j++){
      cin>>graph[i][j];
      if(graph[i][j]==-1)graph[i][j]=INF;
    }
  }
  
  int visited[MAX];
  int min[MAX];
  for(int i=1;i<n+1;i++){
    visited[i]=0;
    min[i]=INF;
  }
  
  min[1]=0;
  int ans=0;
  while(true){
    int a=-1;
    for(int i=1;i<n+1;i++){
      if(visited[i]==0&&(a==-1||min[i]<min[a]))a=i;
    }

    if(a==-1)break;
    visited[a]=1;
    ans +=min[a];

    for(int i=1;i<n+1;i++){
      min[i]=min[i]<graph[a][i] ? min[i]:graph[a][i];
    }
  }
  cout<<ans<<endl;
  return 0;
}

C++: #include <iostream>

using namespace std;

static const int MAX = 100;
static const int INFTY = (1<<21);
static const int WHITE = 0;
static const int GRAY = 1;
static const int BLACK = 2;

int n, M[MAX][MAX];

int prim(){
  int u, minv;
  int d[MAX], p[MAX], color[MAX];
	
  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){
	if(d[v] > M[u][v]){
	  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){
      int e; cin >> e;
      M[i][j] = (e == -1) ? INFTY : e;
    }
  }
	
  cout << prim() << endl;
	
  return 0;
}

C++: #include<bits/stdc++.h>
#define REP(i,n) for(int i=0;i<n;i++)
#define LOOP(i,x,n) for(int i=x;i<n;i++)
#define ALL(v) (v).begin(),(v).end()
#define PB push_back
#define MP make_pair
#define FIR first
#define SEC second
#define int long long
using namespace std;
const int MOD=1000000007;
const int INF=1000000009;

typedef int Weight;
struct Edge{
  int src;int dst;
  Weight wei;
  Edge(int s=0,int d=0,Weight w=0):
    src(s),dst(d),wei(w){}
};
using Graph = vector<vector<Edge>>;
int prim(const Graph &g,const int r=0){
  struct comp{
    bool operator()(const Edge &e,const Edge &f)const {return e.wei>f.wei;}
  };
  Weight sum=0;
  vector<int> used(g.size());
  priority_queue<Edge,vector<Edge>,comp> q;
  q.emplace(-1,r,0);
  while(q.size()){
    Edge e=q.top();
    q.pop();
    if(used[e.dst])continue;
    used[e.dst]=1;
    sum+=e.wei;
    for(auto &f : g[e.dst]){
      if(!used[f.dst])q.emplace(f);
    }
  }
  return sum;
}

signed main(){
  int n;
  cin>>n;
  Graph g(n);
  REP(i,n)REP(j,n){
    int a;
    cin>>a;
    if(a!=-1){
      g[i].emplace_back(i,j,a);
    }
  }
  cout<<prim(g,0)<<endl;
  return 0;
}

C++: #include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<map>
#include<string>
#include<algorithm>
#include<functional>
#define ll long long
#define inf  999999999
#define pa pair<int,int>

using namespace std;

int n,a[200][200]={0};
void prim(){
	int d[200]={0};
	priority_queue<pair<int,pa> >  PQ; 
	pair<int,pa> q;
	pa r;
	int wei=0;
	int w=6;
	int u=w,z=0;
	d[w]=1;
	while(z!=n-1){
		for(int j=n-1;j>-1;j--){
			if(a[u][j]<=0 && d[j]==0 && u!=j) PQ.push(make_pair(a[u][j],make_pair(u,j)));
		}
	while(1){
		if(PQ.size()==0) {
			cout<<"dv"<<endl;
		break;
		}
		q=PQ.top();
		PQ.pop();
		r=q.second;
	//	cout<<"rsec"<<r.second<<endl;
		if(d[r.second]==0) break;
		
		}
	u=r.second;
//	cout<<"a" <<u<<endl;
	d[u]=1;
	wei=wei+q.first;
	z++;
//	cout<<z<<" "<<-q.first<<" "<<u<<" "<<r.first<<endl;
	
	}
	cout<<-wei<<endl;
}



int main(){
	// your code goes here
	int m;
	cin>>n;
	
	for(int i=0;i<n;i++){
		for(int j=0;j<n;j++){
		cin>>m;
		a[i][j]=-m;
		}
	}
	prim();

	return 0;
}

C++: #include<iostream>
using namespace std;
const int MAX = 100;
const int INF = 20000;
int n, G[MAX][MAX], dis[MAX];
bool book[MAX] = { false };
int main()
{
	cin >> n;
	for (int i = 0; i < n; i++)
		for (int j = 0; j < n; j++)
			cin >> G[i][j];
	for (int i = 0; i < n; i++)
		dis[i] = G[0][i];
	book[0] = true;
	int sum = 0;
	for (int k = 1; k <= n - 1; k++)
	{
		int min = INF;
		int u;
		for (int j = 0; j < n; j++)
			if (!book[j] && dis[j] != -1 && dis[j] < min)
			{
				min = dis[j];
				u = j;
			}
		book[u] = true;
		sum += dis[u];
		for (int v = 0; v < n; v++)
			if (!book[v] && G[u][v] != -1)
				if (dis[v] == -1)
					dis[v] = G[u][v];
				else
					if (dis[v] > G[u][v])
						dis[v] = G[u][v];
	}
	cout << sum << endl;
}

C++: #include <iostream>
#include <vector>
#include <queue>
#define to first
#define cost second
const int inf=1e9+7;
using namespace std;
int main(){

  int n,tmp,r=0,flag[100]={},v=0;
  cin>>n;
  vector <pair<int,int> > gl[n];
  priority_queue <pair<int,int> ,vector<pair<int,int> >, greater<pair<int,int> > >q;
  //for(int i=1;i<n;i++) mincost[i]=inf;                                                                                                                                                       

  for(int i=0;i<n;i++) for(int j=0;j<n&&cin>>tmp;j++) if(tmp!=-1) gl[i].push_back(make_pair(j,tmp));
  int now=0;
  for(int j=0;j<n-1;j++){
    for(int i=0;i<gl[now].size();i++) if(!flag[gl[now][i].to]) q.push(make_pair(gl[now][i].cost,gl[now][i].to));
    flag[now]=1;
    // cout<<now<<" "<<q.top().first<<endl;                                                                                                                                                    
    for(;flag[q.top().second];) q.pop();
    if(q.empty()) break;
    r+=q.top().first;
    now=q.top().second;
    q.pop();
  }
  cout<<r<<endl;
  return 0;

}

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

#define WHITE 0
#define GRAY 1
#define BLACK 2
#define INF 2000000000

int x[10000][10000];
int color[10000];
int MIN[10000];
int sum;
int sum_min = INF;

int main() {
	int n, a, b, c;
	cin >> n;
	for (int i = 0; i < n; i++) {
		for (int j = 0; j < n; j++) {
			cin >> x[i][j];
		}
	}
	for (int i = 0; i < n; i++) {
		memset(MIN, 127, sizeof(MIN));
		b = INF;
		a = 1;
		color[i] = GRAY;
		MIN[i] = 0;
		while (a >= 1) {
			for (int j = 0; j < n; j++) {
				if (color[j] = GRAY) {
					if (b > MIN[j]) {
						b = MIN[j];
						c = j;
					}
				}
			}
			color[c] = BLACK;
			a -= 1;
			for (int j = 0; j < n; j++) {
				if (color[j] != BLACK && x[c][j] != -1) {
					if (color[i] == WHITE) {
						a += 1;
					}
					color[j] = GRAY;
					MIN[j] = min(x[c][j], MIN[j]);
				}
			}
		}
		sum = 0;
		for (int j = 0; j < n; j++) {
			sum += MIN[j];
		}
		sum_min = min(sum_min, sum);
	}
	cout << sum_min << endl;
	return 0;
}