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. VS2017新建控制器出现 No executable found matching command: dotnet-asp net-code generator解决办法

    编辑项目.csproj的文件,里面加上如下节点保存即可:  <ItemGroup>    <DotNetCliToolReference Include="Microsof ...

  2. [多线程] Thread

    多线程 概述 单任务处理:一个任务完成后才能进行下一个任务. 多任务处理:CPU分时操作,每个任务看似同时运行. 进程 应用程序的一个运行实例,包含程序所需资源的内存区域,是操作系统进行资源分配的单元 ...

  3. squid代理缓存服务

    man.linuxde.net 1.squid是Linux系统中的代理缓存服务,通常用作WEB网站的前置缓存服务,能够代替用户向网站服务器请求页面数据并进行缓存. 2.squid服务特点:配置简单.效 ...

  4. VS2013如何添加LIb库及头文件的步骤

    在VS工程中,添加c/c++工程中外部头文件及库的基本步骤: 1.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录. 2.添加文件引用 ...

  5. Chrome 中删除单条浏览记录

    悲伤...之前用非隐私窗口观看了小电影.于是打开 chrome://settings/ ...... 现在才知道 windows 上使用 shift + del 即可删除该浏览记录 ....... 以 ...

  6. tf.nn.conv2d()需要搞清楚的几个变量。

    惯例先展示函数: tf.nn.conv2d(input, filter, strides, padding, use_cudnn_on_gpu=None, name=None) 除去name参数用以指 ...

  7. 回归到jquery

    最近在做一个公司的老产品的新功能,使用原来的技术框架,jquery和一堆插件,使用jquery的话,灵活性是有了,但是对于一个工作了3年多的我来说,很low,没什么成就感,技术本身比较简单,但是业务的 ...

  8. leetcode-278-First Bad Version(注意不要上溢)

    题目描述:(说明中有简单翻译) You are a product manager and currently leading a team to develop a new product. Unf ...

  9. postman小结

    1.get和post请求,get有限制2k,post没有限制post安全 在选择的时候别把get post选错然后,run 2.data 选成txt文件  utf-8 ip ip,result12.1 ...

  10. SCOI2019 游记

    写在前面 其实冬令营之后就有一些想说的内容,由于心情原因没有写出来.PKUWC 失误频频,唯一可能还有点价值的就是 Day2T3 计算几何推了 76 分出来.NOIWC 更是无心再谈,感觉是被提答送走 ...