SZUOJ-Problem(A60):Roads

本文介绍了一种求解最小生成树的问题背景及两种经典算法:Prim算法和Kruskal算法。通过具体的输入输出样例展示了如何利用这两种算法来解决城市间道路连接问题,以达到最短总长度的目的。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Judge Info

  • Memory Limit: 32768KB
  • Case Time Limit: 10000MS
  • Time Limit: 10000MS
  • Judger: Number Only Judger

Description

Actually, Mr.K is a famous engineer. He got a big project resently.He is asked to build roads to connect several city. At first, there is no roads between any pair of these city. He is given the distances between any two cities . Mr.K wants to build roads as less length as posible to make all city connected.

Input

The input will contains multiple test cases. The first line of the input is a single integer T (1 \leq T \leq 100) which is the number of test cases. T test cases follow.

Each test case contains one positive integers n(1 \leq n \leq 1,000) --the number of the citis numbered 1 to n. Next,there are n rows, and which row contains n positive integer. The integer at ith row,jth column is the distance between city i and city j, and it will never bigger than 10,000;

Output

For each input test case, you are to output a single integer - the smallest length of roads that make all cities connected.

Sample Input

2


3
0 1 1
1 0 2
1 2 0 

3
0 1 1
1 0 2
1 2 0

Sample Output

2
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值