Prime邻接表+优先队列
- #include <iostream>
- #include <cmath>
- #include <cstring>
- #include <cstdlib>
- #include <cstdio>
- #include <algorithm>
- #include <vector>
- #include <queue>
- using namespace std;
- #define INF 0xfffffff
- #define maxn 103
- struct Edge
- {
- int e, w;
- Edge(int e=,int w=): e(e), w(w) {}
- friend bool operator < (Edge A, Edge B)
- {
- return A.w < B.w;
- }
- };
- vector<Edge> G[maxn];
- int dist[maxn];
- bool vis[maxn];
- int m, n;
- int Prime()
- {
- Edge P, Pn;
- int Nnode = ;
- dist[] = ;
- priority_queue < Edge > Q;
- Q.push( Edge(,) );
- while( !Q.empty() && Nnode < n )
- {
- while( !Q.empty() )
- {
- P = Q.top();
- Q.pop();
- if( !vis[P.e] )
- break;
- }
- if( vis[P.e] )
- continue;
- Nnode ++;
- vis[P.e] = true;
- int len = G[P.e].size();
- for(int i=; i<len; i++)
- {
- Pn = G[P.e][i];
- if(!vis[Pn.e] && dist[Pn.e] > Pn.w)
- {
- dist[Pn.e] = Pn.w;
- Q.push(Pn);
- }
- }
- }
- if(Nnode < n)
- return -;
- int sum = ;
- for(int i=; i<=n; i++)
- {
- sum += dist[i];
- }
- return sum;
- }
- void Init()
- {
- for(int i=; i<=n; i++)
- {
- G[i].clear();
- dist[i] = INF;
- vis[i] = false;
- }
- }
- int main()
- {
- while(cin >> m >> n, m)
- {
- Init();
- for(int i=; i<m; i++)
- {
- int a, b, c;
- cin >> a >> b >> c;
- G[a].push_back( Edge(b,c) );
- G[b].push_back( Edge(a,c) );
- }
- int ans = Prime();
- if(ans == -)
- cout << "?" << endl;
- else
- cout << ans << endl;
- }
- return ;
- }
Prime邻接表+优先队列的更多相关文章
- HDU 1535 Invitation Cards(逆向思维+邻接表+优先队列的Dijkstra算法)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1535 Problem Description In the age of television, n ...
- poj3013 邻接表+优先队列+Dij
把我坑到死的题 开始开题以为是全图连通是的最小值 ,以为是最小生成树,然后敲了发现不是,看了下别人的题意,然后懂了: 然后发现数据大,要用邻接表就去学了一下邻接表,然后又去学了下优先队列优化的dij: ...
- NBOJv2——Problem 1037: Wormhole(map邻接表+优先队列SPFA)
Problem 1037: Wormhole Time Limits: 5000 MS Memory Limits: 200000 KB 64-bit interger IO format: ...
- Genealogical tree(拓扑结构+邻接表+优先队列)
Genealogical tree Time Limit : 2000/1000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) ...
- HDU 2544 最短路(邻接表+优先队列+dijstra优化模版)
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 基于STL优先队列和邻接表的dijkstra算法
首先说下STL优先队列的局限性,那就是只提供入队.出队.取得队首元素的值的功能,而dijkstra算法的堆优化需要能够随机访问队列中某个节点(来更新源点节点的最短距离). 看似可以用vector配合m ...
- 06-图1 列出连通集 (25分)(C语言邻接表实现)
题目地址:https://pta.patest.cn/pta/test/558/exam/4/question/9495 由于边数E<(n*(n-1))/2 所以我选用了邻接表实现,优先队列用循 ...
- SDUT2142数据结构实验之图论二:基于邻接表的广度优先搜索遍历
http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=2142&cid=1186 题目描述 给定一个无向连通图,顶点编号从0到n-1,用广度优先搜 ...
- 图论——最小生成树prim+邻接表+堆优化
今天学长对比了最小生成树最快速的求法不管是稠密图还是稀疏图,prim+邻接表+堆优化都能得到一个很不错的速度,所以参考学长的代码打出了下列代码,make_pair还不是很会,大体理解的意思是可以同时绑 ...
随机推荐
- std::bad_alloc
一个服务器程序,长时间运行报错了. ----------- terminate called after throwing an instance of 'std::bad_alloc' what() ...
- u盘安装linux(windows7+linux双系统)
前提条件:1.先装windows7,后装linux系统 2.windows7 里有“未分区的空间”(不是C:,D:,E:) :计算机→管理→存储空间,删除一些压缩卷即可. 3.下载ultraiso → ...
- android anim 动画效果(转)
动画效果编程基础--AnimationAndroid 动画类型 Android的animation由四种类型组成 XML中 alpha 渐变透明度动画效果 ...
- VB编程技巧推荐
VB编程技巧推荐 1.zyl910的专栏——理论水平高 用VB写高效的图像处理程序 V2.0 优化分支代码——避免跳转指令堵塞流水线 2.Laviewpbt的专栏 —— 有很多算法的代码,实用性高 ...
- jquery获取元素到页面顶部距离
jquery获取元素到页面顶部距离的语句为: $(selector).offset().top
- 基于CANVAS与MD5的客户端生成验证码
好久没写东西,工作太忙了!不想服务端请求太多,搞了个这玩意儿,不过项目中并不会用上,还是使用服务端生成的机制(会安全多少呢?):我就想问个问题,除了图像识别来破解这样的简单验证码外,针对我这种例子,可 ...
- html.day01
1.web标准: 1. 结构 (xhtml) 2. 表现(css) 3.行为(js) html 超文本标记语言 xhtml (严格型超文本标记语言) 2.规范: 1. 所有标签(标记)都要 ...
- C#比较dynamic和Dictionary性能
开发中需要传递变参,考虑使用 dynamic 还是 Dictionary(准确地说是Dictionary<string,object>).dynamic 的编码体验显著优于 Diction ...
- sql 列轉行、行轉列
PIVOT用于将列值旋转为列名(即行转列),在SQL Server 2000可以用聚合函数配合CASE语句实现 PIVOT的一般语法是:PIVOT(聚合函数(列) FOR 列 in (…) )AS P ...
- Swift - 03 - 整数类型
//: Playground - noun: a place where people can play import UIKit var str = "Hello, playground& ...