HDU 1301-Jungle Roads【Kruscal】模板题
题目链接>>>
题目大意:
给出n个城市,接下来n行每一行对应该城市所能连接的城市的个数,城市的编号以及花费,现在求能连通整个城市所需要的最小花费。
解题分析:
最小生成树模板题,下面用的是kruscal算法。
//Kruscal算法采用的是"加边"的想法
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
];
int n, m, k;
struct line
{
int x,y,c;
}edge[];
bool cmp(line a, line b)
{
return a.c < b.c;
}
int find(int x)
{
int r = x;
while (father[r] != r)r = father[r];
int i = x, j;
while (father[i] != r)
{
j = father[i];
father[i] = r;
i = j;
}
return r;
}
void kruskal()
{
int i, j;
;
sort(edge, edge + k, cmp);
; i <= k; i++) //按从小到大的顺序将所有不会构成环的边加入当前边集
{
int f1 = find(edge[i].x);
int f2 = find(edge[i].y);
if (f1 != f2)
{
father[f2] = f1;
sum += edge[i].c;
}
}
printf("%lld\n", sum);
}
int main()
{
,a2;
char str1, str2;
,n)
{
k = ;
memset(edge, , sizeof(edge)); //记得每次都要清空bian数组
; i < n; i++)
{
getchar();
scanf("%c%d", &str1, &m);
; j <= m; j++)
{
getchar();
scanf("%c%d", &str2, &a2);
edge[k].x = str1 - ; //第一个村庄的编号从1开始
edge[k].y = str2 - ;
edge[k].c = a2;
k++;
}
}
; i <= n; i++) //初始化father数组
{
father[i] = i;
}
kruskal();
a1++;
}
;
}
2018-04-01
HDU 1301-Jungle Roads【Kruscal】模板题的更多相关文章
- POJ 1251 && HDU 1301 Jungle Roads (最小生成树)
Jungle Roads 题目链接: http://acm.hust.edu.cn/vjudge/contest/124434#problem/A http://acm.hust.edu.cn/vju ...
- HDU 1301 Jungle Roads (最小生成树,基础题,模版解释)——同 poj 1251 Jungle Roads
双向边,基础题,最小生成树 题目 同题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<stri ...
- hdu 1301 Jungle Roads 最小生成树
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1301 The Head Elder of the tropical island of Lagrish ...
- hdu 1301 Jungle Roads krusckal,最小生成树,并查集
The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was s ...
- hdu 1301 Jungle Roads
http://acm.hdu.edu.cn/showproblem.php?pid=1301 #include <cstdio> #include <cstring> #inc ...
- Hdu 1301 Jungle Roads (最小生成树)
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1301 很明显,这是一道“赤裸裸”的最小生成树的问题: 我这里采用了Kruskal算法,当然用Prim算法也 ...
- POJ 1251 & HDU 1301 Jungle Roads
题目: Description The Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign ...
- POJ 1251 + HDU 1301 Jungle Roads 【最小生成树】
题解 这是一道裸的最小生成树题,拿来练手,题目就不放了 个人理解 Prim有些类似最短路和贪心,不断找距当前点最小距离的点 Kruskal类似于并查集,不断找最小的边,如果不是一棵树的节点就合并为一 ...
- 最小生成树 || HDU 1301 Jungle Roads
裸的最小生成树 输入很蓝瘦 **并查集 int find(int x) { return x == fa[x] ? x : fa[x] = find(fa[x]); } 找到x在并查集里的根结点,如果 ...
- HDU 2222(AC自动机模板题)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=2222 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- Confluence 6 从一个 XML 备份中导入一个空间
有下面 2 中方法可以导入一个空间——通过上传一个文件,或者从你 Confluence 服务器上的一个目录中导入.上传文件仅仅是针对一个小站点的情况.为了取得最好的导入结果,我们推荐你从服务器上的目录 ...
- Android CTS Test
什么是CTS测试?了解这个问题前,我们先来搜索了解一遍“Google GMS 认证”.GMS全称为GoogleMobile Service,即谷歌移动服务.说白了GMS其实就是一系列谷歌的应用集合.谷 ...
- java----static关键字(包括final)
static修饰字段: 使用static关键字修饰一个字段:声明的static变量实际上就是一个全局变量 使用static关键字修饰一个方法:可以直接使用类调用方法,和对象没有关系了 使用static ...
- django----图书管理
待完成 from django.db import models # Create your models here. class Book(models.Model): nid = models.A ...
- 步步为营-78-新闻展示(Ajax+Json+easyUI)
Json:JavaScript Object Notation 1.1 Json对象的接收处理 <!DOCTYPE html> <html xmlns="http://ww ...
- 实战--Keepalived和LVS实现负载高可用
显然,只有上一篇的操作,在WEB运维技术中,只能承担一半的角色. 想像一下,如何LVS本身倒了,肿么办?后端的NGINX再多,也只能是干着急,请求过来不呀! 所以,在本篇时,我们来实现LVS永不倒, ...
- LVS(IPVS)了解
从来都只是看文章,现在手工作一下. 参考URL: https://blog.csdn.net/langyue919/article/details/80935197 https://www.cnblo ...
- CentOS7安装wps
https://blog.csdn.net/u010445843/article/details/77828552
- kafka 备忘
创建topic/usr/local/kafka/bin/kafka-topics.sh --create --zookeeper 127.0.0.1:2181 --partitions 3 --rep ...
- zookeeper 学习 状态机复制的共识算法
https://www.youtube.com/watch?v=BhosKsE8up8 state machine replication 的 共识(consensus) 算法 根据CAP理论,一个分 ...