POJ1251 Jungle Roads
解题思路:看懂题意是关键,Kruskal算法,最小生成树模板。
上代码:
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn = ; //边的最大值
int A[], n, k, vis[maxn], father[]; int Find(int x)
{
return father[x] == x ? x : father[x] = Find(father[x]);
} struct node{
int x, y, w;
}p[maxn]; int cmp(node A, node B)
{
return A.w < B.w;//从小到大
} int main()
{
char ch;
int x, b;
while(~scanf("%d", &n) && n)
{
int t = n - ;
int cnt = ;
//要初始化哦
for(int i = ; i <= n; i++) father[i] = i;
while(t--)
{
scanf(" %c", &ch);
int m = ch - 'A' + ;
scanf("%d", &k);
while(k --)
{
p[cnt].x = m;//放在循环里面
scanf(" %c %d", &ch, &x);
b = ch - 'A' + ;
p[cnt].y = b, p[cnt++].w = x;
}
}
int sum = ;
sort(p, p + cnt, cmp);
for(int i = ; i < cnt; i++)
{
int rootx = Find(p[i].x);
int rooty = Find(p[i].y);
//根节点不同,则加起来,并放到同一个集合
if(rootx != rooty)
{
sum += p[i].w;
father[rootx] = rooty;
}
}
printf("%d\n", sum);
}
return ;
}
POJ1251 Jungle Roads的更多相关文章
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25993 Accepted: 12181 De ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- poj1251 Jungle Roads(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普 ...
- POJ1251 Jungle Roads【最小生成树】
题意: 首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值.拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了 ...
- POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解
题意: 输入n,然后接下来有n-1行表示边的加边的权值情况.如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25.求连接这棵树的最小权值. 思路: 一开始是在做莫 ...
- poj1251 Jungle Roads Kruskal算法+并查集
时限: 1000MS 内存限制: 10000K 提交总数: 37001 接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
随机推荐
- how can i get the source code path && file names from an ELF file(compired with -g)?
https://stackoverflow.com/questions/31333337/how-can-i-get-the-source-code-path-file-names-from-an-e ...
- phpword使用
composer 安装 https://packagist.org/packages/phpoffice/phpword 开发文档:http://phpword.readthedocs.io/en/l ...
- 华为交换机S5700系列配置通过STelnet登录设备示例
配置通过STelnet登录设备示例 组网图形 图1 配置用户通过STelnet登录设备组网图 在服务器端生成本地密钥对 <HUAWEI> system-view [HUAWEI] sysn ...
- Keras实现autoencoder
Keras使我们搭建神经网络变得异常简单,之前我们使用了Sequential来搭建LSTM:keras实现LSTM. 我们要使用Keras的functional API搭建更加灵活的网络结构,比如说本 ...
- 深入解析Linux内核及其相关架构的依赖关系
Linux kernel 成功的两个原因: 灵活的架构设计使得大量的志愿开发者能够很容易加入到开发过程中:每个子系统(尤其是那些需要改进的)都具备良好的可扩展性.正是这两个原因使得Linux kern ...
- 并发-HashMap和HashTable源码分析
HashMap和HashTable源码分析 参考: https://blog.csdn.net/luanlouis/article/details/41576373 http://www.cnblog ...
- MySql判断汉字、日期、数字的具体函数
几个平常用的mysql函数,MySql判断汉字.日期.数字的具体函数分享给大家,具体内容如下 1.判断字符串是否为汉字 返回值:1-汉字 0-非汉字 ? 1 2 3 4 5 6 7 8 9 10 11 ...
- 完全理解Android中的RemoteViews
一.什么是RemoteViews RemoteViews翻译过来就是远程视图.顾名思义,RemoteViews不是当前进程的View,是属于SystemServer进程.应用程序与RemoteView ...
- thinkphp <eq> <if>标签 condition中可以写PHP的判断逻辑
<ul> <volist name="monthArray" id="monthItem"> <if condition=&quo ...
- tensorflow笔记:模型的保存与训练过程可视化
tensorflow笔记系列: (一) tensorflow笔记:流程,概念和简单代码注释 (二) tensorflow笔记:多层CNN代码分析 (三) tensorflow笔记:多层LSTM代码分析 ...