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. 原创:MVC 5 实例教程(MvcMovieStore 新概念版:mvc5.0,EF6.01) - 4、创建数据上下文和数据实体模型

    说明:MvcMovieStore项目已经发布上线,想了解最新版本功能请登录 MVC影视(MvcMovie.cn) 进行查阅.如需转载,请注明出处:http://www.cnblogs.com/Dodu ...

  2. .net core执行dotnet ef migrations createmodel等命令出错

    .net core执行dotnet ef migrations createmodel等命令出错 执行dotnet ef migrations createmodel.dotnet ef migrat ...

  3. C#基础笔记(第二十天)

    1.复习属性:保护字段的构造函数:初始化对象初始化对象:给对象的每个属性去赋值什么时候会调用构造函数:当我们new的时候面向对象中需要注意的两个关键字this 1.代表当前类的对象 2.调用自己的构造 ...

  4. 遇到问题-----cas4.2.x登录成功后报错No principal was found---cas中文乱码问题完美解决

    情况 我们之前已经完成了cas4.2.x登录使用MongoDB验证方式并且自定义了加密. 单点登录(十五)-----实战-----cas4.2.x登录mongodb验证方式实现自定义加密 但是悲剧的是 ...

  5. GO学习笔记 - 函数名前面是否有输入参数肯定是不一样的!!

    在刚接触GO语言时候,我相信你也会有这种困惑,为什么有的函数名前面有输入参数,而一些却没有,它们是否有差别?确实有差别,没有输入参数,是一般的函数:有输入参数,是结构的方法,输入参数叫做“方法接收者” ...

  6. ISE14.7生成.bit文件和mcs文件

    1.FPGA bit文件加载步骤(加载到FPGA的RAM中,用于在线调试,掉电丢失) 第一步:选择Tools->IMPCAT->选择OK: 第二步:双击Boundary Scan-> ...

  7. rpm -ivh 这个ivh是干什么的

    安装的时候显示安装进度 --从百度知道复制过来的 RMP 是 LINUX 下的一种软件的可执行程序,你只要安装它就可以了.这种软件安装包通常是一个RPM包(Redhat Linux Packet Ma ...

  8. lucene3.0_IndexSearcher排序

    系列汇总: lucene3.0_基础使用及注意事项汇总 IndexSearcher排序 本文主要讲解: 1.IndexSearcher中和排序相关的方法及sort类.SortField类(api级别) ...

  9. Java 自定义注解与注解解析实例

    在学习Java之后会遇到很多的注解,有加载JavaBean的注解:@Component,@Service,@Controller:有获取配置文件中数值的注解@Value:有获取Http请求的数据的注解 ...

  10. python自学之第一章 —— 变量

    1.变量的命名(): (1).可以包含数字.字母.下划线‘_’,但只能以字母和下划线‘_’开头,不能以数字开头! (2).变量的命名不能包含空格. (3).不能将python中的关键字(reserve ...