HDU 1102 Constructing Roads(kruskal)
Constructing Roads
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
解题思路:
本题有多组测试数据,每种数据第一行给出村子的数量n,跟随n行,每行为使该行对应的村子与其他村子联通所需要修筑的道路距离(其实就是所有村子的邻接矩阵),之后给出已经修好的道路数量q,之后q行跟随,每行包括两个整数分别为道路两端的两个村子。要求输出使所有村子联通还要修筑道路的最小长度。
若不看已经修好的道路,本题就是一个最小生成树问题。在这里使用kruskal算法。
kruskal算法核心思想:
既然已经给出了邻接矩阵,那我们可以将其拆分为邻接表,即将每一条可以修筑的道路都记录下来。初始视所有结点都为不连通,之后将道路按长度排序,从小到大枚举所有边,判断边的两个顶点是否已经连通,若已经连通不做处理,若不连通则将该边记录入最小生成树,并记录当前总权值,最小生成树也是树,符合边数等于顶点数减一,所以结束条件为边数等于定点数减一,如果边数不等于顶点数减一则说明图不连通(当然在这里不存在不连通的情况,不过写上一定不会错,还能节约时间)。
将邻接矩阵拆分为邻接表:用结构edge保存道路,其成员包括两个顶点村子node1,node2与道路长度len。
for(int i = ; i < n; i++){
for(int j = ; j < n; j++){
Edge[cnt].node1 = i;
Edge[cnt].node2 = j;
scanf("%d", &Edge[cnt].len);
cnt++;
}
}
拆分邻接矩阵
在判断是否连通使用并查集
int father[maxn]; //记录父结点
int getFather(int x){
int tempx = x;
while(x != father[x]){ //寻找父结点
x = father[x];
}
while(tempx != father[tempx]){ //将路径上所有的点的father值改为父结点
int preTempx = tempx;
tempx = father[tempx];
father[preTempx] = tempx;
}
return x;
}
并查集
kruskal算法
int kruskal(int n, int m){ //传入顶点数与边数
int ans = , edgeCnt = ;
//ans记录道路长度和,edgeCnt记录当前最小生成树中边的数量
for(int i = ; i < n; i++){
father[i] = i;
}
sort(Edge, Edge + m, cmp); //将边排序
for(int i = ; i < m; i++){ //从小到大枚举所有边
int faNode1 = getFather(Edge[i].node1);
int faNode2 = getFather(Edge[i].node2);
if(faNode1 != faNode2){ //判断该边的两个顶点是否已经连通
father[faNode1] = faNode2; //不连通将其标记为连通
ans += Edge[i].len; //记录长度
edgeCnt++; //记录遍数
if(edgeCnt == n - ) //边数等于顶点数减一
break;
}
}
if(edgeCnt == n - ){ //连通
return ans;
}else{ //不连通
return -;
}
}
kruskal
之后就要考虑已经建好的道路,这其实很简单,只需要将建好的道路长度标记为0即可。给定一个已经建好的道路数量q,之后传入q组数据,每组包含两个村子village1与village2,根据我们邻接矩阵的拆分方法,我们可以得知,在记录边的数组Edge中,village1与village2所对应边的下标为village1 * n + village2(道路是双向的,在Edge中会有两个顶点为village1 与 village2的道路,但因为我们计算时会排序,所以标记一个就好)。
AC代码
#include <bits/stdc++.h>
using namespace std;
const int maxn = 1e4+;
struct edge{
int node1, node2;
int len;
}Edge[maxn];
bool cmp(edge e1, edge e2){
return e1.len < e2.len;
}
int father[maxn]; //记录父结点
int getFather(int x){
int tempx = x;
while(x != father[x]){ //寻找父结点
x = father[x];
}
while(tempx != father[tempx]){ //将路径上所有的点的father值改为父结点
int preTempx = tempx;
tempx = father[tempx];
father[preTempx] = tempx;
}
return x;
} int kruskal(int n, int m){ //传入顶点数与边数
int ans = , edgeCnt = ;
//ans记录道路长度和,edgeCnt记录当前最小生成树中边的数量
for(int i = ; i < n; i++){
father[i] = i;
}
sort(Edge, Edge + m, cmp); //将边排序
for(int i = ; i < m; i++){ //从小到大枚举所有边
int faNode1 = getFather(Edge[i].node1);
int faNode2 = getFather(Edge[i].node2);
if(faNode1 != faNode2){ //判断该边的两个顶点是否已经连通
father[faNode1] = faNode2; //不连通将其标记为连通
ans += Edge[i].len; //记录长度
edgeCnt++; //记录遍数
if(edgeCnt == n - ) //边数等于顶点数减一
break;
}
}
if(edgeCnt == n - ){ //连通
return ans;
}else{ //不连通
return -;
}
}
int main()
{
int n, cnt = ;
while(scanf("%d", &n) != EOF){
cnt = ; //cnt记录边数
int numNode = n, numEdge = ; //numNode记录村子数量,numEdge记录总道路数量
for(int i = ; i < n; i++){ //拆分邻接矩阵
for(int j = ; j < n; j++){
Edge[cnt].node1 = i;
Edge[cnt].node2 = j;
scanf("%d", &Edge[cnt].len);
cnt++;
}
}
numEdge = cnt;
int q;
scanf("%d", &q);
for(int i = ; i < q; i++){
int village1, village2; //输入已经存在道路的两个村子
scanf("%d%d", &village1, &village2);
village1--; //由于之前拆分时 i 与 j从0开始所以村子对应的值为输入的值减一
village2--;
Edge[village1 * n + village2].len = ;
}
int ans = kruskal(numNode, numEdge);
printf("%d\n", ans);
}
return ;
}
HDU 1102 Constructing Roads(kruskal)的更多相关文章
- hdu 1102 Constructing Roads(kruskal || prim)
求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...
- HDU 1102 Constructing Roads, Prim+优先队列
题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...
- HDU 1102(Constructing Roads)(最小生成树之prim算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...
- hdu 1102 Constructing Roads (Prim算法)
题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- hdu 1102 Constructing Roads (最小生成树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...
- HDU 1102 Constructing Roads (最小生成树)
最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...
- hdu 1102 Constructing Roads Kruscal
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...
- HDU 1102 Constructing Roads
Constructing Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- hdu 1102 Constructing Roads(最小生成树 Prim)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...
随机推荐
- Angularjs 通过directive实现验证两次输入是否一致的功能
实现效果: 1> 当输入确认密码时验证: 2> 当输入密码时验证: 实现步骤: 1.页面代码: <input class="form-control" type= ...
- stack和stack frame
首先,我们先来了解下栈帧和栈的基本知识: 栈帧也常被称为“活动记录”(activation record),是编译器用来实现过程/函数调用的一种数据结构. 从逻辑上讲,栈帧就是一个函数执行的环境,包含 ...
- 数据库表结构文档查看器 基于netcore
前言 日常开发业务代码,新接手一块不熟悉的业务时需要频繁的查看对应业务的数据库表设计文档.相比于直接翻看业务代码,有必要提供一个数据库表结构文档查看器来解决这些繁琐的问题. CML.SqlDoc CM ...
- UWP开发入门(七)——下拉刷新
本篇意在给这几天Win10 Mobile负面新闻不断的某软洗地,想要证明实现一个简单的下拉刷新并不困难.UWP开发更大的困难在于懒惰,缺乏学习的意愿.而不是“某软连下拉刷新控件都没有”这样的想法. 之 ...
- 516. Longest Palindromic Subsequence
Given a string s, find the longest palindromic subsequence's length in s. You may assume that the ma ...
- G - Ice_cream's world I (并查集)
点击打开链接 ice_cream's world is a rich country, it has many fertile lands. Today, the queen of ice_cream ...
- 个人常用的win7 快捷键
1.Win + D – 显示桌面 2.Win+L 锁定系统 3.Win + R – 打开运行窗口 4.Win+M 最小化所有窗口 当按下后当前所有窗口全都最小化.再次按下这个组 ...
- [ActionScript 3.0] 记录几个ByteArray 十六进制 String等相互转换的方法
/** * 通过hax数据返回ByteArray * @param hax 格式 "AA5A000100FF" */ private function getHax(hax:Str ...
- Mysql6.0连接中的几个问题 Mysql6.xx
Mysql6.0连接中的几个问题 在最近做一些Javaweb整合时,因为我在maven官网查找的资源,使用的最新版,6.0.3,发现MySQL连接中的几个问题,总结如下: 1.Loading clas ...
- C++实现二叉树的相应操作
1. 二叉树的遍历:先序(递归.非递归),中序(递归.非递归),后序(递归.非递归). #include <iostream> #include <string> #inclu ...