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. [javascript][翻译]使用javascript添加css rule

    来杭一周,收获很多,成长很多. 周六在搞一个插件的时候碰到需要动态添加伪元素的需求,搜了一下解决方案,有人用正则写出了读取伪元素的函数:我觉得倒是可以通过注入css rule的方式,来让预留有某些类的 ...

  2. WPF里面多线程访问UI线程、主线程的控件

    如果出现以下错误:调用线程无法访问此对象,因为另一个线程拥有该对象. 你就碰到多线程访问UI线程.主线程的控件的问题了. 先占位.

  3. 【总结】 BZOJ前100题总结

    前言 最近发现自己trl,所以要多做题目但是Tham布置的题目一道都不会,只能来写BZOJ HA(蛤)OI 1041 复数可以分解成两个点,所以直接把\(R^2\)质因数分解一下就可以了,注意计算每一 ...

  4. iOS 应用隐藏状态栏

    有时候在 Storyboard 和 target 里的设定并不足以使得应用能够完全隐藏状态栏. 这时候需要到 Info.plist,添加一项:View controller-based status ...

  5. “全栈2019”Java多线程第四章:设置和获取线程名称

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

  6. blueborne漏洞的联想

    本文作者:ice 0X00前言 昨天看到blueborne的漏洞,顺手给我的nexus6装了一个app,测试了一下,一脸懵逼,怎么修复啊,然后我联想了一下, 还有哪些协议和传输是我们身边的威胁了,于是 ...

  7. 表格Table宽度设置无效的解决方法

    表格Table宽度设置无效的解决方法 bootstrap中使用table时发现不管用width赋值方式都无法改变table>td的宽度 解决方法: 设置table:table-layout:fi ...

  8. php性能优化三(PHP语言本身)

    0.用单引号代替双引号来包含字符串,这样做会更快一些.因为PHP会在双引号包围的字符串中搜寻变量,单引号则不会,注意:只有echo能这么做,它是一种可以把多个字符串当作参数的“函数”(译注:PHP手册 ...

  9. XorPay 个人支付平台增加 个人支付宝支付接口

      XorPay 今天新增 个人支付宝当面付 接口,欢迎大家使用. 「 XorPay 支付平台」 已经同时支持 个人微信支付接口 和 个人支付宝接口. 个人可用的 支付宝/微信支付 接口,支持 当面付 ...

  10. iOS9下UICollectionViewCell的awakeFromNib问题

    最近项目测试出一个隐藏已久的bug,经过多番测试,发现在iOS9下自定义的一个UICollectionViewCell只走一次awakeFromNib. 具体情况是,项目中有一个控制器用到了自定义的U ...