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: Time Limit Exceeded)

/**
 * File:  minimum_spanning_tree.c
 * Author: ymiyamoto
 *
 * Created on Sun Jun 25 04:55:09 2017
 */

typedef enum {
  WHITE,
  GRAY,
  BLACK,
} color_t;

#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>


#define MINIMUM_SPANNING_TREE_GRAPH_SIZE 100
extern bool minimum_spannig_tree(int32_t graph[][MINIMUM_SPANNING_TREE_GRAPH_SIZE], size_t size, size_t *weight);

static color_t colors[MINIMUM_SPANNING_TREE_GRAPH_SIZE];
static int32_t minimum_costs[MINIMUM_SPANNING_TREE_GRAPH_SIZE];

bool minimum_spannig_tree(int32_t graph[][MINIMUM_SPANNING_TREE_GRAPH_SIZE], size_t size, size_t *weight)
{
  if (graph == NULL) {
    *weight = 0;
    return false;
  }

  uint32_t i;
  for (i = 0; i < size; i++) {
    colors[i] = WHITE;
    minimum_costs[i] = INT32_MAX;
  }

  minimum_costs[0] = 0;
  *weight = 0;

  while (true) {
    int32_t mincost = INT32_MAX;
    uint32_t minimum_vertex;
    for (i = 0; i < size; i++) {
      if ((colors[i] != BLACK) && (minimum_costs[i] < mincost)) {
        mincost = minimum_costs[i];
        minimum_vertex = i;
      }
    }

    if (mincost == INT32_MAX) break;
    *weight += mincost;
    colors[minimum_vertex] = BLACK;

    for (i = 0; i < size; i++) {
      if ((colors[i] != BLACK) && (graph[minimum_vertex][i] < INT32_MAX)) {
        if (graph[minimum_vertex][i] < minimum_costs[i]) {
          minimum_costs[i] = graph[minimum_vertex][i];
          colors[i] = GRAY;
        }
      }
    }
  }

  return true;
}

#if !defined(TEST)
int32_t graph[MINIMUM_SPANNING_TREE_GRAPH_SIZE][MINIMUM_SPANNING_TREE_GRAPH_SIZE];

int32_t main(void)
{
  int32_t num;
  scanf("%d", &num);
  uint32_t i;
  for (i = 0; i < num; i++) {
    uint32_t j;
    for (i = 0; j < num; j++) {
      int32_t d;
      scanf("%d", &d);
      graph[i][j] = d == -1 ? INT32_MAX : d;
    }
  }

  size_t weight;
  minimum_spannig_tree(graph, 6, &weight);
  printf("%lu\n", weight);

  return 0;
}
#endif