裸的最小生成树

输入很蓝瘦

**并查集

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. 如何 Xcode 开发工具里安装一个空的项目末模板

    很多朋友因为Xcode升级取消了空工程模板而发愁  今天给大家推荐一个简单方便的方法,导入空工程模板 对于 xcode7 来说可以使用下面的方法添加空模板.建议在升级的时候,不要下载beta版,最好下 ...

  2. uploadify提示修改为中文

    使用uploadify时报错时是英文提示,并且上传文件进度条显示的是英文,如何修改为中文呢,直接打开jquery.uploadify.min.js(如果你使用的是jquery.uploadify.js ...

  3. poj2239 poj1274【二分匹配】

    题意: 就是尽可能的选多的课 思路: 把课程和上课的时间看作二分图 跑一跑二分匹配就好了 #include<iostream> #include<cstdio> #includ ...

  4. 力荐!35 个最好用的 Vue 开源库!

    无论是开发新手还是经验丰富的老手,我们都喜欢开源软件包.对于开发者来说,如果没有这些开源软件包,很难想象我们的生活会变得多么疲惫不堪,而且靠咖啡度日也会成为家常便饭.所幸的是,随着 Vue.js 和 ...

  5. Qt基本应用

    1 使用方式 在qt designer中直接设计图形界面,然后使用pyGUI转换成py文件. 可以发现,转换的文件为一个class.并不是一个完整的程序(运行时无法出现窗口).这个类名字是Ui_Mai ...

  6. April Fools Contest 2017 D

    Description Input The only line of the input contains a string of digits. The length of the string i ...

  7. Educational Codeforces Round 18 B

    Description n children are standing in a circle and playing the counting-out game. Children are numb ...

  8. Java图解

    java虚拟机 JVM运行过程: java开发工具包 java入门图解1 java入门图解2 java入门图解3 java入门图解4

  9. cpp extern 关键字用法

    语法说明: extern 可以置于变量或者函数前,以标示变量或者函数的在别的文件中定义,提示编译器遇到此变量和函数后,在其他模块中寻找其定义.此外extern也可用来进行链接指定. 即 extern ...

  10. 抽象类 abstract

    抽象类就是拿来继承的抽象方法就是拿来重写的 1.用abstract可以用来修饰类或方法,分别叫抽象类和抽象方法. 2.含有抽象方法的类必须被声明为抽象类.,抽象类必须被继承,抽象方法也必须被重写. 3 ...