题目链接:http://lightoj.com/volume_showproblem.php?problem=1040
Time Limit: 2 second(s) Memory Limit: 32 MB

Problem Description

A local charity is trying to gather donations of Ethernet cable. You realize that you probably have a lot of extra cable in your house, and make the decision that you will donate as much cable as you can spare.

You will be given the lengths (in meters) of cables between each pair of rooms in your house. You wish to keep only enough cable so that every pair of rooms in your house is connected by some chain of cables, of any length. The lengths are given in n lines, each having n integers, where n is the number of rooms in your house. The jth integer of ith line gives the length of the cable between rooms i and j in your house.

If both the jth integer of ith line and the ith integer of jth line are greater than 0, this means that you have two cables connecting rooms i and j, and you can certainly donate at least one of them. If the ith integer of ith line is greater than 0, this indicates unused cable in room i, which you can donate without affecting your home network in any way. 0 means no cable.

You are not to rearrange any cables in your house; you are only to remove unnecessary ones. Return the maximum total length of cables (in meters) that you can donate. If any pair of rooms is not initially connected by some path, return -1.

Input

Input starts with an integer T (≤ 100), denoting the number of test cases.

Each case begins with a blank line and an integer n (1 ≤ n ≤ 50) denoting the number of rooms in your house. Then there will be n lines, each having nspace separated integers, denoting the lengths as described. Each length will be between 0 and 100.

Output

For each case of input you have to print the case number and desired result.

Sample Input

3

2
27 26
1 52 

4
0 10 10 0
0 0 1 1
0 0 0 2
0 0 0 0 

4
0 1 0 0
1 0 0 0
0 0 0 1
0 0 1 0

Output for Sample Input

Case 1: 105
Case 2: 12
Case 3: -1

Problem solving report:

Description: 有n个房间用电线连接,你想捐出一些电线,给出一个n*n的矩阵,第 i 行第 j 列代表第 i 个房间和第 j 个房间之间有a[i][j]米长的电线,第 i 行第 i 列如果不为0说明这个房间中有闲置的电线;如果第 i 行第 j 列和第 j 行第 i 列都不为0,说明两个房间不止一条电线连接,则可以选择其中一条,求最多可以捐出来的电线是多少。
Problem solving: 全部电线长sum,首先求出最小生成树为num,那么num是必须要的,那么答案就为sum-num。

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 55;
struct edge {
    int u, v, w;
    edge() {}
    edge(int u, int v, int w) : u(u), v(v), w(w) {}
    bool operator < (const edge &s) const {
        return s.w > w;
    }
}e[MAXN * MAXN];
int f[MAXN];
int getf(int v) {
    if (f[v] != v)
        f[v] = getf(f[v]);
    return f[v];
}
bool merge(int u, int v) {
    int t1 = getf(u);
    int t2 = getf(v);
    if (t1 != t2) {
        f[t2] = t1;
        return true;
    }
    return false;
}
int main() {
    int t, k, w, n;
    int cnt, ans, sum, kase = 0;
    scanf("%d", &t);
    while (t--) {
        sum = 0, k = 0;
        scanf("%d", &n);
        for (int i = 0; i < n; i++) {
            f[i] = i;
            for (int j = 0; j < n; j++) {
                scanf("%d", &w);
                sum += w;
                if (i != j && w)
                    e[k++] = edge(i, j, w);
            }
        }
        sort(e, e + k);
        cnt = 1, ans = 0;
        for (int i = 0; i < k && cnt < n; i++) {
            if (merge(e[i].u, e[i].v)) {
                cnt++;
                ans += e[i].w;
            }
        }
        sum -= ans;
        if (cnt < n)
            printf("Case %d: -1\n", ++kase);
        else printf("Case %d: %d\n", ++kase, sum);
    }
    return 0;
}