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

Problem Description

A Civil Engineer is given a task to connect n houses with the main electric power station directly or indirectly. The Govt has given him permission to connect exactly n wires to connect all of them. Each of the wires connects either two houses, or a house and the power station. The costs for connecting each of the wires are given.

Since the Civil Engineer is clever enough and tries to make some profit, he made a plan. His plan is to find the best possible connection scheme and the worst possible connection scheme. Then he will report the average of the costs.

Now you are given the task to check whether the Civil Engineer is evil or not. That's why you want to calculate the average before he reports to the Govt.

Input

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

Each case contains a blank line and an integer n (1 ≤ n ≤ 100) denoting the number of houses. You can assume that the houses are numbered from 1 to nand the power station is numbered 0. Each of the next lines will contain three integers in the form u v w (0 ≤ u, v ≤ n, 0 < w ≤ 10000, u ≠ v) meaning that you can connect u and v with a wire and the cost will be w. A line containing three zeroes denotes the end of the case. You may safely assume that the data is given such that it will always be possible to connect all of them. You may also assume that there will not be more than 12000 lines for a case.

Output

For each case, print the case number and the average as described. If the average is not an integer then print it in p/q form. Where p is the numerator of the result and q is the denominator of the result; p and q are relatively-prime. Otherwise print the integer average.

Sample Input

3

1
0 1 10
0 1 20
0 0 0

3
0 1 99
0 2 10
1 2 30
2 3 30
0 0 0 

2
0 1 10
0 2 5
0 0 0

Output for Sample Input

Case 1: 15
Case 2: 229/2
Case 3: 15

Problem solving report:

Description: 用n条线路连接n个房屋和主电站,求最好情况和最坏情况的平均值。
Problem solving: 按价格求最小生成树,然后求最大生成树,最后输出平均值。

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
const int MAXM = 12005;
struct edge {
    int u, v, w;
    bool operator < (const edge &s) const {
        return s.w > w;
    }
}e[MAXM];
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, n;
    int cnt, ans, kase = 0;
    scanf("%d", &t);
    while (t--) {
        k = 0;
        scanf("%d", &n);
        for (int i = 0; i <= n; i++)
            f[i] = i;
        while (scanf("%d%d%d", &e[k].u, &e[k].v, &e[k].w), e[k].u + e[k].v + e[k].w)
            k++;
        sort(e, e + k);
        cnt = 0, ans = 0;
        for (int i = 0; i < k && cnt < n; i++) {
            if (merge(e[i].u, e[i].v)) {
                cnt++;
                ans += e[i].w;
            }
        }
        cnt = 0;
        for (int i = 0; i <= n; i++)
            f[i] = i;
        for (int i = k - 1; ~i && cnt < n; i--) {
            if (merge(e[i].u, e[i].v)) {
                cnt++;
                ans += e[i].w;
            }
        }
        if (ans & 1)
            printf("Case %d: %d/2\n", ++kase, ans);
        else printf("Case %d: %d\n", ++kase, ans >> 1);
    }
    return 0;
}