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 ...
随机推荐
- OpenCV笔记(5)(定位票据并规范化、调库扫描文本)
一.定位和变换票据 定位照片中的不规范票据或矩形文本,并将其变换为正规矩形,以供OCR识别. # -*- coding:utf-8 -*- __author__ = 'Leo.Z' import cv ...
- Javascript兼容各浏览器的日期转换
var date = new Date(Date.parse("2015-09-05".replace(/-/g,"/")));'2015-09-05'是无法被 ...
- sh_01_列表基本使用
sh_01_列表基本使用 name_list = ["zhangsan", "lisi", "wangwu"] # 1. 取值和取索引 # ...
- Selenium 环境安装
前言: 本人在学习Selenium时,用的版本是Python3.6+Selenium3,后续写的所有学习资料都是基于这套环境.在安装Selenium3前,请确保本机已安装好了Python3,如未安装可 ...
- Ubuntu18.04安装rabbitvcs svn图形化客户端和简单实用
1.1 自带source源里面查找rabbitvcs信息 sudo apt search rabbitvcs 1.2 安装rabbitvcs sudo apt install rabbitvcs- ...
- 兼容pc端和移动端的轮播图插件 swiper.js
swiper.js是一款纯JavaScript打造的滑动特效插件,可以用来实现检点轮播图,tab触摸滑动切换等常用效果.下载地址:https://www.swiper.com.cn/download/ ...
- ubuntu16.04增大swap空间
参见->这里 参见->这里
- C++入门经典-例5.19-指针的引用与传递参数
1:引用传递参数与指针传递参数能达到同样的目的.指针传递参数也属于一种值传递,其传递的是指针变量的副本.如果使用指针的引用,就可以达到在函数体内改变指针地址的目的.运行代码如下: // 5.19.cp ...
- “fatal error: hdf5.h: 没有那个文件或目录”解决方法
問題一: Installing Caffe without CUDA: fatal error: cublas_v2.h No such file: 在Makefile.config中修改,將CPU_ ...
- [学习笔记] CNN与RNN方法结合
CNN与RNN的结合 问题 前几天学习了RNN的推导以及代码,那么问题来了,能不能把CNN和RNN结合起来,我们通过CNN提取的特征,能不能也将其看成一个序列呢?答案是可以的. 但是我觉得一般直接提取 ...