POJ - 1251 Jungle Roads (最小生成树&并查集
#include<iostream>
#include<algorithm>
using namespace std;
int ans=,tot=;
const int N = 1e5;
int f[];
struct ac{
int v,u,w;
}edge[N];
bool cmp(ac a,ac b){
return a.w<b.w;
}
inline int find(int x){
if(x!=f[x])f[x]=find(f[x]);return f[x];
}
inline int join(int x,int y,int w){
int fx=find(x),fy=find(y);
if(fx!=fy){
f[fx]=fy;ans+=w;tot++;
}
}
int main()
{
int n;string a,b;int m,w,cnt=;
while(cin>>n&&n){
//cin.ignore();
ans=tot=cnt=;
for(int i = ;i <=n;++i)f[i]=i;
for(int i = ;i <n;++i){
cin>>a;cin>>m;
while(m--){
cin>>b;cin>>w;
edge[cnt].u=a[]-'A'+,edge[cnt].v=b[]-'A'+,edge[cnt].w=w;
cnt++;
}
}
sort(edge,edge+cnt,cmp);
for(int i = ;i < cnt;++i){
join(edge[i].u,edge[i].v,edge[i].w);
if(tot==n-)break;
}
cout<<ans<<endl;
}
return ;
}
AC代码
https://vjudge.net/problem/POJ-1251
读题读的有点难受,其余很简单
POJ - 1251 Jungle Roads (最小生成树&并查集的更多相关文章
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- POJ 1251 Jungle Roads - C语言 - Kruskal算法
Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid ...
- [ An Ac a Day ^_^ ] [kuangbin带你飞]专题六 最小生成树 POJ 1251 Jungle Roads
题意: 有n个点 每个点上有一些道路 求最小生成树 解释下输入格式 A n v1 w1 v2 w2 A点上有n条边 A到v1权值是w1 A到v2权值是w2 思路: 字符串处理之后跑kruskal求最小 ...
- POJ 1251 Jungle Roads(最小生成树)
题意 有n个村子 输入n 然后n-1行先输入村子的序号和与该村子相连的村子数t 后面依次输入t组s和tt s为村子序号 tt为与当前村子的距离 求链接全部村子的最短路径 还是裸的最小生成树咯 ...
- POJ 1251 Jungle Roads (prim)
D - Jungle Roads Time Limit:1000MS Memory Limit:10000KB 64bit IO Format:%I64d & %I64u Su ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- POJ 1251 Jungle Roads (最小生成树)
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- POJ 1251 Jungle Roads(Kruskal算法求解MST)
题目: The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money w ...
随机推荐
- poj 2761 主席树的应用(查询区间第k小值)
Feed the dogs Time Limit: 6000MS Memory Limit: 65536K Total Submissions: 22084 Accepted: 7033 De ...
- BZOJ 4386 Luogu P3597 [POI2015]Wycieczki (矩阵乘法)
题目链接: (bzoj) https://www.lydsy.com/JudgeOnline/problem.php?id=4386 (luogu) https://www.luogu.org/pro ...
- 【Python】学习笔记一:Hello world
前言 在我看来,无论我们学习什么语言第一个学习的估计都是Hello world,那么接下来就从Hello world说起! 编写代码 我在本机上已经安装了pycharm,所以我所编辑的代码都是在pyc ...
- 实现图像添加label
void CmapwingisTest2View::OnToolsAddTiffLayer() { TCHAR szFilters[]= _T("TIFF Files (*.tif)|*.t ...
- LeetCode 198. 打家劫舍(House Robber)LeetCode 213. 打家劫舍 II(House Robber II)
打家劫舍 题目描述 你是一个专业的小偷,计划偷窃沿街的房屋.每间房内都藏有一定的现金,影响你偷窃的唯一制约因素就是相邻的房屋装有相互连通的防盗系统,如果两间相邻的房屋在同一晚上被小偷闯入,系统会自动报 ...
- Learn The Architecture Memory Management 译文
1.概述 本文档介绍了ARMv8-A架构内存管理的关键——内存地址转换,包括虚拟地址(VA)到物理地址(PA)的转换.页表(或称地址转换表)格式以及TLBs(Translation Lookaside ...
- AOP 代理类的创建
AOP 代理类的创建 入口:AnnotationAwareAspectJAutoProxyCreator#postProcessAfterInitialization 和 AnnotationAwar ...
- Prism 订阅事件 IEventAggregator 说明
本节学习了Event Aggregation事件聚合,这个在Prism中很重要,特别是对于Module间的通信.除了前面介绍的Command可以用于模块间的通信,还有我们这一节介绍的Event Agg ...
- docker(常见调试技巧):docker打包镜像调试技巧
写Dockerfile可以先不指定CMD.ENTRYPOINT等启动命令,只要拷贝就好了 如下: # Dockerfile for basic-app-client # Build with: # d ...
- Delphi中的Free和Nil和freeandnil函数
Delphi中的Free和Nil 在Delphi中释放对象资源时一般用Obj.Free(Obj为一个实例名),不过程Delphi中还有一个FreeAndNil(对象名)函数,那么用哪个好呢?Free和 ...