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 管理协同编辑 - 修改编辑模式
编辑模式确定了你站点所有用户使用协同编辑的体验,这个是你对协同编辑进行启用和关闭的地方. 希望修改编辑模式: 进入 > 基本配置(General Configuration) > 协同编 ...
- Linux 用户(user)和用户组(group)管理概述
一.理解Linux的单用户多任务,多用户多任务概念: Linux 是一个多用户.多任务的操作系统:我们应该了解单用户多任务和多用户多任务的概念: 1.Linux 的单用户多任务:单用户多任务:比如我们 ...
- 第八单元 正文处理命令及tar命令
使用cat命令进行文件的纵向合并 两种文件的纵向合并方法 归档文件和归档技术 归档的目的 什么是归档 tar命令的功能 tar命令的常用选项 使用tar命令创建.查看及抽取归档文件 使用tar命令 ...
- npm无反应的问题&npm常用命令
RT: windows安装完nodejs后做了相关环境变量配置后,cmd输入npm命令无反应,就光标一直闪,百度了半天终于找到解决办法 解决方法:C:\Users\Administrator(或你的账 ...
- laravel 服务提供者
服务提供者,在laravel里面,其实就是一个工厂类.它最大的作用就是用来进行服务绑定.当我们需要绑定一个或多个服务的时候,可以自定义一个服务提供者,然后把服务绑定的逻辑都放在该类的实现中.在lara ...
- ATM机模拟系统
实验要求 https://pan.baidu.com/s/1Sq5joWyVsNhY8DYiyI4UmQ 源代码 /*信1705-1 20173458 王嘉梁*/ package atm; impor ...
- JS实现简单的观察者模式
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- axios 发送json数据
var qs = require('qs'); axios.post(ajaxurl,qs.stringify({ username:'zhangsan', age:'18' })).then(fun ...
- Linux-server-sshd
Linux-server-sshd 1:安装 OpenSSH 服务需要4 个软件包 openssh-5.3p1-114.el6_7.x86_64:包含OpenSSH 服务器及客户端需要的核心文件 op ...
- Stuck on "Authenticating with iTunes Store"
https://forums.developer.apple.com/thread/76803 Try this, it fixed it for me. Open Terminal and run: ...