#include<iostream>
#include<algorithm>
using namespace std; const int maxn = ;
int n, m;
int fa[];
struct node
{
int x, y;
int cost;
}arr[maxn]; void init()
{
for (int i = ; i <= maxn; i++)
{
fa[i] = i;
}
} int find(int x)
{
if (x != fa[x])
{
return find(fa[x]);
}
else
return fa[x];
} bool cmp(node a, node b)
{
return a.cost<b.cost;
} int main()
{
char c1, c2;
int k, c;
while (cin >> n)
{
if (n == ) break;
int j = ; //表示边的数量
init();
for (int i = ; i<n; i++)
{
cin >> c1 >> k;
while (k--)
{
cin >> c2 >> c;
arr[j].x = c1 - 'A';
arr[j].y = c2 - 'A';
arr[j].cost = c;
j++;
}
}
sort(arr, arr + j, cmp); //排序
int ans = ;
for (int i = ; i<j; i++)
{
int fx, fy;
fx = find(arr[i].x);
fy = find(arr[i].y);
if (fx != fy)
{
ans += arr[i].cost;
fa[fy] = fx;
}
}
cout << ans << endl;
}
return ;
}

hdu1301 Jungle Roads 基础最小生成树的更多相关文章

  1. HDU-1301 Jungle Roads(最小生成树[Prim])

    Jungle Roads Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 65536/32768K (Java/Other) Total ...

  2. POJ 1251 && HDU 1301 Jungle Roads (最小生成树)

    Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...

  3. POJ 1251 Jungle Roads(最小生成树)

    题意  有n个村子  输入n  然后n-1行先输入村子的序号和与该村子相连的村子数t  后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离  求链接全部村子的最短路径 还是裸的最小生成树咯 ...

  4. HDU1301 Jungle Roads

    Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...

  5. POJ1251 Jungle Roads 【最小生成树Prim】

    Jungle Roads Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 19536   Accepted: 8970 Des ...

  6. Jungle Roads(最小生成树)

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total ...

  7. hdu1301 Jungle Roads 最小生成树

    The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...

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

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

  9. POJ 1251 Jungle Roads (最小生成树)

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

随机推荐

  1. 简明扼要谈Spring IOC的好处

    http://a-kuei.iteye.com/blog/676524 iOC:控制反转,它是不是什么技术,它是一种设计模式.所谓控制反转就是由容器控制程序间的关系,而不是传统实现中,由编程代码直接操 ...

  2. JDK提供的几种常用的锁

    可重入互斥锁: Lock lock = new ReentrantLock() lock.lock(); ... lock.unlock(); 信号量: Semaphore semaphore = n ...

  3. Java程序员从笨鸟到菜鸟之(十四)Html基础积累总结(上)

     本文来自:曹胜欢博客专栏.转载请注明出处:http://blog.csdn.net/csh624366188 注:由于本文内含有大量html标签,所以在排版上有些困难,所以排版有点难看,请大家见谅 ...

  4. monitor and move the log content to our big data system

    Apache Flume HDFS Sink Tutorial | HowToProgram https://howtoprogram.xyz/2016/08/01/apache-flume-hdfs ...

  5. swt_table 回车可编辑Esc取消

    package 宿舍管理系统; import java.util.Hashtable; import org.eclipse.swt.SWT; import org.eclipse.swt.custo ...

  6. CSS中的那点事儿(一)--- CSS中的单位1

    单位主要用在规定元素的数值性css属性的,这里主要讨论应用的比较多的是width height  padding margin font-size,而单位中应用最广泛就是%.px.em 百分比 百分比 ...

  7. model.js

    var Model = { inherited: function () {}, created: function () {}, prototype: { init: function (attrs ...

  8. eclipse本地覆盖版本库

    1,右键team,与资源库同步 2,选中冲突文件,右键“更新”,此时本地代码出现冲突 3,选中冲突文件,右键点击“标记为解决”,勾选第二项,以本地版本为准 4,冲突被解决,正常提交本地代码

  9. [置顶] SQL Server 2005 双机热备的实现

    [置顶] SQL Server 2005 双机热备的实现 分类: SQLSERVER2011-08-24 21:25 901人阅读 评论(0) 收藏 举报 sql servermicrosoftsql ...

  10. 后台while收发过程

    fuse_loop_mt.c 中fuse_do_work函数使用while循环在后台不断运行,每一个while循环中,主要有两个操作. 1. fuse_session_receive_buf(mt-& ...