#C8745. Minimum Cost to Connect All Points

    ID: 52761 Type: Default 1000ms 256MiB

Minimum Cost to Connect All Points

Minimum Cost to Connect All Points

In this problem, you are given a complete weighted graph represented by an n×n cost matrix. Your task is to find the minimum cost required to connect all points (i.e., the minimum spanning tree cost). Formally, given an integer nn and a cost matrix CC, where C[i][j]C[i][j] represents the cost to connect point ii and point jj, find the minimum total cost such that all points become connected.

One efficient solution uses Prim's algorithm.

Formally, you are to compute $$\min\sum_{(u,v)\in T}C[u][v],$$ where TT is a spanning tree of the complete graph with vertices {0,1,,n1}\{0,1,\ldots,n-1\}.

inputFormat

The input is read from standard input (stdin) and has the following format:

First line: a single integer nn denoting the number of points.

Next nn lines: each line contains nn space-separated integers representing the cost matrix. The jj-th integer in the ii-th line denotes C[i][j]C[i][j].

outputFormat

Output to standard output (stdout) a single integer that is the minimum cost to connect all points.## sample

3
0 1 2
1 0 3
2 3 0
3