ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-08-11 10:04:52  阅读:546  来源: 互联网

标签:Dhaka 1041 cities Rajshahi LightOJ Construction roads include road


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;
}

 

标签:Dhaka,1041,cities,Rajshahi,LightOJ,Construction,roads,include,road
来源: https://blog.csdn.net/qq_41505957/article/details/99166202

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有