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 C# (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _20190129_AOJ
{
    class ALDS1_12A
    {
        static void Main(string[] args)
        {
            for (;;)
            {
                string line = Console.ReadLine();
                if (string.IsNullOrEmpty(line))
                {
                    break;
                }

                int n = int.Parse(line);
                int[,] matrix = new int[n, n];
                HashSet<HashSet<int>> trees = new HashSet<HashSet<int>>();
                List <Vertex> vertices = new List<Vertex>();
                for(int i=0;i<n;i++)
                {
                    HashSet<int> th = new HashSet<int>();
                    th.Add(i);
                    trees.Add(th);

                    int[] iarr = ReadIntArr();
                    for(int j=0;j<iarr.Length;j++)
                    {
                        matrix[i, j] = iarr[j];
                        if (j > i && iarr[j] >= 0)
                        {
                            vertices.Add(new Vertex(i, j, iarr[j]));
                        }
                    }
                }

                int sum = 0;
                foreach(var v in vertices.OrderBy(x => x.W))
                {
                    HashSet<int> t1 = trees.First(x => x.Contains(v.P1));
                    HashSet<int> t2 = trees.First(x => x.Contains(v.P2));
                    if(t1 == t2)
                    {
                        // do nothing
                    }
                    else
                    {
                        t1.UnionWith(t2);
                        trees.Remove(t2);
                        sum += v.W;
                    }
                }

                Console.WriteLine(sum);
            }
        }

        static int ReadInt() { return int.Parse(Console.ReadLine()); }
        static double ReadDouble() { return double.Parse(Console.ReadLine()); }
        static int[] ReadIntArr(char dlm = ' ') { return Console.ReadLine().Trim().Split(dlm).Select(x => int.Parse(x)).ToArray(); }
        static double[] ReadDoubleArr(char dlm = ' ') { return Console.ReadLine().Split(dlm).Select(x => double.Parse(x)).ToArray(); }
        static string[] ReadStringArr(char dlm = ' ') { return Console.ReadLine().Split(dlm).ToArray(); }
    }

    class Vertex
    {
        public int P1 { get; set; }
        public int P2 { get; set; }
        public int W { get; set; }
        public Vertex(int _p1, int _p2, int _w)
        {
            P1 = _p1;
            P2 = _p2;
            W = _w;
        }
    }
}