最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树
输入很蓝瘦
**并查集
int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); }
找到x在并查集里的根结点,如果两个端点在同一个集合内,find之后两个值就相等了
每次找到权值最小的端点不在同一集合的边 把两个集合合并
#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int fa[];
struct node
{
int s, e, d;
}l[];
bool cmp(node x, node y)
{
if(x.d != y.d) return x.d < y.d;
}
int find(int x)
{
return x == fa[x] ? x : fa[x] = find(fa[x]);
}
int main()
{
while()
{
int n, i, j, x, val, sum = , cnt = ;//cnt是边数
char c;
scanf("%d", &n);
if(n == ) break;
for(i = ; i < n - ; i++)
{
scanf(" %c", &c);
int s = c - 'A' + ;
scanf("%d", &x);
for(j = ; j < x; j++)
{
scanf(" %c %d", &c, &val);
int e = c - 'A' + ;
l[cnt++] = (node){s, e, val};
}
}
sort(l, l + cnt, cmp);
for(int i = ; i <= n; i++)
fa[i] = i;
for(int i = ; i < cnt; i++)
{
int fs = find(l[i].s), fe = find(l[i].e);
if(fs == fe) continue;
sum += l[i].d;
fa[fs] = fe;
}
printf("%d\n", sum);
}
return ;
}
最小生成树 || 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 ...
- 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 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了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 (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- POJ 1251 & HDU 1301 Jungle Roads
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- HDOJ 1301 Jungle Roads
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 //HDOJ1301 #include<iostream>#include<c ...
随机推荐
- 【推荐】 体验SubSonic
SubSonic简介 SubSonic配置 利用sonic.exe来生成代码 通过Substage来生成代码 简单操作示例 1.SubSonic简介 一句讲完就是:SubSonic就是一个ORM开源框 ...
- Hibernate 4.3 配置文件实现
1.建立web项目 2.复制相关的jar文件到 项目的lib目录下antlr-2.7.7.jardom4j-1.6.1.jarhibernate-commons-annotations-4.0.5.F ...
- innobackupex参数说明
1.备份: #常用参数 --user:该选项表示备份账号. --password:该选项表示备份的密码. --port:该选项表示备份数据库的端口. --host:该选项表示备份数据库的地址. --s ...
- sprintf系列函数
1 简介和功能 字符串格式化命令,主要功能是把格式化的数据写入某个字符串中.sprintf 是个变参函数. 把格式化的数据写入某个字符串缓冲区. 2 函数原型 int sprintf( char *b ...
- vim中编辑了代码 但是提示can not write的解决办法和代码对齐办法
方式1: 1 :w /tmp/xxxx(如果是c文件就.c拉) 保存在/tmp下面 2 从tmp中复制到有权限的目录下面 cp /tmp xxxx ./(当前目录) 方式2::w !sudo tee ...
- hihoCoder搜索二·骑士问题
#include <stdio.h> #include <string.h> #include <math.h> #include <algorithm> ...
- python __builtins__ zip类 (71)
71.'zip' , 函数用于将可迭代的对象作为参数,将对象中对应的元素打包成一个个元组,然后返回由这些元组组成的列表.如果各个迭代器的元素个数不一致,则返回列表长度与最短的对象相同,利用 * 号操作 ...
- IT兄弟连 JavaWeb教程 请求重定向案例
Check2Servlet类与Output2Servlet类之间为请求转发关系.在web.xml文件中,为Check2Servlet映射的URL为"/check2",为Output ...
- python中用代码实现99乘法表
第一种:使用for遍历循环嵌套 ,): ,x+): print("%s*%s=%s" % (y,x,x*y),end=" ") print("&quo ...
- TensorFlow多线程输入数据处理框架(四)——输入数据处理框架
参考书 <TensorFlow:实战Google深度学习框架>(第2版) 输入数据处理的整个流程. #!/usr/bin/env python # -*- coding: UTF-8 -* ...