1. #include <iostream>
  2. #include <cmath>
  3. #include <cstring>
  4. #include <cstdlib>
  5. #include <cstdio>
  6. #include <algorithm>
  7. #include <vector>
  8. #include <queue>
  9. using namespace std;
  10. #define INF 0xfffffff
  11. #define maxn 103
  12.  
  13. struct Edge
  14. {
  15. int e, w;
  16. Edge(int e=,int w=): e(e), w(w) {}
  17. friend bool operator < (Edge A, Edge B)
  18. {
  19. return A.w < B.w;
  20. }
  21. };
  22.  
  23. vector<Edge> G[maxn];
  24. int dist[maxn];
  25. bool vis[maxn];
  26. int m, n;
  27. int Prime()
  28. {
  29. Edge P, Pn;
  30. int Nnode = ;
  31. dist[] = ;
  32. priority_queue < Edge > Q;
  33. Q.push( Edge(,) );
  34.  
  35. while( !Q.empty() && Nnode < n )
  36. {
  37. while( !Q.empty() )
  38. {
  39. P = Q.top();
  40. Q.pop();
  41. if( !vis[P.e] )
  42. break;
  43. }
  44. if( vis[P.e] )
  45. continue;
  46. Nnode ++;
  47. vis[P.e] = true;
  48. int len = G[P.e].size();
  49.  
  50. for(int i=; i<len; i++)
  51. {
  52. Pn = G[P.e][i];
  53.  
  54. if(!vis[Pn.e] && dist[Pn.e] > Pn.w)
  55. {
  56. dist[Pn.e] = Pn.w;
  57. Q.push(Pn);
  58. }
  59. }
  60. }
  61. if(Nnode < n)
  62. return -;
  63.  
  64. int sum = ;
  65. for(int i=; i<=n; i++)
  66. {
  67. sum += dist[i];
  68. }
  69.  
  70. return sum;
  71. }
  72.  
  73. void Init()
  74. {
  75. for(int i=; i<=n; i++)
  76. {
  77. G[i].clear();
  78. dist[i] = INF;
  79. vis[i] = false;
  80. }
  81. }
  82.  
  83. int main()
  84. {
  85. while(cin >> m >> n, m)
  86. {
  87. Init();
  88. for(int i=; i<m; i++)
  89. {
  90. int a, b, c;
  91. cin >> a >> b >> c;
  92.  
  93. G[a].push_back( Edge(b,c) );
  94. G[b].push_back( Edge(a,c) );
  95.  
  96. }
  97.  
  98. int ans = Prime();
  99.  
  100. if(ans == -)
  101. cout << "?" << endl;
  102. else
  103. cout << ans << endl;
  104.  
  105. }
  106. return ;
  107. }

Prime邻接表+优先队列的更多相关文章

  1. HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...

  2. poj3013 邻接表+优先队列+Dij

    把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...

  3. NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)

    Problem 1037: Wormhole Time Limits:  5000 MS   Memory Limits:  200000 KB 64-bit interger IO format: ...

  4. Genealogical tree(拓扑结构+邻接表+优先队列)

    Genealogical tree Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) ...

  5. HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  6. 基于STL优先队列和邻接表的dijkstra算法

    首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...

  7. 06-图1 列出连通集 (25分)(C语言邻接表实现)

    题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495 由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循 ...

  8. SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历

    http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...

  9. 图论——最小生成树prim+邻接表+堆优化

    今天学长对比了最小生成树最快速的求法不管是稠密图还是稀疏图,prim+邻接表+堆优化都能得到一个很不错的速度,所以参考学长的代码打出了下列代码,make_pair还不是很会,大体理解的意思是可以同时绑 ...

随机推荐

  1. std::bad_alloc

    一个服务器程序,长时间运行报错了. ----------- terminate called after throwing an instance of 'std::bad_alloc' what() ...

  2. u盘安装linux(windows7+linux双系统)

    前提条件:1.先装windows7,后装linux系统 2.windows7 里有“未分区的空间”(不是C:,D:,E:) :计算机→管理→存储空间,删除一些压缩卷即可. 3.下载ultraiso → ...

  3. android anim 动画效果(转)

    动画效果编程基础--AnimationAndroid      动画类型      Android的animation由四种类型组成      XML中    alpha    渐变透明度动画效果   ...

  4. VB编程技巧推荐

    VB编程技巧推荐   1.zyl910的专栏——理论水平高 用VB写高效的图像处理程序 V2.0 优化分支代码——避免跳转指令堵塞流水线 2.Laviewpbt的专栏 —— 有很多算法的代码,实用性高 ...

  5. jquery获取元素到页面顶部距离

    jquery获取元素到页面顶部距离的语句为: $(selector).offset().top

  6. 基于CANVAS与MD5的客户端生成验证码

    好久没写东西,工作太忙了!不想服务端请求太多,搞了个这玩意儿,不过项目中并不会用上,还是使用服务端生成的机制(会安全多少呢?):我就想问个问题,除了图像识别来破解这样的简单验证码外,针对我这种例子,可 ...

  7. html.day01

    1.web标准: 1. 结构  (xhtml)  2. 表现(css)  3.行为(js) html   超文本标记语言 xhtml  (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...

  8. C#比较dynamic和Dictionary性能

    开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary<string,object>).dynamic 的编码体验显著优于 Diction ...

  9. sql 列轉行、行轉列

    PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...

  10. Swift - 03 - 整数类型

    //: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...