1. int vis[];
  2. int n;
  3. int map[][];
  4. int d[];
  5. int prim(){
  6. int i,j,mi,v;
  7. for(i=;i<n;i++){
  8. d[i]=map[][i];
  9. vis[i]=;
  10. }
  11. for(i=;i<=n;i++){
  12. mi=<<;
  13. for(j=;j<n;j++){
  14. if(!vis[j] && mi>d[j]){
  15. v=j;
  16. mi=d[j];
  17. }
  18. }
  19. vis[v]=;
  20. for(j=;j<n;j++)
  21. if(!vis[j] && d[j]>map[v][j])
  22. d[j]=map[v][j];
  23. }
  24. for(i=;i<n;i++) d[]+=d[i];
  25. return d[];
  26. }

heap优化

http://www.nocow.cn/index.php/Prim%E7%AE%97%E6%B3%95

  1. /*
  2. 二叉堆优化Prim算法
  3. Author:YangZX
  4. Date:9.11 2011
  5. */
  6. #include <iostream>
  7. using namespace std;
  8. const int MAXV = , MAXE = , INF = (~0u)>>;
  9. struct edge{
  10. int t, w, next;
  11. }es[MAXE * ];
  12. int h[MAXV], cnt, n, m, heap[MAXV], size, pos[MAXV], dist[MAXV];
  13. void addedge(int x, int y, int z)
  14. {
  15. es[++cnt].t = y;
  16. es[cnt].next = h[x];
  17. es[cnt].w = z;
  18. h[x] = cnt;
  19. }
  20.  
  21. void heapup(int k)
  22. {
  23. while(k > ){
  24. if(dist[heap[k>>]] > dist[heap[k]]){
  25. swap(pos[heap[k>>]], pos[heap[k]]);
  26. swap(heap[k>>], heap[k]);
  27. k>>=;
  28. }else
  29. break;
  30. }
  31. }
  32. void heapdown(int k)
  33. {
  34. while((k<<) <= size){
  35. int j;
  36. if((k<<) == size || dist[heap[(k<<)]] < dist[heap[(k<<)+]])
  37. j = (k<<);
  38. else
  39. j = (k<<) + ;
  40. if(dist[heap[k]] > dist[heap[j]]){
  41. swap(pos[heap[k]], pos[heap[j]]);
  42. swap(heap[k], heap[j]);
  43. k=j;
  44. }else
  45. break;
  46. }
  47. }
  48. void push(int v, int d)
  49. {
  50. dist[v] = d;
  51. heap[++size] = v;
  52. pos[v] = size;
  53. heapup(size);
  54. }
  55. int pop()
  56. {
  57. int ret = heap[];
  58. swap(pos[heap[size]], pos[heap[]]);
  59. swap(heap[size], heap[]);
  60. size--;
  61. heapdown();
  62. return ret;
  63. }
  64.  
  65. int prim()
  66. {
  67. int mst = , i, p;
  68. push(, );
  69. for(i=; i<=n; i++)
  70. push(i, INF);
  71. for(i=; i<=n; i++){
  72. int t = pop();
  73. mst += dist[t];
  74. pos[t] = -;
  75. for(p = h[t]; p; p = es[p].next){
  76. int dst = es[p].t;
  77. if(pos[dst] != - && dist[dst] > es[p].w){
  78. dist[dst] = es[p].w;
  79. heapup(pos[dst]);
  80. heapdown(pos[dst]);
  81. }
  82. }
  83. }
  84. return mst;
  85. }
  86. int main()
  87. {
  88. cin>>n>>m;
  89. for(int i=; i<=m; i++){
  90. int x, y, z;
  91. cin>>x>>y>>z;
  92. addedge(x, y, z);
  93. addedge(y, x, z);
  94. }
  95. cout<<prim()<<endl;
  96. return ;
  97. }

prim模板的更多相关文章

  1. HDU1875+Prim模板

    https://cn.vjudge.net/problem/HDU-1875 相信大家都听说一个“百岛湖”的地方吧,百岛湖的居民生活在不同的小岛中,当他们想去其他的小岛时都要通过划小船来实现.现在政府 ...

  2. HDU 1223 还是畅通工程(最小生成树prim模板)

    一个很简单的prim模板,但虽然是模板,但也是最基础的,也要脱离模板熟练打出来 后期会更新kruskal写法 #include<iostream> #include<cstdio&g ...

  3. Kruskal && Prim模板

    1. Kruskal(并查集模板): /* Kruskal:并查集实现,记录两点和距离,按距离升序排序,O (ElogE) */ struct Edge { int u, v, w; bool ope ...

  4. prim模板题

    题目链接:http://acm.hrbeu.edu.cn/index.php?act=problem&id=1223 #include <cstdio> #include < ...

  5. 最小生成树(kruskal模版 Prim模板)

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2144&cid=1186 最小生成树,最重要的是了解思想 稠密图用Prim,稀疏图用Kru ...

  6. poj 1258 Agri-Net prim模板 prim与dijkstra的区别

    很裸地求最小生成树的题目.题意就不多说了,最重要的就是记录一下学会了prim算法. 初学prim,给我的第一感觉就是和dijkstra好像啊,感觉两者的区别还是有的: 1:prim是求最小生成树的算法 ...

  7. prim 模板

    #include<cstdio> #include<vector> #include<cstring> #include<set> #define ma ...

  8. 最小生成树模板【kruskal & prim】

    CDOJ 1966 Kruskal 解法 时间复杂度O(mlogm) m为边数,这里主要是边排序占时间,后面并查集还好 #include <cstdio> #include <cst ...

  9. 最小生成树(次小生成树)(最小生成树不唯一) 模板:Kruskal算法和 Prim算法

    Kruskal模板:按照边权排序,开始从最小边生成树 #include<algorithm> #include<stdio.h> #include<string.h> ...

随机推荐

  1. 如何得到Sessionid的值

    当用户向一个网站请求第一个页面时,用户会话启动.当第一个页面被请求时,web服务器将asp.net_sessionID  cookie添加进用户的浏览器.可以使用newsession属性探测新会话的启 ...

  2. video标签 拖动 转自w3school

    调整视频大小 播放 暂停 用js实现 详细参见http://www.w3school.com.cn/tiy/t.asp?f=html5_video_dom 图片的拖动详见http://www.w3sc ...

  3. asp.net发布网站(转)

    1.         在Web项目中点击发布网站,如图1所示 图1 2.         选择要发布的路径——>“确定”,如果项目显示发布成功就可以了.如图2所示 图2 3.         打 ...

  4. SqlServer 2015修改表时出现“save changes is not permitted…”的解决方法

    使用SqlServer 2015的过程中,会出现如下情况: 在修改完表字段名或是类型后点击保存时会弹出一个对话框,且无法保存已做的修改.对话框内容大致如下: Saving changes is not ...

  5. [NewCoder]复杂链表的复制

    看下面一个链表结点的定义: struct ComplexListNode { int val; struct ComplexListNode *next; struct ComplexListNode ...

  6. Servlet url-pattern优先级

    完全匹配>目录匹配>扩展名匹配

  7. 转几篇关于Android webView的网文

    1,控件WebView显示网页 http://www.cnblogs.com/tinyphp/p/3858997.html http://blog.csdn.net/t12x3456/article/ ...

  8. linq to entity asp.net mvc 多字段排序

    字段1 降序 字段2 降序 var str = db.xxx.OrderByDescending(p=>p.字段1).ThenByDescending(p=>p.字段2) ThenBy - ...

  9. Qt多线程编程总结(二)——QMutex

    QMutex类提供的是线程之间的访问顺序化. QMutex的目的是保护一个对象.数据结构或者代码段,所以同一时间只有一个线程可以访问它.(在Java术语中,它和同步关键字“synchronized”很 ...

  10. c++ cout 保留小数点位

    需要头文件 <iomanip> 输出时需要用 fixed 和 setprecision() fixed代表输出浮点数,setprecision()设置精度. #include <io ...