裸的最小生成树

输入很蓝瘦

**并查集

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的更多相关文章

  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. hdu 1301 Jungle Roads 最小生成树

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

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

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

  4. 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 ...

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

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

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

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

  7. POJ 1251 & HDU 1301 Jungle Roads

    题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...

  8. hdu 1301 Jungle Roads

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

  9. HDOJ 1301 Jungle Roads

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

随机推荐

  1. Meta标签中的format-detection属性及含义(转)

    一.Meta标签中的format-detection属性及含义 意为:格式检测 或许你会有这样的经历:当你在制作手机端的页面中,点击了没有加任何链接的格式的数字时,这时手机会进行自动拔号提示操作! 禁 ...

  2. 【旧文章搬运】Windows句柄表格式

    原文发表于百度空间,2009-02-28========================================================================== 句柄是Wi ...

  3. 记一次OutOfMemory定位过程-续

    在前文<记一次OutOfMemory定位过程>完成时最终也没有定位到ECS 中JVM Heap size无法控制的原因,今天再次尝试终于有了一些线索,翻查了ECS的部署脚本发现了memor ...

  4. POJ2576【背包】

    题意: 每个人必须在一个团队或其他; 人对两支球队的数量不得超过1不同; 人们对各队的总重量应尽可能接近相等越好. 思路: 那么我求一个能接近最接近总和一半的值. 每个人的值就是物品,每个物品有且只有 ...

  5. Cg(C for Graphic)语言语义词与语义绑定详述 (转)

    摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 语义词( Semantic )与语义绑定 ...

  6. Mybatis 未设置主键映射报错;Cause: java.sql.SQLSyntaxErrorException: Unknown column 'system_id' in 'field list'

    使用MyBatis的时候,主键的字段建议绑定在Bean的属性上面, import javax.persistence.*; public class User { @Id @Column(name = ...

  7. 键值观察 KVO

    http://www.cnblogs.com/dyf520/p/3805297.html Key-Value Observing Programming Guide 1,注册Key-Value Obs ...

  8. GitHub笔记---邮箱访问错误

    GitHub地址太长,所以需要一个变量来保存 把远程仓库赋值给一个变量,以后就用就这变量代表这个地址 GitHub推送push 推送过程中发生一个小插曲,出现了错误,错误提示我复制过来吧 remote ...

  9. 详解基于linux环境MySQL搭建与卸载

    本篇文章将从实际操作的层面,讲解基于linux环境的mysql的搭建和卸载. 1  搭建mysql 1.1  官网下载mysql压缩包 下载压缩包时,可以先把安装包下载到本地,再上传到服务器,也可以在 ...

  10. Linux下tcp服务器创建的步骤

    创建一个socket,使用函数socket() socket(套接字)实质上提供了进程通信的端点,进程通信之前,双方首先必须建立各自的一个端点,否则没有办法通信.通过socket将IP地址和端口绑定之 ...