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 题目大意:多个模式串.问匹配串中含有多少个模式串.注意模式串有重复,所以要累计重复结果. 解题 ...
随机推荐
- IntelliJ IDEA使用教程 (总目录篇)
注:本文来源于< IntelliJ IDEA使用教程 (总目录篇) > IntelliJ IDEA使用教程 (总目录篇) 硬件要求 IntelliJ IDEA 的硬件要求 安装包云 ...
- Confluence 6 配置白名单
Confluence 管理员可以通过添加 URLs 到白名单选择出入的链接和使用 RSS 宏,HTML 包含宏和小工具中的内容. 如果一个内容被添加到 Confluence 系统中,但是这个 URLs ...
- pod 使用详解
cd 进去到 项目目录 包含 xcodeproj 结尾的目录下 1 pod init 创建一个pod 文件 2 打开生产的pod 文件 然后 配置pod 文件 并保存 3 pod install 安 ...
- Java并发编程-并发工具类及线程池
JUC中提供了几个比较常用的并发工具类,比如CountDownLatch.CyclicBarrier.Semaphore. CountDownLatch: countdownlatch是一个同步工具类 ...
- LeetCode(72):编辑距离
Hard! 题目描述: 给定两个单词 word1 和 word2,计算出将 word1 转换成 word2 所使用的最少操作数 . 你可以对一个单词进行如下三种操作: 插入一个字符 删除一个字符 替换 ...
- 前端基础之初识HTML
一.web服务的本质 import socket def main(): sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) sock.b ...
- cf161d 求距离为k的点对(点分治,树形dp)
点分治裸题,但是用树形dp也能做 /* dp[u][k]表示在u下距离k的点数量 */ #include<bits/stdc++.h> using namespace std; ]; ], ...
- node.js 框架express关于报错页面的配置
1.声明报错的方法,以及相对应的页面 //把数据库的调用方法配置到请求中 server.use((req, res, next) => { //把数据库存入req中 req.db = db; / ...
- Spring-data-redis: serializer实例
spring-data-redis提供了多种serializer策略,这对使用jedis的开发者而言,实在是非常便捷.sdr提供了4种内置的serializer: JdkSerializationRe ...
- AI学习吧-Redis操作-事务、订阅
事务 #首先启动redis服务端和客户端:#关于事务,数据库中的事务指的是逻辑上的一组操作,这组操作要么都执行成功要么不执行成功,出现异常会回滚到初始状态. 在代码中加入xxx,代码报错的话,不会执行 ...