Snowy Roads 最小生成树Kruskal算法

 
 

Description

There was a heavy snow in the town yesterday. Some roads in the town are covered with thick snow so that they are hard to pass. The Snow Cleaning Team wants to make the intersections in the town connect to each other, in other words, one can walk from any intersection to any others only by passing the roads without snow. And they want the length of the snowy roads they have to clean is as short as possible. Help them to find the minimum length of the roads they must clean.

Input

The first line is a positive integer T (0<T<=10), T test cases follow.

For each of the test case, the first line will be three non-negative integers N (0<N<=100), the number of intersections in the town, M1 (0<=M1<=1000), the number of roads that are not covered with snow, and M2 (0<=M2<=1000), the number of roads covered with snow. The intersections are numbered from 0 to N-1.

Then M1 lines follow, each of the lines contain two integers X, Y (0<=X, Y<N, X≠Y), it means that there is a road without snow between intersection X and Y.

Then M2 lines follow, each of the lines contain three integers X, Y, Z (0<=X, Y<N, X≠Y, 0<Z<=1000), it means that there is a road with snow between intersection X and Y, and the team need time Z minutes to clean it.

It is guaranteed that it is possible to clean some snowy roads to make all the intersections connected. And there is at most one road between two intersections.

Output

For each of the test cases output only one integer in one line, the minimum length of the snowy roads they have to clean.

Sample Input

Copy sample input to clipboard

3

3 0 3

0 1 4

1 2 5

2 0 6

4 3 3

2 1

0 1

2 3

3 1 1

0 3 3

0 2 2

4 3 3

3 1

1 2

2 3

0 3 7

0 2 8

0 1 9

Sample Output

9

0

7

题意:

有N个城市,有M1条没有被雪覆盖的路,有M2条有被雪覆盖的路;

输入M1组<x,y>,代表城市x到城市y的路没有被雪覆。

输入M2组<x, y, z>,代表城市x到城市y的路被雪覆盖的路程为z。

求遍历这N个城市需要清除被雪覆盖的最小的路程。

PS:很久之前写过这个算法,最近复习一些数据结构知识,顺便整理这个算法模板出来。

参考《算法导论(第三版)中文版》P366

<pre name="code" class="cpp">#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;int root[101];struct Node {int x;int y;int w;Node(int a, int b, int c) {x = a;y = b;w = c;}
};bool cmp(Node a, Node b) {return a.w < b.w;
}int main() {int ncase;cin >> ncase;while (ncase--) {int n, m1, m2;int x, y, z;cin >> n >> m1 >> m2;vector<Node> v;for (int i = 0; i < m1; i++) {cin >> x >> y;v.push_back(Node(x, y, 0));}for (int i = 0; i < m2; i++) {cin >> x >> y >> z;v.push_back(Node(x, y, z));}for (int i = 0; i < n; i++) root[i] = i;sort(v.begin(), v.end(), cmp);int len = 0, flag = 0;for (int i = 0; i < v.size(); i++) {int x = v[i].x;int y = v[i].y;if (root[x] != root[y]) {len += v[i].w;flag++;if (flag == n - 1) break; // n个城市相连,最多为n-1条边int root_x = root[x];int root_y = root[y];for (int j = 0; j < n; j++) {if (root[j] == root_y) {root[j] = root_x;  // 更新根节点}}}}cout << len << endl;}return 0;
}

1005. F.Snowy Roads最小生成树Kruskal算法相关推荐

  1. 图的最小生成树-Kruskal算法

    问题引入 [问题描述] 编写程序,利用带权无向图的邻接矩阵存储,实现图的最小生成树Kruskal算法. [输入形式] 输入图的顶点序列及图的边的情况.如样例所示.边的输入以输入-1,-1,-1作为结束 ...

  2. HDOJ 1863畅通工程(最小生成树kruskal算法并查集实现)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1863 最小生成树kruskal算法:http://www.zhuoda.org/irini/78592.h ...

  3. 最小生成树kruskal算法

    最小生成树kruskal算法 概述 算法分析 代码 概述 克鲁斯卡尔(Kruskal)(Kruskal)(Kruskal)算法是求连通网的最小生成树的另一种方法.与普里姆(Prim)(Prim)(Pr ...

  4. 实现最小生成树Kruskal算法(附完整代码)

    实现最小生成树Kruskal算法(附完整代码) Kruskal算法是一种常见的计算最小生成树的算法.它的主要思想是将所有的边按照权值从小到大进行排序,并逐个加入到生成树中,如果加入后不会形成环,则保留 ...

  5. 最小生成树kruskal算法并查集版 C语言实现

    今天数据结构课讲了最小生成树的Kruskal算法和Prim算法,不过都只是概念,可能是怕他们听不懂吧,反正算法实现一概不讲...囧 下午抱着<算法导论>跑去图书馆看Kruskal算法,发现 ...

  6. 图论(九)最小生成树-Kruskal算法

    前面说过,Kruskal是从最短边着手构建最小生成树的.其基本过程是:先对图中的所有边按照权重值从小到大进行排序,然后着手选取边构建最小生成树.如果直接从小到大按顺序选取,有可能形成了环,所以对环的处 ...

  7. 算法实践--最小生成树(Kruskal算法)

    什么是最小生成树(Minimum Spanning Tree) 每两个端点之间的边都有一个权重值,最小生成树是这些边的一个子集.这些边可以将所有端点连到一起,且总的权重最小 下图所示的例子,最小生成树 ...

  8. C++ 实现无向图的最小生成树Kruskal算法(完整代码)

    按照Kruskal思想,n个结点的生成树有n-1条边,故反复上述过程,直到选取了n-1条边为止,就构成了一棵最小生成树. 实现Kruskal算法的关键问题是: 当一条边加入T的边集中后,如何判断是否构 ...

  9. 最小生成树 Kruskal算法 Prim算法

    文章目录 Kruskal算法 模板: Prim算法 模板: Kruskal算法 模板: https://blog.csdn.net/Rain722/article/details/65642992 K ...

  10. * poj 1251 JungleRoad 最小生成树 Kruskal算法、Prim算法

    文章目录 Kruskal算法 模板:https://blog.csdn.net/Rain722/article/details/65642992 Prim算法 模板: poj 1251 JungleR ...

最新文章

  1. 04.微博消息的语言检测
  2. R,python(pandas)以及linux shell 常见命令对比
  3. I.MX6 View长宽大于屏的分辨率
  4. linux shell获取字符串第1个字符
  5. 下一代对话系统中的关键技术(下篇)
  6. 上币至iamToken
  7. moxy json介绍_MOXy作为您的JAX-RS JSON提供程序–服务器端
  8. ansys命令流_ANSYS命令流建模3之划分单元+施加弹簧
  9. 大家身边极度聪明的人是什么样子?
  10. ui kit模板,让新手设计师临摹提高!
  11. 关于最新上映电影的评论
  12. Kickstarter 开源其 Android 和 iOS 应用
  13. JDRefresh 轻简下拉刷新框架
  14. Java 无限级递归生成树级菜单
  15. Jlink-V9虚拟串口接口(JTAG、SWD、TTL)
  16. JVM菜鸟进阶高手之路十(基础知识开场白)
  17. 计算机软件技术就业方向,软件技术的就业前景和就业方向
  18. JRUL数字交流三相电流继电器
  19. python中的pd进行数据处理
  20. Mac平台配置OpenGL(glut,glew)

热门文章

  1. PostgreSQL数据库概述
  2. codeforces1359E Modular Stability
  3. 新手写的一个12306刷票工具
  4. Unity Shader混合模式笔记(Blend)
  5. 工信部,映射,映射端口,热备,磁盘,虚拟磁盘,冗余,磁盘阵列技术,廉价冗余磁盘阵列 ,RAID 0,RAID1 ,RAID 3,RAID 5,RAID 6 名词解释
  6. python random函数sample_Python random.sample()用法及代码示例
  7. Python爬虫自学系列(一)
  8. asp文件解密 加密
  9. 30本从禁书到名著的阅读书单
  10. 4个漂亮的wordpress企业主题