POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解
题意:
输入n,然后接下来有n-1行表示边的加边的权值情况。如A 2 B 12 I 25 表示A有两个邻点,B和I,A-B权值是12,A-I权值是25。求连接这棵树的最小权值。
思路:
一开始是在做莫队然后发现没学过最小生成树,就跑过来做模板题了...
Kruskal的使用过程:先按权值大小排序,然后用并查集判断是否能加这条边
Kruskal详解博客:【贪心法求解最小生成树之Kruskal算法详细分析】---Greedy Algorithm for MST
考试周还在敲代码...我...
update:最近复习模板,蓝桥杯不给带模板TAT,顺手A了Prim做法。
代码:
/*Prim*/
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<queue>
#include<vector>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
typedef long long ll;
using namespace std;
const int maxn = + ;
const int seed = ;
const ll MOD = ;
const int INF = 0x3f3f3f3f;
int mp[maxn][maxn];
int dis[maxn], vis[maxn], pre[maxn], n, ans;
void init(){
scanf("%d", &n);
memset(vis, , sizeof(vis));
for(int i = ; i <= n; i++){
for(int j = ; j <= n; j++){
scanf("%d", &mp[i][j]);
}
}
vis[] = ;
for(int i = ; i <= n; i++){
dis[i] = mp[][i];
pre[i] = ;
}
}
void prim(){
ans = ;
for(int i = ; i <= n - ; i++){
int pos, Min = INF;
for(int j = ; j <= n; j++){
if(!vis[j] && dis[j] < Min){
pos = j;
Min = dis[j];
}
}
vis[pos] = ;
ans = max(ans, mp[pos][pre[pos]]);
for(int j = ; j <= n; j++){
if(!vis[j] && mp[j][pos] < dis[j]){
dis[j] = mp[j][pos];
pre[j] = pos;
}
}
}
}
int main(){
int t, n;
scanf("%d", &t);
while(t--){
init();
prim();
printf("%d\n", ans);
}
return ;
}
代码:
#include<queue>
#include<cstring>
#include<set>
#include<map>
#include<stack>
#include<cmath>
#include<vector>
#include<cstdio>
#include<iostream>
#include<algorithm>
#define ll long long
const int N = 1e5+;
const int MOD = ;
using namespace std;
struct edge{
int u,v,value;
friend bool operator < (edge a,edge b){
return a.value < b.value;
}
}e[N];
int fa[];
int find(int x){
return fa[x] == x? x : find(fa[x]);
}
int main(){
int n,u,v,tmp;
int num;
char s[];
while(scanf("%d",&n) && n){
num = ;
for(int i = ;i < ;i++) fa[i] = i;
for(int i = ;i <= n-;i++){
scanf("%s%d",s,&tmp);
u = s[] - 'A';
for(int j = ;j <= tmp;j++){
scanf("%s%d",s,&e[num].value);
e[num].u = u;
e[num].v = s[] - 'A';
num++;
}
}
sort(e,e+num);
int ans = ;
for(int i = ;i < num;i++){
int x = find(e[i].u);
int y = find(e[i].v);
if(x != y){
fa[x] = fa[y];
ans += e[i].value;
}
}
printf("%d\n",ans);
}
return ;
}
POJ1251 Jungle Roads (最小生成树&Kruskal&Prim)题解的更多相关文章
- POJ1251 Jungle Roads 【最小生成树Prim】
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19536 Accepted: 8970 Des ...
- POJ1251 Jungle Roads(Kruskal)(并查集)
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 23882 Accepted: 11193 De ...
- POJ1251 Jungle Roads Kruskal+scanf输入小技巧
Jungle Roads The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ai ...
- HDU1301&&POJ1251 Jungle Roads 2017-04-12 23:27 40人阅读 评论(0) 收藏
Jungle Roads Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25993 Accepted: 12181 De ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- kruskal算法求最小生成树(jungle roads的kruskal解法)
注意: 注意数组越界问题(提交出现runtimeError代表数组越界) 刚开始提交的时候,边集中边的数目和点集中点的数目用的同一个宏定义,但是宏定义是按照点的最大数定义的,所以提交的时候出现了数组越 ...
- POJ1251 Jungle Roads【最小生成树】
题意: 首先给你一个图,需要你求出最小生成树,首先输入n个节点,用大写字母表示各节点,接着说有几个点和它相连,然后给出节点与节点之间的权值.拿第二个样例举例:比如有3个节点,然后接下来有3-1行表示了 ...
- poj1251 Jungle Roads(Prime || Kruskal)
题目链接 http://poj.org/problem?id=1251 题意 有n个村庄,村庄之间有道路连接,求一条最短的路径能够连接起所有村庄,输出这条最短路径的长度. 思路 最小生成树问题,使用普 ...
- poj1251 Jungle Roads Kruskal算法+并查集
时限: 1000MS 内存限制: 10000K 提交总数: 37001 接受: 17398 描述 热带岛屿拉格里山的首长有个问题.几年前,大量的外援花在了村庄之间的额外道路上.但是丛林不断地超 ...
随机推荐
- gulp-jshint使用说明
hint是暗示的意思,jshint是什么意思? 1.使用npm安装 cnpm i --save-dev gulp-jshint jshint ps:gulp-jshint和jshnt要一起下载,安装. ...
- 为什么说Java语言是平台无关的?
适当的整理了一下: 一.平台与机器指令 无论哪种编程语言编写的应用程序都需要经过操作系统和处理器来完成程序的运行,因此这里的平台是又OS和CPU所构成的,所谓的平台无关就是指软件的运行不会因操作系统. ...
- UIStoryboard跳转界面
/**1.创建Storyboard,加载Storyboard的名字,这里是自己创建的Storyboard的名字*/ UIStoryboard *storyboard = [UIStoryboard s ...
- 007-Redi-命令-脚本命令、链接命令、服务器命令、事务、HyperLogLog
Redis 脚本命令 下表列出了 redis 脚本常用命令: 序号 命令及描述 1 EVAL script numkeys key [key ...] arg [arg ...] 执行 Lua 脚本. ...
- 发现《深入理解C++11》中扩展的friend代码的错误
目前在总结现代C++的新特性,看了<深入理解C++11>这本书. 今天看到扩展的friend语法这一节,遇到了问题.本节电子版内容参见:https://book.2cto.com/2013 ...
- java读取resource目录下的配置文件
java读取resource目录下的配置文件 1:配置resource目录 下的文件 host: 127.0.0.1 port: 9300 2:读取 / 代表resource目录 InputSt ...
- PAT 1068 Find More Coins[dp][难]
1068 Find More Coins (30)(30 分) Eva loves to collect coins from all over the universe, including som ...
-  
这个东西在 html 里是空格占位符,普通的空格在 html 里如果连续的多个可能被认为只有一个,而这个东西你写几个就能占几个空格位.
- numpy.random.rand()/randn()/randint()/normal()/choice()/RandomState()
这玩意用了很多次,但每次用还是容易混淆,今天来总结mark一下~~~ 1. numpy.random.rand(d0,d1,...,dn) 生成一个[0,1)之间的随机数或N维数组 np.random ...
- 集成树模型使用自动搜索模块GridSearchCV,stacking
一. GridSearchCV参数介绍 导入模块: from sklearn.model_selection import GridSearchCV GridSearchCV 称为网格搜索交叉验证调参 ...