zoukankan      html  css  js  c++  java
  • LightOJ

    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
    ", ++t, sum);
    		else printf("Case %d: Impossible
    ", ++t);	
    	}
    	return 0;
    }
  • 相关阅读:
    数据结构与算法之二叉查找树精简要点总结
    数据结构与算法之并查集的精简要点总结
    数组嵌套实现哈希函数的数据分片应用
    InfluxDB 写入&查询 Qt工程(C++ 客户端 API)
    关于Maven中<packaging>产生的一些问题
    Spring Boot:The field file exceeds its maximum permitted size of 1048576 bytes
    Npm管理命令
    ES6转ES5(Babel转码器)
    IOC容器中的依赖注入(一)
    Nginx常用部分命令
  • 原文地址:https://www.cnblogs.com/zyq1758043090/p/11852524.html
Copyright © 2011-2022 走看看