Constructing Roads

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 19847    Accepted Submission(s): 7594

Problem Description
There are N villages, which are numbered from 1 to N, and you should build some roads such that every two villages can connect to each other. We say two village A and B are connected, if and only if there is a road between A and B,
or there exists a village C such that there is a road between A and C, and C and B are connected.




We know that there are already some roads between some villages and your job is the build some roads such that all the villages are connect and the length of all the roads built is minimum.
 
Input
The first line is an integer N (3 <= N <= 100), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within
[1, 1000]) between village i and village j.



Then there is an integer Q (0 <= Q <= N * (N + 1) / 2). Then come Q lines, each line contains two integers a and b (1 <= a < b <= N), which means the road between village a and village b has been built.
 
Output
You should output a line contains an integer, which is the length of all the roads to be built such that all the villages are connected, and this value is minimum.

 
Sample Input
3
0 990 692
990 0 179
692 179 0
1
1 2
 
Sample Output

179

首先将已有的顶点放入集合中,然后再kruskal


#include <iostream>
#include <algorithm>
#include <cstring>
using namespace std; struct node {
int u, v, w;
}edge[10010]; #define mem(a) memset(a, 0, sizeof(a))
int par[110];
int cmp(node a, node b) {
return a.w < b.w;
} int find(int a) {
if (a != par[a]) return find(par[a]);
else return a;
} int kruskal(int n, int num) {
int ans = 0;
sort(edge, edge+num, cmp); for (int i = 0; i<num; i++) {
int x = edge[i].u, y = edge[i].v;
x = find(x), y = find(y);
if (x != y) {
ans += edge[i].w;
par[y] = x;
}
}
return ans;
} int main() {
int n;
while (cin >> n) {
mem(edge);
mem(par);
int num = 0;
for (int i = 1; i<=n; i++) {
for (int j = 1; j<=n; j++) {
int k;
cin >> k;
if (i >= j) continue;
edge[num].u = i;
edge[num].v = j;
edge[num++].w = k;
}
}
for (int i = 1; i<=n; i++) par[i] = i;
int q;
cin >> q;
while (q --) {
int x, y;
cin >> x >> y;
x = find(x);
y = find(y);
par[x] = y;
}
cout << kruskal(n, num) << endl;
}
return 0;
}

HDU1102(最小生成树Kruskal算法)的更多相关文章

  1. 【转】最小生成树——Kruskal算法

    [转]最小生成树--Kruskal算法 标签(空格分隔): 算法 本文是转载,原文在最小生成树-Prim算法和Kruskal算法,因为复试的时候只用到Kruskal算法即可,故这里不再涉及Prim算法 ...

  2. 最小生成树——kruskal算法

    kruskal和prim都是解决最小生成树问题,都是选取最小边,但kruskal是通过对所有边按从小到大的顺序排过一次序之后,配合并查集实现的.我们取出一条边,判断如果它的始点和终点属于同一棵树,那么 ...

  3. 最小生成树Kruskal算法

    Kruskal算法就是把图中的所有边权值排序,然后从最小的边权值开始查找,连接图中的点,当该边的权值较小,但是连接在途中后会形成回路时就舍弃该边,寻找下一边,以此类推,假设有n个点,则只需要查找n-1 ...

  4. 最小生成树------Kruskal算法

    Kruskal最小生成树算法的概略描述:1 T=Φ:2 while(T的边少于n-1条) {3 从E中选取一条最小成本的边(v,w):4 从E中删去(v,w):5 if((v,w)在T中不生成环) { ...

  5. 求最小生成树——Kruskal算法

    给定一个带权值的无向图,要求权值之和最小的生成树,常用的算法有Kruskal算法和Prim算法.这篇文章先介绍Kruskal算法. Kruskal算法的基本思想:先将所有边按权值从小到大排序,然后按顺 ...

  6. 最小生成树 kruskal算法&prim算法

    (先更新到这,后面有时间再补,嘤嘤嘤) 今天给大家简单的讲一下最小生成树的问题吧!(ps:本人目前还比较菜,所以最小生成树最后的结果只能输出最小的权值,不能打印最小生成树的路径) 本Tianc在刚学的 ...

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

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

  8. 模板——最小生成树kruskal算法+并查集数据结构

    并查集:找祖先并更新,注意路径压缩,不然会时间复杂度巨大导致出错/超时 合并:(我的祖先是的你的祖先的父亲) 找父亲:(初始化祖先是自己的,自己就是祖先) 查询:(我们是不是同一祖先) 路径压缩:(每 ...

  9. 数据结构之最小生成树Kruskal算法

    1. 克鲁斯卡算法介绍 克鲁斯卡尔(Kruskal)算法,是用来求加权连通图的最小生成树的算法. 基本思想:按照权值从小到大的顺序选择n-1条边,并保证这n-1条边不构成回路. 具体做法:首先构造一个 ...

随机推荐

  1. 个人的MySql配置总结

    lower_case_table_names参数是用来设置MySQL是否让Schema和数据表大小写敏感,我测试的是在查询界面和MySQL控制台界面无法改变它的值,要在配置文件中改变(先关闭服务),一 ...

  2. 507. Perfect Number

    We define the Perfect Number is a positive integer that is equal to the sum of all its positive divi ...

  3. JS画几何图形之五【过圆外一点作切线】

    样例:http://www.zhaojz.com.cn/demo/draw9.html 依赖:[点].[直线].[圆] //画切线 //point 圆外的一点 //dot 圆心 //r 半径 func ...

  4. Oracle数据库中插入日期型数据(to_date的用法)(转载)

    往Oracle数据库中插入日期型数据(to_date的用法) INSERT  INTO  FLOOR  VALUES  ( to_date ( '2007-12-20 18:31:34' , 'YYY ...

  5. 阅读MDN文档之CSS选择器介绍(一)

    本文为阅读MDN文档笔记 目录 Different types of Selectors Attribute Selectors Presence and value attribute select ...

  6. MySQL 的调节和优化的提示

    MySQL 服务器硬件和操作系统调节: 1. 拥有足够的物理内存来把整个InnoDB文件加载到内存中——在内存中访问文件时的速度要比在硬盘中访问时快的多.2. 不惜一切代价避免使用Swap交换分区 – ...

  7. 移动端IOS第三方输入法遮挡底部Input及android键盘回落留白问题

    var interval; //消息框获取焦点 $('#J_text').focus(function(){ interval = setInterval(function() { scrollToE ...

  8. python Is 与== 的坑

    以前看过一篇python技术贴,说用is替代==,这样更加pythonic?然后我就能把用'=='的地方用'Is'替代,结果程序运行结果的偏差很大,甚至完全不同.后来发现,Is与==使用上是有区别的. ...

  9. Python新手需要掌握的知识点

    一.基础语法 1 变量 2 逻辑判断 3 循环 4 函数 二.数据结构 1 数字(加减乘除) 2 字符串(一串字符) 3 布尔 (真假) 4 元组 (不能修改的列表) 5 列表(Python的苦力,最 ...

  10. CDN 边缘规则,三秒部署、支持定制、即时生效,多种规则覆盖常用业务场景

    2017年的最后一周,又拍云进行了一次重要升级,将自定义 Rewrite 升级为"边缘规则".互联网应用场景的日益多样化,简单.方便.快速的根据不同应用场景实现不同的功能变得越来越 ...