1. POJ 最小生成树模板
  2. Kruskal算法
  3.  
  4. #include<iostream>
  5. #include<algorithm>
  6. #include<stdio.h>
  7. #include<string.h>
  8. #include<ctype.h>
  9. #include<stdlib.h>
  10. #include<limits.h>
  11. #include<math.h>
  12. #include<queue>
  13. #include<stack>
  14. #define max(a, b) a>b?a:b;
  15. #define min(a, b) a<b?a:b;
  16. using namespace std;
  17. const int N = ;
  18.  
  19. typedef struct rode
  20. {
  21. int x, y, l;
  22. } RODE;
  23. RODE maps[N];
  24. int f[N];
  25.  
  26. int cmp(const void *a, const void *b)
  27. {
  28. RODE *c, *d;
  29. c=(RODE *)a;
  30. d=(RODE *)b;
  31. return c -> l - d -> l;
  32. }
  33.  
  34. void Kruskal(int k);
  35. int Find(int x);
  36.  
  37. int main()
  38. {
  39. int n, i, j, k, a;
  40. while(scanf("%d", &n)!=EOF)
  41. {
  42. k=;
  43. for(i=; i<n; i++)
  44. {
  45. f[i]=i;
  46. }
  47. for(i=; i<n; i++)
  48. for(j=; j<n; j++)
  49. {
  50. scanf("%d", &a);
  51. maps[k].x=i;
  52. maps[k].y=j;
  53. maps[k].l=a;
  54. k++;
  55. }
  56. qsort(maps, k, sizeof(maps[]), cmp);
  57. Kruskal(k);
  58. }
  59. return ;
  60. }
  61. void Kruskal(int k)
  62. {
  63. int ans=;
  64. for(int i=; i<k; i++)
  65. {
  66. int ru=Find(maps[i].x);
  67. int rv=Find(maps[i].y);
  68. if(ru!=rv)
  69. {
  70. f[ru]=rv;
  71. ans+=maps[i].l;
  72. }
  73. }
  74. printf("%d\n", ans);
  75. }
  76. int Find(int x)
  77. {
  78. if(f[x]!=x)
  79. f[x]=Find(f[x]);
  80. return f[x];
  81. }
  82.  
  83. 另一种 prim算法 dijkstra算法很像
  84.  
  85. #include <iostream>
  86. #include <cstring>
  87. #include <cstdio>
  88. #include <algorithm>
  89.  
  90. using namespace std;
  91.  
  92. int Map[][];
  93. const int oo=0xfffffff;
  94. int visit[];
  95. int dist[];
  96. int prim(int s,int n)
  97. {
  98. int ans=;
  99. memset(visit,,sizeof(visit));
  100. for(int i=; i<=n; i++)
  101. dist[i]=Map[s][i];
  102. visit[s]=;
  103. for(int i=; i<n; i++)
  104. {
  105. int index;
  106. int Min=oo;
  107. for(int j=; j<=n; j++)
  108. {
  109. if(!visit[j]&&dist[j]<Min)
  110. {
  111. index=j;
  112. Min=dist[j];
  113. }
  114. }
  115. ans+=Min;
  116. visit[index]=;
  117. for(int j=; j<=n; j++)
  118. {
  119. if(!visit[j]&&dist[j]>Map[index][j])
  120. {
  121. dist[j]=Map[index][j];
  122. }
  123. }
  124.  
  125. }
  126. return ans;
  127. }
  128. int main()
  129. {
  130. int n;
  131. while(scanf("%d",&n)!=EOF)
  132. {
  133. for(int i=; i<=n; i++)
  134. {
  135. for(int j=; j<=n; j++)
  136. {
  137. Map[i][j]=oo;
  138. }
  139. }
  140.  
  141. for(int i=; i<=n; i++)
  142. {
  143. for(int j=; j<=n; j++)
  144. {
  145. scanf("%d",&Map[i][j]);
  146. }
  147. }
  148. printf("%d\n",prim(,n));
  149. }
  150. return ;
  151. }

poj 1258 最小生成树 模板的更多相关文章

  1. POJ 1258 最小生成树

    23333333333 完全是道水题.因为是偶自己读懂自己做出来的..T_T.prim的模板题水过. DESCRIPTION:John竞选的时候许诺会给村子连网.现在给你任意两个村子之间的距离.让你求 ...

  2. poj 1251 poj 1258 hdu 1863 poj 1287 poj 2421 hdu 1233 最小生成树模板题

    poj 1251  && hdu 1301 Sample Input 9 //n 结点数A 2 B 12 I 25B 3 C 10 H 40 I 8C 2 D 18 G 55D 1 E ...

  3. 最小生成树 10.1.5.253 1505 poj 1258 http://poj.org/problem?id=1258

    #include <iostream>// poj 1258 10.1.5.253 1505 using namespace std; #define N 105 // 顶点的最大个数 ( ...

  4. POJ 1258 + POJ 1287 【最小生成树裸题/矩阵建图】

    Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet c ...

  5. Poj 2187 凸包模板求解

    Poj 2187 凸包模板求解 传送门 由于整个点数是50000,而求凸包后的点也不会很多,因此直接套凸包之后两重循环即可求解 #include <queue> #include < ...

  6. POJ 1258 Agri-Net|| POJ 2485 Highways MST

    POJ 1258 Agri-Net http://poj.org/problem?id=1258 水题. 题目就是让你求MST,连矩阵都给你了. prim版 #include<cstdio> ...

  7. POJ-图论-最小生成树模板

    POJ-图论-最小生成树模板 Kruskal算法 1.初始时所有结点属于孤立的集合. 2.按照边权递增顺序遍历所有的边,若遍历到的边两个顶点仍分属不同的集合(该边即为连通这两个集合的边中权值最小的那条 ...

  8. POJ 1258:Agri-Net Prim最小生成树模板题

    Agri-Net Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 45050   Accepted: 18479 Descri ...

  9. POJ 1258 Agri-Net(最小生成树,模板题)

    用的是prim算法. 我用vector数组,每次求最小的dis时,不需要遍历所有的点,只需要遍历之前加入到vector数组中的点(即dis[v]!=INF的点).但其实时间也差不多,和遍历所有的点的方 ...

随机推荐

  1. BNU 34990 Justice String (hash+二分求LCP)

    思路:枚举第一个字符串的位置,然后枚举最长公共前缀的长度,时间即会下降-- #pragma comment(linker, "/STACK:1024000000,1024000000&quo ...

  2. IAR模板--怎样在IARproject中创建和使用模板

    怎样在IARproject中创建和使用模板 1.编辑和使用模板的方式: 路径为:Edit -> Code Templates -> Edit Templates  例如以下图: water ...

  3. Unity动态字体在手机上出现字体丢失问题解决

    在我们游戏的开发过程中,在部分手机上运行游戏的时候,出现了字体丢失的问题,出问题的手机似乎用的都是高通芯片. 使用的unity是4.2.0版本,ngui是3.4.9版本. 在unity的论坛及unit ...

  4. ifconfig 命令

    许多windows非常熟悉ipconfig命令行工具,它被用来获取网络接口配置信息并对此进行修改.Linux系统拥有一个类似的工具,也就是ifconfig(interfaces config).通常需 ...

  5. cobbler+koan

    cobbler+koan自动重装客户机   koan是kickstart-over-a-network的缩写,它是cobbler的客户端帮助程序,koan允许你通过网络提供虚拟机,也允许你重装已经存在 ...

  6. Android Studio 2.3版本 Run项目不能自动启动APP的问题 (转)

    参考: http://blog.csdn.net/lucasey/article/details/61071377 Android Studio 升级到2.3版本后 运行项目后,只是安装上了,而APP ...

  7. Spring Cloud 微服务四:熔断器Spring cloud hystrix

    前言:在微服务架构中,一般都是进程间通信,有可能调用链都比较长,当有底层某服务出现问题时,比如宕机,会导致调用方的服务失败,这样就会发生一连串的反映,造成系统资源被阻塞,最终可能造成雪崩.在sprin ...

  8. Lumen开发:lumen源码解读之初始化(4)——服务提供(ServiceProviders)与路由(Routes)

    版权声明:本文为博主原创文章,未经博主允许不得转载. 前面讲了singleton和Middleware,现在来继续讲ServiceProviders和Routes,还是看起始文件bootstrap/a ...

  9. android shape的用法总结

    参考代码: <shape xmlns:android="http://schemas.android.com/apk/res/android" > <corner ...

  10. ASIHTTP 框架,同步、 异步请求、 上传 、 下载

    ASIHTTPRequest详解 ASIHTTPRequest 是一款极其强劲的 HTTP 访问开源项目.让简单的 API 完成复杂的功能,如:异步请求,队列请求,GZIP 压缩,缓存,断点续传,进度 ...