题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102

题目描述:

Problem Description
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
 Simple Output
179
  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5.  
  6. struct node
  7. {
  8. int u,v,cost;
  9. }a[];
  10. int pre[];
  11. int fin(int x)
  12. {
  13. if(x==pre[x])
  14. {
  15. return x;
  16. }
  17. else
  18. {
  19. return pre[x]=fin(pre[x]);
  20. }
  21. }
  22.  
  23. void join(int x,int y)
  24. {
  25. int t1=fin(x);
  26. int t2=fin(y);
  27. if(t1!=t2)
  28. {
  29. pre[t1]=t2;
  30. }
  31. }
  32.  
  33. bool cmp(node x,node y)
  34. {
  35. return x.cost<y.cost;
  36. }
  37.  
  38. int main()
  39. {
  40. int n;
  41. while(~scanf("%d",&n))
  42. {
  43. for(int i=;i<=n;i++)
  44. {
  45. pre[i]=i;
  46. }
  47. int num,cnt=;;
  48. for(int i=;i<=n;i++)
  49. {
  50. for(int j=;j<=n;j++)
  51. {
  52. scanf("%d",&num);
  53. a[cnt].u=i;
  54. a[cnt].v=j;
  55. a[cnt].cost=num;
  56. cnt++;
  57. }
  58. }
  59. sort(a,a+cnt,cmp);
  60. int sum1=,sum=;//此处算是一个小剪枝吧
  61. int q;
  62. scanf("%d",&q);
  63. int c,d;
  64. for(int i=;i<q;i++)
  65. {
  66. scanf("%d%d",&c,&d);
  67. if(fin(c)!=fin(d))
  68. {
  69. join(c,d);//已经修好路的村庄链接成一个集合
  70. sum1++;
  71. }
  72. }
  73. for(int i=;i<cnt;i++)
  74. {
  75. if(fin(a[i].u)!=fin(a[i].v))
  76. {
  77. join(a[i].u,a[i].v);
  78. sum+=a[i].cost;
  79. sum1++;
  80. }
  81. if(sum1==n-)
  82. {
  83. break;
  84. }
  85. }
  86. printf("%d\n",sum);
  87. }
  88. return ;
  89. }

Constructing Roads-最小生成树(kruskal)的更多相关文章

  1. hdu Constructing Roads (最小生成树)

    题目:http://acm.hdu.edu.cn/showproblem.php?pid=1102 /************************************************* ...

  2. POJ 2421 Constructing Roads (最小生成树)

    Constructing Roads Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u ...

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

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

  4. HDU1102 Constructing Roads —— 最小生成树

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1102 题解: 纯最小生成树,只是有些边已经确定了要加入生成树中,特殊处理一下这些边就可以了. krus ...

  5. POJ - 2421 Constructing Roads (最小生成树)

    There are N villages, which are numbered from 1 to N, and you should build some roads such that ever ...

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

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

  7. (step6.1.4)hdu 1102(Constructing Roads——最小生成树)

    题目大意:输入一个整数n,表示村庄的数目.在接下来的n行中,每行有n列,表示村庄i到村庄 j 的距离.(下面会结合样例说明).接着,输入一个整数q,表示已经有q条路修好. 在接下来的q行中,会给出修好 ...

  8. POJ2421 Constructing Roads 最小生成树

    修路 时限: 2000MS   内存限制: 65536K 提交总数: 31810   接受: 14215 描述 有N个村庄,编号从1到N,您应该修建一些道路,使每两个村庄可以相互连接.我们说两个村庄A ...

  9. HDU 1102 Constructing Roads(最小生成树,基础题)

    注意标号要减一才为下标,还有已建设的路长可置为0 题目 #define _CRT_SECURE_NO_WARNINGS #include <stdio.h> #include<str ...

  10. HDU1102(最小生成树Kruskal)

    开学第三周.........真快尼 没有计划的生活真的会误入歧途anytime 表示不开心不开心不开心 每天都觉得自己的生活很忙 又觉得想做的事又没有完成 这学期本来计划重点好好学算法,打码码,臭臭美 ...

随机推荐

  1. jemter 新增sha256函数

    1  https://blog.csdn.net/y100100/article/details/80701049 2  https://blog.csdn.net/y100100/article/d ...

  2. swift 学习- 16 -- 构造过程 02

    // 类的继承 和 构造过程 // 类里面的所有的存储型属性 -- 包括所有继承自父类的属性 -- 都必须在构造过程中设置初始值 // Swift  为类类型提供了 两种构造器来确保实例中所有的存储属 ...

  3. STM32L476应用开发之二:模拟量数据采集

    采集模拟量数据在一台一起中是必不可少的功能.在本次实验中我们要采集的模拟量值主要包括氧气传感器的输出以及压力变送器的输出. 1硬件设计 我们需要采集数据对精度有一定的要求,而STM32L476自带AD ...

  4. Confluence 6 针对大数据量备份

    XML 站点备份的方式只针对 Confluence 包含有几千页面的情况,XML 备份所需要的时间随着数据量的变化而增加.另外的一个问题是 XML 站点的备份将会包含上 G 的附件数据,随着数据量的增 ...

  5. Confluence 6 复杂授权或性能问题

    提交一个 服务器请求(support request) 然后在你的服务请求中同时提供下面的信息. Confluence 服务器 登录 Confluence 然后访问管理员控制台. 将 系统信息(Sys ...

  6. vue.js的计算机属性学习

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. centos忘记密码

    1.启动时按上下箭头,然后按e进入进入编辑模式 2.上下箭头切换在选择 linux ...这行在末尾输入 LANG=en_US.UTF-8 init=/bin/sh 然后按 ctrl+x 进行引导 3 ...

  8. Kali linux Nessus &Cracking Password

    1 .Nessus漏洞网站测试(真正体会到什么是专业版和社区版的区别,要技术就不光要勤恳的态度,严谨的思维.还有矢志不渝的志气,还必须要求砸钱,所以狠狠的赚钱才是硬道理),我的半个社区版的很多扫描模块 ...

  9. 兼容IE浏览器样式的html上传文件控件

    最近在公司做项目时需要用到html的上传文件控件,但发现原生的上传文件控件<input type="file" />在IE.Chrome浏览器的显示效果相差很大,为了统 ...

  10. 第二周学习总结-Java

    2018年7月22日 暑假第二周马上就要结束了,这一周我继续学习了java. 本周学到了一些Java的修饰词,比如static.private.public等,这些修饰词用法与c++类似,很容易掌握. ...