题目:

Description


The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems.

Input

The input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above. 

Output

The output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit. 

Sample Input

9
A 2 B 12 I 25
B 3 C 10 H 40 I 8
C 2 D 18 G 55
D 1 E 44
E 2 F 60 G 38
F 0
G 1 H 35
H 1 I 35
3
A 2 B 10 C 40
B 1 C 20
0

Sample Output

216
30

题意:

有n个村庄,按字母顺序输入每个村庄的代表字母,其连有k条路,重复的不计。然后输入这k个村庄及这两村庄之间需要的费用。计算最小的费用。

思路:

  • 并查集和最小生成树的应用,建立字母和初始化编号间的关系即可解决问题。

代码:

 #include <iostream>
#include <algorithm>
#define MAXN 27
using namespace std; int pre[MAXN]; struct node{
int begin;
int end;
int len;
}s[MAXN * (MAXN - )]; bool cmp(node a, node b)
{
return a.len < b.len;
} void init()
{
for(int i = ; i < MAXN; i++)
{
pre[i] = i;
} } int find(int x)
{
while(x != pre[x])
x = pre[x]; return x;
} int merge(int fx, int fy)
{
if(fx != fy)
{
pre[fx] = fy;
return ;
}
else
return ;
} int kruskal(int cnt)
{
int minlen = ;
sort(s, s + cnt, cmp);
for(int i = ; i < cnt; i++)
{
int fx = find(s[i].begin);
int fy = find(s[i].end);
if(merge(fx, fy))
minlen += s[i].len;
}
return minlen;
} int main()
{
char a, b;
int n, k, w;
while(scanf("%d", &n) != EOF)
{
if(n == )
break; init();
int cnt = ;
for(int i = ; i < n-; i++) //n个结点有n-1条路
{
cin >> a >> k;
for(int j= ; j < k; j++)
{
cin >> b >> w;
s[cnt].begin = a - 'A' + ;
s[cnt].end = b - 'A' + ;
s[cnt].len = w;
cnt++;
}
} printf("%d\n", kruskal(cnt));
}
return ;
}

总结:

刚开始用scanf进行输入结果一直没反应超时了,就这样前后又花了好多时间。

  • scanf在输入字符类型时容易把空格当作字符读入,总之需要连续输入字符型和整型的话还是避开scanf用cin比较好,因为cin不会出现scanf这样的问题。

POJ 1251 & HDU 1301 Jungle Roads的更多相关文章

  1. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  2. POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】

    题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解  Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...

  3. HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads

    双向边,基础题,最小生成树   题目 同题目     #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...

  4. hdu 1301 Jungle Roads 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...

  5. hdu 1301 Jungle Roads

    http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...

  6. Hdu 1301 Jungle Roads (最小生成树)

    地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...

  7. hdu 1301 Jungle Roads krusckal,最小生成树,并查集

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

  8. 最小生成树 || HDU 1301 Jungle Roads

    裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...

  9. HDOJ 1301 Jungle Roads

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...

随机推荐

  1. javascript中的location的用法

    javascript中的location.href有很多种用法,主要如下. self.location.href="/url" 当前页面打开URL页面 location.href= ...

  2. php URL各部分获取方法(全局变量)

    php URL各部分获取方法(全局变量),主要介绍php全局变量$_SERVER的用法,有需要的朋友,可以参考下. 1.$_SESSION['PHP_SELF'] - 获取当前正在执行脚本的文件名 2 ...

  3. windows服务器搭建SVN[多项目设置方法]

    https://tortoisesvn.net/downloads.html 根据系统版本进行下载,下载后正常一路正常安装. 第一.设置版本号仓库目录,比如:cdengine 第二.在cdengine ...

  4. 完成在本机远程连接HBase进行数据增删改查

    1.进行hbase与本机远程连接测试连接 1.1 修改虚拟机文件hbase-site.xml(cd/usr/local/hbase/conf)文件,把localhost换成你的虚拟机主机名字 1.2修 ...

  5. Python—插入排序算法

    # 插入排序,时间复杂度O(n²) def insert_sort(arr): """ 插入排序:以朴克牌为例,从小到大排序.摸到的牌current与手里的每张牌进行对比 ...

  6. ZJNU 1367 - Party--中高级

    寻找从i到X,再从X到i的最短路 可以在正向图中从X开始跑一遍最短路,每个点的距离dis1[i]当作从X回到点i的距离 再将图反向从X再跑一遍,每个点的距离dis2[i]当作从i到点X的距离 最后搜索 ...

  7. 电影评论分类:二分类问题(IMDB数据集)

    IMDB数据集是Keras内部集成的,初次导入需要下载一下,之后就可以直接用了. IMDB数据集包含来自互联网的50000条严重两极分化的评论,该数据被分为用于训练的25000条评论和用于测试的250 ...

  8. Tensorflow学习教程------读取数据、建立网络、训练模型,小巧而完整的代码示例

    紧接上篇Tensorflow学习教程------tfrecords数据格式生成与读取,本篇将数据读取.建立网络以及模型训练整理成一个小样例,完整代码如下. #coding:utf-8 import t ...

  9. Java--Json解析

    普通Json {"code":"S0000", "describe":"数据正常返回", "result&qu ...

  10. 常用STL的常见用法

    //#pragma comment(linker, "/STACK:1024000000,1024000000") //#pragma GCC optimize(2) //#inc ...