Lingt oj 1041 - Road Construction(最小生成树)

题目链接:http://lightoj.com/volume_showproblem.php?problem=1041


1041 - Road Construction
Time Limit: 2 second(s)Memory Limit: 32 MB

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 city1and 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

Output for 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

Case 1: 31

Case 2: Impossible

 


SPECIAL THANKS: JANE ALAM JAN (DESCRIPTION, SOLUTION, DATASET)


题目大意:给你 m 条路,每条路是从城市a 到城市b, 然后一个距离s,求把所有城市连起来的最短路径


解析: 这题是最小生成树的变形没有给城市数量,需要自己根据城市出现的次数自己判断,把城市名字转化成数字非常巧妙,

             会C++ STL中map就简单多了


不多说了,上代码:

#include<iostream>
#include<algorithm>
#include<map>
#include<string>
#include<cstdio>
#include<cstring>
#include<cctype>
#include<cmath>
#define N 1009
using namespace std;
const int inf = 0x3f3f3f3f;

map<string, int> mp;
int maps[N][N], book[N];
int pri(int n)
{
    int i, j, vis[N];
    for(i = 1; i <= n; i++)
    {
        vis[i] = maps[1][i];
    }
    vis[1] = 0;
    int ans = 0;
    for(i = 1; i <= n; i++)
    {
        int m, f = inf;
        for(j = 1; j <= n; j++)
        {
            if(!book[j] && f > vis[j])
            {
                f = vis[j]; m = j;
            }
        }
        if(f == inf) break;
        book[m] = 1;
        ans += vis[m];
        for(j = 1; j <= n; j++)
        {
            if(!book[j] && vis[j] > maps[m][j])
            vis[j] = maps[m][j];
        }
    }
    for(i = 1; i <= n; i++)
        if(vis[i] == inf)
        return -1;
    return ans;
}
int main()
{
    int t, n, s, i;
    int kk = 0;
    string s1, s2;
    cin >> t;
    while(t--)
    {
        int cnt = 0;
        scanf("%d", &n);
        memset(book, 0, sizeof(book));
        memset(maps, 0x3f, sizeof(maps));
        mp.clear();
        for(i = 1; i <= n; i++)
        {
            cin >> s1 >> s2 >> s;
            if(!mp.count(s1))  mp[s1] = ++cnt;
            if(!mp.count(s2))  mp[s2] = ++cnt;
            if(maps[mp[s1]][mp[s2]] > s)
                maps[mp[s1]][mp[s2]]  = maps[mp[s2]][mp[s1]] = s;
        }
        int ans = pri(cnt);
        if(ans == -1)
            printf("Case %d: Impossible\n", ++kk);
        else
            printf("Case %d: %d\n", ++kk, ans);

    }
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值