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

Problem Description

There are several cities in the country, and some of them are connected by bidirectional roads. Unfortunately, some of the roads are damaged and cannot be used right now. Your goal is to rebuild enough of the damaged roads that there is a functional path between every pair of cities.

You are given the description of roads. Damaged roads are formatted as "city1 city2 cost" and non-damaged roads are formatted as "city1 city2 0". In this notation city1 and city2 are the case-sensitive names of the two cities directly connected by that road. If the road is damaged, cost represents the price of rebuilding that road. Every city in the country will appear at least once in roads. And there can be multiple roads between same pair of cities.

Your task is to find the minimum cost of the roads that must be rebuilt to achieve your goal. If it is impossible to do so, print "Impossible".

Input

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

Each case begins with a blank line and an integer m (1 ≤ m ≤ 50) denoting the number of roads. Then there will be m lines, each containing the description of a road. No names will contain more than 50 characters. The road costs will lie in the range [0, 1000].

Output

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

Sample Input

2

12
Dhaka Sylhet 0
Ctg Dhaka 0
Sylhet Chandpur 9
Ctg Barisal 9
Ctg Rajshahi 9
Dhaka Sylhet 9
Ctg Rajshahi 3
Sylhet Chandpur 5
Khulna Rangpur 7
Chandpur Rangpur 7
Dhaka Rajshahi 6
Dhaka Rajshahi 7 

2
Rajshahi Khulna 4
Kushtia Bhola 1

Output for Sample Input

Case 1: 31
Case 2: Impossible

Problem solving report:

Description: 给你 m 条路,每条路是从城市a 到城市b, 然后一个距离s,求把所有城市连起来的最短路径
Problem solving: 这题就是个最小生成树,只不过没有给城市数量,需要自己根据城市出现的次数自己判断,把城市名字转化成数字,利用STL中map就简单多了。

Accepted Code:

/* 
 * @Author: lzyws739307453 
 * @Language: C++ 
 */
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 105;
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 >> 1];
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() {
    char sa[55], sb[55];
    int t, w, n, m;
    int cnt, ans, kase = 0;
    scanf("%d", &t);
    while (t--) {
        n = 0;
        scanf("%d", &m);
        map <string, int> mp;
        for (int i = 0; i < m; i++) {
            scanf("%s%s%d", sa, sb, &w);
            if (!mp.count(sa))
                mp[sa] = n++;
            if (!mp.count(sb))
                mp[sb] = n++;
            e[i] = edge(mp[sa], mp[sb], w);
        }
        for (int i = 0; i < n; i++)
            f[i] = i;
        sort(e, e + m);
        cnt = 1, ans = 0;
        for (int i = 0; i < m && cnt < n; i++) {
            if (merge(e[i].u, e[i].v)) {
                cnt++;
                ans += e[i].w;
            }
        }
        if (cnt < n)
            printf("Case %d: Impossible\n", ++kase);
        else printf("Case %d: %d\n", ++kase, ans);
    }
    return 0;
}