【Kruscal最小生成树】D. Jungle Roads
2024-09-07 16:42:17
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 ;
}
最新文章
- 关于CryptoJS中md5加密以及aes加密的随笔
- localForage——轻松实现 Web 离线存储(转)
- word 使用宏批量设置表格
- Codeforces 710 E. Generate a String (dp)
- PKU 1458 Common Subsequence(最长公共子序列,dp,简单)
- .Net程序员学习Linux(一)
- 创建了一个基于最短路径规划geoserver的wms服务
- android脚步---设置layout隐藏属性
- Day3-递归函数、高阶函数、匿名函数
- Spring MVC使用样例
- windows下安装DB2数据库以及使用Aqua Data Studio链接数据库
- Android进阶(十一)Android系统架构讲解
- 接口Set
- 京东饭粒捡漏V1.14
- May 25. 2018 Week 21st Friday
- Confluence 6 为登录失败编辑,禁用和配置验证码
- Introduction of filter in servlet
- 20165304《Java程序设计》第九周学习总结
- HTTP.ContentType
- php环境搭建及入门
热门文章
- JS防止页面被其他网站iframe使用方法
- 在linux中使用多个redis端口来构建redis集群
- c/s架构搭建
- 洛谷P3254 圆桌问题(最大流)
- Android(java)学习笔记156:开源框架post和get方式提交数据(qq登录案例)
- centos 更换yum源 (解决下载慢的问题)
- python基础一 day5 复习
- [BZOJ3940]:[Usaco2015 Feb]Censoring(AC自动机)
- FWT板子
- [POJ] 2223 Muddy Fields