POJ 1251 & HDU 1301 Jungle Roads
题目:
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
Output
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的更多相关文章
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- Hdu 1301 Jungle Roads (最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...
- 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 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
随机推荐
- 杂记 -- 关于less在vue项目中的使用
1.安装less,less-loader npm install less less-loader --save 2.配置wepack.js(vue3+版本中不用自己设置) //添加less路径模块 ...
- Thinkcmf截取内容长度
例1: {$vo.post_title|msubstr=0,10} 截取标题,msubstr=0,10,数字表示截取的字符串长度,显示省略号,但无论长度是否超过截取的长度都会出现省略号: 例2: {$ ...
- poj2778 矩阵乘法+ac自动机
题:http://poj.org/problem?id=2778 题意:给定m个模式串,问长度为n的字符串不包含这些模式串的有几种可能 分析:因为n很大,所以考虑矩阵ksm来解决,构造一个矩阵res[ ...
- 关于http协议的总结
http协议知识结构图 简介 HTTP(HyperText Transfer Protocol),超文本传输协议,是Web应用的基本协议 HTTP规定了客户端(浏览器)和服务器之间的通信步骤以及通信时 ...
- Idea创建Spring项目
环境 win7 + Idea2018 Classpath commons-logging-1.2 + spring-framework-4.1.6.RELEASE Step1 创建工程 File -& ...
- tensorflowlite 分类模型从训练到安卓部署
https://blog.csdn.net/qq_33200967/article/details/82773677
- 输入一段汉字可以获得首字母简拼的java代码
package com.zl; import java.io.UnsupportedEncodingException; public class Test12 { public static voi ...
- ES6之模块化
本文介绍ES6实现模块化的方法:使用import和export. 导入的时候需不需要加大括号的判断:1.当用export default people导出时,就用 import people 导入(不 ...
- 论文翻译——Recursive Deep Models for Semantic Compositionality Over a Sentiment Treebank
Abstract Semantic word spaces have been very useful but cannot express the meaning of longer phrases ...
- 16)PHP, set_include_path
代码展示: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w ...