LightOJ - 1041 Road Construction(最小生成树)

18 篇文章 0 订阅
13 篇文章 1 订阅

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

Sample Output

Case 1: 31

Case 2: Impossible



典型的最小生成树题,只不过这道题没有给出城市数目,需要根据给出的边中城市的数量去重后计算出城市数目,由于map中的元素实现一一对应的关系,所以这道题用STL的map来写就很容易的解决这个问题了;

#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<map>
#define inf 99999999
using namespace std;
map<string,int>mp;
int e[220][220],b[220],dis[220],n;
string sta,stb;
int str(string s)
{
    if(!mp[s])
    {
        mp[s]=++n;
        return n;
    }
    return mp[s];
}
int main()
{
    int t,m,i,j,k,cost,t1,t2,min,u,v,sum;
    k=0;
    scanf("%d",&t);
    while(t--)
    {
        mp.clear();
        scanf("%d",&m);
        for(i=1; i<=110; i++)
            for(j=1; j<=110; j++)
                    e[i][j]=inf;
        n=0;
        for(i=1; i<=m; i++)
        {
            cin>>sta>>stb>>cost;
            t1=str(sta);//利用map中元素不重复,一一对应的性质,来将字符串给转换成对应的数字;
            t2=str(stb);
            if(cost<e[t1][t2])
                e[t1][t2]=e[t2][t1]=cost;
        }
        for(i=1; i<=n; i++)
        {
            dis[i]=e[1][i];
            b[i]=0;
        }
        b[1]=1;
        sum=0;
        for(i=1; i<n; i++)
        {
            min=inf;
            for(j=1; j<=n; j++)
            {
                if(b[j]==0&&dis[j]<min)
                {
                    min=dis[j];
                    u=j;
                }
            }
            b[u]=1;
            sum+=min;
            for(v=1; v<=n; v++)
            {
                if(b[v]==0&&dis[v]>e[u][v])
                    dis[v]=e[u][v];
            }
        }
        if(sum>=inf)
            printf("Case %d: Impossible\n",++k);
        else
            printf("Case %d: %d\n",++k,sum);
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值