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 ...
随机推荐
- 多版本python的pip 升级后, pip2 pip3 与python版本失配
mint19.2 本来pip 和 pip2 对应 python2.7 pip3对应python3.6 用源码安装了python3.7之后. 这样 版本也没问题. 但是, 用pip3.7 安装 ...
- openvas 安装
NMAP apt-get update & apt-get upgrade kali的更新命令 https://www.fujieace.com/kali-linux/update-sourc ...
- C++入门经典-例3.19-使用break跳出循环
1:代码如下: // 3.19.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> usin ...
- vue项目内嵌入到app input type=file 坑(文件上传插件)
w问题描述: 我用vue-cli完成的一个移动端项目,内嵌到app当中,用原生的input type=file 来完成文件上传.在安卓下没有问题但是在苹果手机 上传第二次手机就会发生白屏 并无缘无故跳 ...
- [论文理解] Attentional Pooling for Action Recognition
Attentional Pooling for Action Recognition 简介 这是一篇NIPS的文章,文章亮点是对池化进行矩阵表示,使用二阶池的矩阵表示,并将权重矩阵进行低秩分解,从而使 ...
- Dell做RAID配置图文全教程
首先准备工作:Dell服务器 首先重启Dell,我们会看到Dell服务器的启动页面: 当我们看到出现下面这个界面的时候,根据提示按下Ctrl+R进到SAS磁盘阵列里面 备注:大多数Dell服务器进磁盘 ...
- P1070道路游戏题解
日常吐槽 作为hin久hin久以前考试考到过的一道题窝一直咕咕咕到现在才想起来去做因为讲解都忘干净了然后自己重新考虑发现被卡了3天 题面 看到题目发现这题的dp状态似乎有点不是很明确? 我们来理一理题 ...
- 开源控件slidingmenu的使用
在github.com网站搜索slidingmenu后https://github.com/jfeinstein10/SlidingMenu 下载demo,导入library到你的项目中,添加到你项目 ...
- python 实验2 分支结构
该博客专为我的小伙伴们提供参考而附加,没空加上代码具体解析,望各位谅解 实验一 货币转换 写一个程序进行人民币和欧元间币值转换,其中: ...
- GIS开源程序收集
每一个项目包含以下信息: 名称 主题 分类 描述 开始时间 语言 许可 演示网址 项目网址 成熟度 活跃度 评价 分类包括:GIS基础函数库.GIS控件.GIS桌面程序.GIS数据引擎.WEBGI ...