Constructing Roads

  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

解题思路:
  本题有多组测试数据,每种数据第一行给出村子的数量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)的更多相关文章

  1. hdu 1102 Constructing Roads(kruskal || prim)

    求最小生成树.有一点点的变化,就是有的边已经给出来了.所以,最小生成树里面必须有这些边,kruskal和prim算法都能够,prim更简单一些.有一点须要注意,用克鲁斯卡尔算法的时候须要将已经存在的边 ...

  2. HDU 1102 Constructing Roads, Prim+优先队列

    题目链接:HDU 1102 Constructing Roads Constructing Roads Problem Description There are N villages, which ...

  3. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  4. hdu 1102 Constructing Roads (Prim算法)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  5. hdu 1102 Constructing Roads (最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Jav ...

  6. HDU 1102 Constructing Roads (最小生成树)

    最小生成树模板(嗯……在kuangbin模板里面抄的……) 最小生成树(prim) /** Prim求MST * 耗费矩阵cost[][],标号从0开始,0~n-1 * 返回最小生成树的权值,返回-1 ...

  7. hdu 1102 Constructing Roads Kruscal

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题意:这道题实际上和hdu 1242 Rescue 非常相似,改变了输入方式之后, 本题实际上更 ...

  8. HDU 1102 Constructing Roads

    Constructing Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Other ...

  9. hdu 1102 Constructing Roads(最小生成树 Prim)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 Problem Description There are N villages, which ...

随机推荐

  1. s21day25 python笔记

    s21day25 python笔记 正则表达式 1.定义 定义:正则表达式是一种规则匹配字符串的规则 re模块本身只是用来操作正则表达式的,和正则本身没关系 为什么要有正则表达式? 匹配字符串 一个人 ...

  2. PostgreSQL 表空间

    PostgreSQL 表空间 一 介绍使用表空间可以将不同的表放到不同的存储介质或不同的文件系统下,实际上是为表指定一个存储的目录.创建数据库,表,索引时可以指定表空间,将数据库,表,索引放到指定的目 ...

  3. Ubuntu 12.04 安装最新版本NodeJS

    昨天搭建了一个Windows NodeJS 运行环境,但Windows 运行NodeJS命令行各种别扭,开源包的编译也是各种问题,折磨了我一天一夜,果断换到Linux 平台.. 我选择了Ubuntu ...

  4. C#基础之流程控制语句详解

    C#程序的执行都是一行接一行.自上而下地进行,不遗漏任何代码.为了让程序能按照开发者所设计的流程进行执行,必然需要进行条件判断.循环和跳转等过程,这就需要实现流程控制.C#中的流程控制包含了条件语句. ...

  5. “全栈2019”Java第八十九章:接口中能定义内部类吗?

    难度 初级 学习时间 10分钟 适合人群 零基础 开发语言 Java 开发环境 JDK v11 IntelliJ IDEA v2018.3 文章原文链接 "全栈2019"Java第 ...

  6. 洛谷P5280 [ZJOI2019]线段树(线段树)

    题面 传送门 题解 考场上就这么一道会做的其它连暴力都没打--活该爆炸-- 首先我们得看出问题的本质:有\(m\)个操作,总共\(2^m\)种情况分别对应每个操作是否执行,求这\(2^m\)棵线段树上 ...

  7. BZOJ 1922--大陆争霸(最短路)

    1922: [Sdoi2010]大陆争霸 Time Limit: 10 Sec  Memory Limit: 64 MBSubmit: 2113  Solved: 947[Submit][Status ...

  8. HTML Strip Char Filter

    The html_strip character filter strips HTML elements from the text and replaces HTML entities with t ...

  9. Win10将用户名修改为英文

    前言 最近重装了一次Win10系统,手贱地在引导里设置了中文的用户名.使用微软账户进行登录后,Win10以这个中文名建立了用户的文件夹,使得少数软件安装或使用过程中,保存路径不支持中文路径,从而报错, ...

  10. C#-WebForm-AJAX阿贾克斯(二)★★★★★ajax的完整结构★★★★★

    ajax完整结构: $.ajax({ url:"",//服务器路径 data:{},//给服务端传递的参数,可以没有,也可以是多个 type:"post", / ...