#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. 细说linux IPC(三):mmap系统调用共享内存

    [版权声明:尊重原创,转载请保留出处:blog.csdn.net/shallnet 或 .../gentleliu,文章仅供学习交流,请勿用于商业用途]         前面讲到socket的进程间通 ...

  2. Struts2的配置文件——web.xml

    任何MVC框架都需要与Web应用整合,这就不得不借助于web.xml文件,只有配置在web.xml文件中Servlet才会被应用加载. 通常,所有的MVC框架都需要Web应用加载一个核心控制器,对于S ...

  3. 常用到的JS 验证(包括例子)

    //验证是否为空    function check_blank(obj, obj_name){         if(obj.value != ''){                  retur ...

  4. 答案{{index==0 ? '一' : (index==1 ? '二':'三' )}}

    答案{{index==0 ? '一' : (index==1 ? '二':'三' )}}    

  5. Writing a Simple YARN Application 从hadoop生态抽出yarn ,单独使用yarn

    Apache Hadoop 2.9.1 – Hadoop: Writing YARN Applications https://hadoop.apache.org/docs/current/hadoo ...

  6. How do I set the timeout for a JAX-WS webservice client?

    How do I set the timeout for a JAX-WS webservice client? up vote58down votefavorite 27 I've used JAX ...

  7. hibernate面试点

    1.谈谈你对hibernate的认识和理解 01.全自动的ORM框架 02.子项目 03.面向对象的思想来解决操作数据库 01.hibernate是一个开放源代码的对象关系映射(ORM)框架,它对JD ...

  8. kaminari分页插件样式

    修改国际化文件,zh-cn views: pagination: first: "首页" last: "尾页" previous: "上一页" ...

  9. jdbc navcat for mysql 连不上远程服务器的原因(安全组设置)

    如果你权限,防火墙什么都设置好了,但是还是连不上远程数据库, 那么你就必须要看看你的服务器上安全组的设置(很重要) 这里以阿里云为例子(之前用阿里云服务都没设置),现在阿里云的服务器租的时候就要求配置 ...

  10. POJ 2421 Constructing Roads (Kruskal算法+压缩路径并查集 )

    Constructing Roads Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19884   Accepted: 83 ...