Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

Minimum Spanning Tree

For a given weighted graph $G = (V, E)$, find the minimum spanning tree (MST) of $G$ and print total weight of edges belong to the MST.

Input

In the first line, an integer $n$ denoting the number of vertices in $G$ is given. In the following $n$ lines, a $n \times n$ adjacency matrix $A$ which represents $G$ is given.
$a_{ij}$ denotes the weight of edge connecting vertex $i$ and vertex $j$. If there is no edge between $i$ and $j$, $a_{ij}$ is given by -1.

Output

Print the total weight of the minimum spanning tree of $G$.

Constraints

$1 \leq n \leq 100$

$0 \leq a_{ij} \leq 2,000$ (if $a_{ij} \neq -1$)

$a_{ij} = a_{ji}$

$G$ is a connected graph

Sample Input 1

5
-1 2 3 1 -1
2 -1 -1 4 -1
3 -1 -1 1 1
1 4 1 -1 3
-1 -1 1 3 -1

Sample Output 1

5

Reference

Introduction to Algorithms, Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. The MIT Press.

Solution in JAVA (judge verdict: Wrong Answer)


import java.util.*;


public class Main{ 

	public static void main(String args[]){
		Scanner scan = new Scanner(System.in);
		int nodeNum = scan.nextInt();
		AdjacencyMatrices am = new AdjacencyMatrices(nodeNum);
		am.readMatrix(scan);
		Iterator<Edge> iter = am.getIter();
		UnionFind unionFind  = new UnionFind(nodeNum);
		int sumDist = 0;
		while(iter.hasNext()){
			Edge e = iter.next();

			if(!unionFind.same(e.x, e.y)){
				sumDist += e.dist;
				unionFind.union(e.x, e.y);
				//System.out.println(e);
			}
		}
		System.out.println(""+sumDist);
	}

}

class Edge implements Comparable<Edge>{
	int x;
	int y;
	int dist;
	public Edge(int x, int y, int d){
		this.x = x;
		this.y = y;
		this.dist = d;
	}
	@Override
 	public int compareTo(Edge other) {
		return this.dist - other.dist;
	}
	@Override
	public String toString(){
		return "x: " + x + " y : " + y + " dist : " + dist;
	}
}
class AdjacencyMatrices{
	int pointNum;
	ArrayList<Edge> edgeList = new ArrayList<Edge>();
	public AdjacencyMatrices(int n){
		pointNum = n;
	}
	public void readMatrix(Scanner scan){
		for(int i=0; i<pointNum; i++)
			for(int j=0; j< pointNum; j++){
				int dist = scan.nextInt();
				if(j > i && dist>0)
					edgeList.add(new Edge(i, j, dist));
		}
	}
	public void printEdge(){
		
		for(int i=0; i<edgeList.size(); i++)
			System.out.println(edgeList.get(i));
	}
	public Iterator<Edge> getIter(){
		Collections.sort(edgeList);
		return edgeList.iterator();
	}

}
class UnionFind{
    int[] index;
    public UnionFind(int n){
        index = new int[n];
        for(int i=0; i<n; i++)
            index[i] = i;
    }
    int find(int x){
        if(index[x]==x)
            return x;
        else
            return find(index[x]);
    }
    boolean same(int x, int y){
        return find(x) == find(y);
    }
    void union(int x, int y){
        int rootx = find(x);
        int rooty = find(y);
        if(rootx == rooty)
            return;
        else
            index[rooty] = rootx;
    }
    void printIndex(){
        for(int i=0; i<index.length; i++)
            System.out.print(" " + index[i]);
        System.out.println("");
    }

}