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 ...
随机推荐
- spring hystrix和内置tomcat组件的参数调优解析
1. springboot内置tomcat容器的参数配置 server: port: 12021 # server端的socket超时间(毫秒),使用值-1表示没有(即无限)超时,默认值为60000( ...
- Linux ES集群服务配置说明
说明: ES官网不建议在root用户使用Elastic Server,因此ES集群配置均使用普通账户操作,新建账户 elastic. Linux版本为CentOS 7.3,ES版本为5.5.0. 一. ...
- Aras Innovator获取项目任务序列号
//方法名:GetProjectTasksNumber //功能描述:获取项目任务序列号 //原作者:joe //创建时间:20141225 //版权所有(C)JOE.FAN //---------- ...
- 利用FastJson,拼接复杂嵌套json数据&&直接从json字符串中(不依赖实体类)解析出键值对
1.拼接复杂嵌套json FastJson工具包中有两主要的类: JSONObject和JSONArray ,前者表示json对象,后者表示json数组.他们两者都能添加Object类型的对象,但是J ...
- Maven--传递性依赖和依赖范围
依赖范围不仅可以控制依赖与三种 classpath 的关系,还对传递性依赖产生影响. 假设 A 依赖于 B,B依赖于 C,我们说 A 对于 B 是第一直接依赖,B 对于 C 是第二直接依赖,A 对于 ...
- JavaSE--类加载器
参考:http://www.importnew.com/6581.html Java 编译器会为虚拟机转换源指令.虚拟机代码存储在以 .class 为扩展名的类文件中,每个类文件都包含某个类或者接口的 ...
- 远程调用shell脚本文件和远程复制文件
1.安装sshpass yum install sshpass 2.本地调用远程服务器的shell脚本文件: sshpass -p sa ssh root@192.168.56.105 -C &quo ...
- 移动端web前端开发
移动端浏览器现状 视口 meta视口标签 二倍图 移动端主流方案 移动端技术解决方案 移动端常见布局 1.流式布局(百分比布局) 2.flex布局 3.rem适配布局 1)rem单位 2)媒体查询 3 ...
- jquery.marquee.js - 有点奇怪的跑马灯动画,不过还是加上去了
客户想要一个跑马灯的效果,最终我用了jquery.marquee.js. 这个库很简单就能用. 效果是这样,从左到右,移动速度都不一样. 1. HTML <div class="mar ...
- typescript 使用的几种情况
接口的创建 可以使用 type 和 interface 来创建类型 type 特有的优点: 声明基本类型别名,联合类型,元组等类型 type S = string; type IFoo = IBar ...