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还不是很会,大体理解的意思是可以同时绑 ...
随机推荐
- 数据结构 - 堆排序(heap sort) 具体解释 及 代码(C++)
堆排序(heap sort) 具体解释 及 代码(C++) 本文地址: http://blog.csdn.net/caroline_wendy 堆排序包括两个步骤: 第一步: 是建立大顶堆(从大到小排 ...
- Qt 学习之路 :线程简介
现代的程序中,使用线程的概率应该大于进程.特别是在多核时代,随着 CPU 主频的提升,受制于发热量的限制,CPU 散热问题已经进入瓶颈,另辟蹊径地提高程序运行效率就是使用线程,充分利用多核的优势.有关 ...
- Android(java)学习笔记227:服务(service)之服务的生命周期 与 两种启动服务的区别
1.之前我们在Android(java)学习笔记171:Service生命周期 (2015-08-18 10:56)说明过,可以回头看看: 2.Service 的两种启动方法和区别: (1)Servi ...
- U口破解指令介绍.
- 抓取锁的sql语句-第六次修改
增加异常处理 CREATE OR REPLACE PROCEDURE SOLVE_LOCK AS V_SQL VARCHAR2(3000); --定义 v_sql 接受抓取锁的sql语句V_SQL02 ...
- 说说oracle的 sysdate、trunc函数
SQL> select trunc(sysdate)+1/24+3 from dual; TRUNC(SYSDATE)+1/24-------------------2015-08-14 01: ...
- 05_Smart-image通过SoftReference提高性能
文章导读: 文件介绍了常见的图片下载开源插件smart-image, 由于移动设备硬件受限,因此Android的相关app都要考虑到性能的关系, 所以很多的第三方插件都使用到了缓存cache技术,本人 ...
- Java安全发布的理解
看<Java并发编程实战>遇到如下问题 代码: /** * Created by yesiming on 16/11/11. */public class Holder { private ...
- Solution(项目部署):The server does not support version 3.0 of the J2EE Web module specification
1.错误: 在eclipse中使用run->run on server的时候,选择tomcat6会报错误:The server does not support version 3.0 of t ...
- Wireshark抓包、过滤器
查阅于http://blog.sina.com.cn/s/blog_5d527ff00100dwph.html 1.捕捉过滤器 设置捕捉过滤器的步骤是:- 选择 capture -> optio ...