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;
using System.Diagnostics;

namespace Aoj.ALDS.Chapter12A
{
    enum Color
    {
        WHITE, GRAY, BLACK
    }

    class Program
    {
        /// <summary>
        /// ??????????????¢?????´???????????§????°???¨?????¨????±??????????
        /// </summary>
        /// <param name="m">???????????£??\?????????</param>
        /// <param name="n">??????????????°???</param>
        /// <returns></returns>
        public static int Prim(List<List<int>> m, int n)
        {
            // ??£??\????????????????????????????°?????????????????´??????????
            var d = new List<int>();
            // ????±?????????????????????????????´???????(?§?????????????-1)???
            var p = new List<int>();
            // ??¢?´¢????????¶??????BLACK??§??¢?´¢?????????
            var color = new List<Color>();

            // ???????´??????????????????????
            for (int i = 0; i < n; i++)
            {
                d.Add(Int32.MaxValue);
                p.Add(-1);
                color.Add(Color.WHITE);
            }

            // ????????????????????????????????¢?´¢????§????????????????d[0]???0????????????
            d[0] = 0;

            int u;
            int minV;
            while (true)
            {
                u = -1;
                minV = Int32.MaxValue;

                // ??°??¬????????????????????????d[i]??????????°?????????????????±??????????
                for (int i = 0; i < n; i++)
                {
                    if (minV > d[i] && color[i] != Color.BLACK) {
                        u = i;
                        minV = d[i];
                    }
                }

                // ??¨?????????????????¢?´¢???????????£???????????§break???
                if (u == -1) break;

                // ??£??\???????????????????°??????¢??¨??????????????´??°???????????????
                for (int i = 0; i < n; i++)
                {
                    if (color[i] != Color.BLACK && m[u][i] != -1) {
                        if (d[i] > m[u][i]){
                            d[i] = m[u][i];
                            p[i] = u;
                            color[i] = Color.GRAY;
                        }
                    }
                }

                // ????????????????????¢?´¢????????????????????????
                color[u] = Color.BLACK;
            }

            // ????¨?????±??????????
            int sum = 0;
            for (int i = 0; i < n; i++)
            {
                if (p[i] != -1) sum += m[i][p[i]];
            }
            return sum;
        }

        public static void Solve()
        {
            int n = int.Parse(Console.ReadLine());

            var m  = new List<List<int>>();

            for (int i = 0; i < n; i++)
            {
                m.Add(new List<int>());
                string line = Console.ReadLine();
                line = line.Substring(1, line.Length - 1);
                m[i] = Array.ConvertAll(line.Split(' '), int.Parse).ToList();
            }

            int answer = Prim(m, n);
            Console.WriteLine(answer);
        }

        public static void Main()
        {
            // Console.SetIn(new System.IO.StreamReader("Aoj/AlgorithmsAndDataStructures/Input/12_Graph_A_01.txt"));
            // var sw = new Stopwatch();
            // sw.Start();

            Solve();

            // Console.WriteLine("{0}ms", sw.ElapsedMilliseconds);
        }
    }
}