#P1546. [USACO3.1] 最短网络 Agri-Net

[USACO3.1] 最短网络 Agri-Net

Description

FJ has arranged a high-speed network line for his farm, and he wants to share it with other farms. To minimize the cost, he wants to lay the shortest optical fiber to connect all the farms.

You are given a list of connection costs between farms. You must find a plan that connects all farms with the minimum total fiber length. The distance between any two farms will not exceed 10510^5.

Input Format

The first line contains the number of farms NN (3N1003 \leq N \leq 100).

Next is an N×NN \times N matrix representing the distance between each pair of farms. In theory, it consists of NN lines, each with NN integers separated by spaces. In practice, due to the 8080-character-per-line limit, some rows may continue directly onto subsequent lines. The diagonal entries are 00, since there is no line from the ii-th farm to itself.

Output Format

Output a single integer: the minimum total length of fiber needed to connect all farms.

4
0 4 9 21
4 0 8 17
9 8 0 16
21 17 16 0
28

Hint

Problem translation from NOCOW. USACO Training Section 3.1.

Translated by ChatGPT 5