裸的最小生成树

输入很蓝瘦

**并查集

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. 数据库MySQL技术-基础知识

    数据库技术: SQL,关系数据库标准 注意: 环境编码:  cmd客户端是固定的gbk编码  而php网页中,是该网页文件的编码(现在主流都是utf8). mysql> set names gb ...

  2. SCUT - 249 - A piece of Cake - 组合数学

    https://scut.online/contest/25/I 由结论:d维物体切n刀分成的部分=sum(C(n,0)~C(n,d)),直接算就行了.

  3. 51nod 1103【鸽巢原理】

    思路: 这道题嘛有些弯还是要转的,比如你说让你搞n的倍数,你别老老实实照她的意思去啊,倍数可以除法,取膜 . 因为n个数我们可以求前缀和然后取膜,对n取膜的话有0-n-1种情况,所以方案一定是有的,说 ...

  4. 黑客攻防技术宝典web实战篇:攻击其他用户习题

    猫宁!!! 参考链接:http://www.ituring.com.cn/book/885 随书答案. 1. 在应用程序的行为中,有什么“明显特征”可用于确定大多数 XSS 漏洞? 用户提交的输入在应 ...

  5. NOIp 2014 解方程 【数学/秦九韶算法/大数取膜】By cellur925

    题目传送门 题意:求高次方程的解及其个数.其中 1° 我们知道,高次方程是没有求根公式的.但是利用逆向思维,我们可以进行“试根法”,因为题目中给出了所求根的范围.但是多项式系数过于吓人,达到了sxbk ...

  6. SpringBoot整合Spring Data Solr

    此文不讲solr相关,只讲整合,内容清单如下 1. maven依赖坐标 2. application.properties配置 3. Java Config配置 1. maven坐标 <depe ...

  7. background-size属性

    background-size:属性有 auto:length :百分比 length 如:10px 20px 固定的 或者是写成一个 ,10px  另外一个就默认为 auto; 写成百分比的形式 是 ...

  8. Educational Codeforces Round 46 (Rated for Div. 2) E. We Need More Bosses

    Bryce1010模板 http://codeforces.com/contest/1000/problem/E 题意: 给一个无向图,求图的最长直径. 思路:对无向图缩点以后,求图的最长直径 #in ...

  9. AtCoder Grand Contest 017 B

    B - Moderate Differences Time limit : 2sec / Memory limit : 256MB Score : 400 points Problem Stateme ...

  10. [已读]JavaScript面向对象编程指南

    又是一个忽悠人的书名,其实这本书的花了大量内容阐述JS的基础语法,BOM,DOM,事件,ajax(这个和很多js书一样).最后一章则是编程模式与设计模式. 我觉得与面向对象没多大关系,要算的话,pro ...