LightOJ - 1041:Road Construction (最短路+字符串处理)

https://vjudge.net/problem/LightOJ-1041

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

 

题意分析:

有的城市之间的道路有所损坏,0代表未损坏,其他数字代表修复道路所需要的价钱,如果要使所有的城市之间都连通,最少需要多少资金。

解题思路:

用map函数存储所有城市名,再用克鲁斯卡尔算法求最短路。

#include <stdio.h>
#include <algorithm>
#include <map>
#include <iostream>
#define N 120
using namespace std;
struct edge{
	char a[N];
	char b[N];
	int w;
}e[N];
int f[N];
int cmp(edge a, edge b)
{
	return a.w<b.w;
}
int getf(int v)
{
	if(f[v]==v)
		return v;
	f[v]=getf(f[v]);
	return f[v];
}
int merge(int u, int v)
{
	int t1, t2;
	t1=getf(u);
	t2=getf(v);
	if(t1!=t2)
	{
		f[t1]=t2;
			return 1;
	}
	return 0;
}
int main()
{
	int T, t=0, n, m, i, sum, point, temp;
	map<string, int>maps;
	scanf("%d", &T);
	while(T--)
	{
		maps.clear();
		scanf("%d", &m);
		n=0;
		for(i=1; i<=m; i++)
		{
			scanf("%s%s%d", e[i].a, e[i].b, &e[i].w);
			if(maps[e[i].a]==0)
				maps[e[i].a]=++n;
			if(maps[e[i].b]==0)
				maps[e[i].b]=++n;
		}
		for(i=1; i<=n; i++)
			f[i]=i;
		sort(e+1, e+m+1, cmp);
		sum=0;
		point=1;
		temp=0;
		for(i=1; i<=m; i++)
		{
			if(merge(maps[e[i].a], maps[e[i].b]))
			{
				point++;
				sum+=e[i].w;
			}
			if(point==n)
			{
				temp=1;
				break;
			}
		}
		if(temp==1)
			printf("Case %d: %d\n", ++t, sum);
		else printf("Case %d: Impossible\n", ++t);	
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

张宜强

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值