#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. iOS应用崩溃日志分析 iOS应用崩溃日志揭秘

    转自:http://www.raywenderlich.com/zh-hans/30818/ios%E5%BA%94%E7%94%A8%E5%B4%A9%E6%BA%83%E6%97%A5%E5%BF ...

  2. AVL树,红黑树,B-B+树,Trie树原理和应用

    前言:本文章来源于我在知乎上回答的一个问题 AVL树,红黑树,B树,B+树,Trie树都分别应用在哪些现实场景中? 看完后您可能会了解到这些数据结构大致的原理及为什么用在这些场景,文章并不涉及具体操作 ...

  3. UICollectionViewController xcode6.1 自定义Cell

    本文转载至 http://blog.csdn.net/daleiwang/article/details/40423219 UICollectionViewContAutolayoutstoryboa ...

  4. CentOS笔记-其他杂记

    1.忘记密码时,可以用single模式来修改密码,为了安全,可以禁用single模式,参考网址如下 Centos服务器禁止单用户模式(single)来增强系统安全 2.远程登录:ssh root@xx ...

  5. 在C/C++中使用VLD检测内存泄漏

    VLD地址:https://kinddragon.github.io/vld/ 若出现内存泄漏,VS输出窗口会有如下提示: 若要确定造成内存泄漏的代码位置,仅需进入工程属性->链接器->调 ...

  6. 用Java编写的http下载工具类,包含下载进度回调

    HttpDownloader.java package com.buyishi; import java.io.FileOutputStream; import java.io.IOException ...

  7. 获取Android设备无线和以太网MAC地址

    package com.raycloud.wolf.blogformac; import android.net.wifi.WifiManager; import android.support.v7 ...

  8. MRP 流程

    正常流程是这样的:需要>MRP>PLANNED ORDER>PR>PO PLANNED ORDER>PR>PO之间的转换可手工或自动设置转换的时间点是根据计划边际, ...

  9. elasticsearch索引查询,日志搜素

    索引查询 http://10.199.137.115:9200/_cat/indices?format=json 返回json字符串的索引状态 增加索引名称过滤 http://10.199.137.1 ...

  10. 修改u-boot的开机logo及显示过程【转】

    本文转载自;http://blog.csdn.net/voice_shen/article/details/6789424 [ u-boot: Git://git.denx.de/u-boot.git ...