https://www.bnuoj.com/v3/contest_show.php?cid=9154#problem/D

【Accepted】

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<cmath>
#include<algorithm>
#include<queue>
#include<map>
using namespace std;
const int maxn=;
const int maxm=maxn*maxn;
char str[];
int fa[maxn];
int n,m;
struct edge
{
int u;
int v;
int c;
bool operator<(const edge& a) const
{
return c<a.c;
}
}e[maxm];
int tot;
void init()
{
tot=;
for(int i=;i<maxn;i++)
{
fa[i]=i;
}
}
int find(int x)
{
return fa[x]==x?x:fa[x]=find(fa[x]);
}
void add(int u,int v,int c)
{
e[tot].u=u;
e[tot].v=v;
e[tot++].c=c;
}
int Kruscal()
{
sort(e,e+tot);
int cnt=;
int ans=;
for(int i=;i<tot;i++)
{
if(cnt==n-)
{
break;
}
int fx=find(e[i].u);
int fy=find(e[i].v);
if(fx!=fy)
{
fa[fy]=fx;
ans+=e[i].c;
cnt++;
}
}
return ans;
}
int main()
{ while(~scanf("%d",&n))
{
if(n==)
{
break;
}
init();
for(int i=;i<n-;i++)
{
scanf("%s%d",str,&m);
int u=str[]-'A';
while(m--)
{
int c;
scanf("%s%d",str,&c);
int v=str[]-'A';
add(u,v,c);
}
}
int ans=Kruscal();
printf("%d\n",ans);
}
return ;
}

最新文章

  1. 关于CryptoJS中md5加密以及aes加密的随笔
  2. localForage——轻松实现 Web 离线存储(转)
  3. word 使用宏批量设置表格
  4. Codeforces 710 E. Generate a String (dp)
  5. PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
  6. .Net程序员学习Linux(一)
  7. 创建了一个基于最短路径规划geoserver的wms服务
  8. android脚步---设置layout隐藏属性
  9. Day3-递归函数、高阶函数、匿名函数
  10. Spring MVC使用样例
  11. windows下安装DB2数据库以及使用Aqua Data Studio链接数据库
  12. Android进阶(十一)Android系统架构讲解
  13. 接口Set
  14. 京东饭粒捡漏V1.14
  15. May 25. 2018 Week 21st Friday
  16. Confluence 6 为登录失败编辑,禁用和配置验证码
  17. Introduction of filter in servlet
  18. 20165304《Java程序设计》第九周学习总结
  19. HTTP.ContentType
  20. php环境搭建及入门

热门文章

  1. JS防止页面被其他网站iframe使用方法
  2. 在linux中使用多个redis端口来构建redis集群
  3. c/s架构搭建
  4. 洛谷P3254 圆桌问题(最大流)
  5. Android(java)学习笔记156:开源框架post和get方式提交数据(qq登录案例)
  6. centos 更换yum源 (解决下载慢的问题)
  7. python基础一 day5 复习
  8. [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
  9. FWT板子
  10. [POJ] 2223 Muddy Fields