1. #include<stdio.h>
  2. #include<string.h>
  3. int father[105],depth[105];
  4. int dist[105],map[101][101];
  5. int vis[105],n;
  6. void init_B()
  7. {
  8. int i;
  9. for(i = 1;i <= n;i ++)
  10. {
  11. father[i] = i;
  12. depth[i] = 0;
  13. }
  14. }
  15.  
  16. int find(int x)
  17. {
  18. if(x == father[x])
  19. return x;
  20. return father[x] = find(father[x]);
  21. }
  22.  
  23. void unit(int x,int y)
  24. {
  25. x = find(x);
  26. y = find(y);
  27. if(x == y)
  28. return ;
  29. if(depth[x] < depth[y])
  30. father[x] = y;
  31. else
  32. {
  33. if(depth[x] > depth[y])
  34. father[y] = x;
  35. else
  36. {
  37. father[x] = y;
  38. depth[y]++;
  39. }
  40. }
  41. }
  42.  
  43. void init()
  44. {
  45. int i;
  46. memset(vis,0,sizeof(vis));
  47. for(i = 1;i <= n;i ++)
  48. dist[i] = map[1][i];
  49. }
  50.  
  51. int main()
  52. {
  53. int m,i,j,k,a,b;
  54. int min,cnt,cost,sum;
  55. while(~scanf("%d%d",&m,&n) && m)
  56. {
  57. init_B();
  58. sum = cnt = 0;
  59. for(i = 1;i <= n;i ++)
  60. {
  61. for(j = 1;j <= n;j ++)
  62. {
  63. if(i != j)
  64. {
  65. map[i][j] = 1 << 30;
  66. }
  67. }
  68. }
  69. while(m--)
  70. {
  71. scanf("%d%d%d",&a,&b,&cost);
  72. map[a][b] = map[b][a] = cost;
  73. unit(a,b);
  74. }
  75. init();
  76. for(i = 1;i <= n;i ++)
  77. {
  78. if(i == find(i))
  79. cnt++;
  80. if(cnt == 2)
  81. break ;
  82. }
  83. if(cnt == 2)
  84. {
  85. printf("?\n");
  86. continue ;
  87. }
  88. for(i = 0;i < n;i ++)
  89. {
  90. min = 1 << 30;
  91. for(j = 1;j <= n;j ++)
  92. {
  93. if(!vis[j] && min > dist[j])
  94. {
  95. min = dist[j];
  96. k = j;
  97. }
  98. }
  99. vis[k] = 1;
  100. if(min != 1 << 30)
  101. sum += min;
  102. for(j = 1;j <= n;j ++)
  103. {
  104. if(!vis[j] && dist[j] > map[k][j])
  105. dist[j] = map[k][j];
  106. }
  107. }
  108. printf("%d\n",sum);
  109. }
  110. return 0;
  111. }

HDOJ 1863的更多相关文章

  1. Kruskal HDOJ 1863 畅通工程

    题目传送门 /* 此题为:HDOJ 1233 + HDOJ 1232 */ #include <cstdio> #include <algorithm> #include &l ...

  2. 最小生成树(HDOJ 1863)

    畅通工程 http://acm.hdu.edu.cn/showproblem.php?pid=1863 1.Prim算法: Prim算法是由一个点(最初的集合)向外延伸,找到与集合相连权值最小的边, ...

  3. HDOJ 1863 prim算法 HDOJ 1879

    #include<cstdio> #include<cstring> #define inf 0xffffff ][]; int ans; void prim(int n) { ...

  4. hdoj 1863 畅通工程 最小生成树---prime算法

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=1863 注意有可能出现无法生成树的情况. #include <iostream> #inclu ...

  5. hdoj 1863 畅通工程

    并查集+最小生成树 畅通工程 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  6. MST最小生成树

    首先,贴上一个很好的讲解贴: http://www.wutianqi.com/?p=3012 HDOJ 1233 还是畅通工程 http://acm.hdu.edu.cn/showproblem.ph ...

  7. 杭电hdoj题目分类

    HDOJ 题目分类 //分类不是绝对的 //"*" 表示好题,需要多次回味 //"?"表示结论是正确的,但还停留在模块阶 段,需要理解,证明. //简单题看到就 ...

  8. HDOJ 题目分类

    HDOJ 题目分类 /* * 一:简单题 */ 1000:    入门用:1001:    用高斯求和公式要防溢出1004:1012:1013:    对9取余好了1017:1021:1027:   ...

  9. HDOJ 1009. Fat Mouse' Trade 贪心 结构体排序

    FatMouse' Trade Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) ...

随机推荐

  1. A Case Study -- Performance Evaluation of a DRAM-Based Solid State Disk

    研究将固态硬盘作为持久存储层和传统硬盘的在数据库性能上的研究

  2. tomcat 192.168.1.110?不烦吗?

    最近做一个在线播放器,因为要用到网络服务器做在线播放,又不想直接在本地用tomcat做实验,因为没有真实感. so,手边两台电脑,同时连在局域网. 客户端,笔记本,ip1:192.168.1.101 ...

  3. svn 相关

    // svn相关内容,windows下的可以根据网上的,安装客户端和服务器端安装成功后,可以在服务器端中的 Repositories中建立相关的项目库文件夹,右键相应的文件夹可以复制相关的 url,一 ...

  4. rman全备份异机恢复

    一.测试环境 [oracle@localhost ~]$ uname -a Linux localhost.localdomain -.el6.x86_64 # SMP Tue May :: EDT ...

  5. emctl start dbconsole OC4J_dbconsole*** not found

    C:\windows\system32>emctl start dbconsole OC4J Configuration issue. D:\app\product\\db_1/oc4j/j2e ...

  6. Node.js和MongoDB - MongoJS入门

    第一次尝试翻译外国牛人的博文,希望大家喜欢. 本文源码详见:https://github.com/njaulj/mongojs 一点都不夸大的说,近年来node.js和mongodb的确是大放异彩,在 ...

  7. Centos 7 通过YUM安装 PHP7 NGINX1.1.8 POSTGRESQL9.5

    转载 1.最小化安装CENTOS7 2.更新源: yum update reboot 3.安装扩展源: yum install epel-release 4.安装工具软件: yum install w ...

  8. python获取系统时间

    import time print time.time() 输出的结果是: 1279578704.6725271 但是这样是一连串的数字不是我们想要的结果,我们可以利用time模块的格式化时间的方法来 ...

  9. 【applicationContext.xml】配置文件找不到

    配置文件找不到时可以手动配置一下此处 信息: Initializing Spring root WebApplicationContext 八月 12, 2015 5:36:45 下午 org.apa ...

  10. js中的ajax的运用

    XMLHttpRequest对象 IE7+,FireFox,Chrome,Opera,Safari创建XHR对象: var xhr=new XMLHttpRequest(); 创建XHR对象的兼容性写 ...