最小生成树prim (c++ 已大改)

2019独角兽企业重金招聘Python工程师标准>>> hot3.png

#include <iostream>
#include <vector>
#include <set>
#include <map>
#include <initializer_list>
#include <memory>
template<typename T>
class Graph{private:std::map<T, map<T, unsigned int>> graph; //存储无向图. std::map<T, std::vector<T>> edge; //邻接链表. 也就是说给定一个结点另外有多少个结点是与其相连接的. std::queue<T> vertex; //存储所有的结点. std::set<T> memberFromQueue; //从栈内弹出的元素放到set中.std::vector<T> currentVertex;unsigned int vertexNumber;public:template<typename Ty, unsigned int N>Graph(const Ty (&edges)[N][3]);~Graph();void primAlgorithm();
};
template<typename T>
template<typename Ty, unsigned int N>
Graph<T>::Graph(const Ty (&edges)[N][3]):vertex(nullptr),vertexNumber(vertexs.size())
{if(vertexs.size() == 0){throw std::bad_cast();}std::cout<<"enter successfully"<<std::endl;for(int i=0; i<N; ++i){this->graph[edges[i][0]][edges[i][2]] = edges[i][1]; //map的特性就是如果其中不含有edges[i][0]以及edge[i][0]元素就会自动创建一个. this->graph[edges[i][2]][edges[i][0]] = edges[i][1];this->edge[edges[i][0]].push_back(edges[i][2]); //与结点edges[i][0]相接的所有结点,被放到与其对应的vector中. this->edge[edges[i][2]].push_back(edges[i][0]); //同上. }for(std::map<Ty, std::map<Ty, unsigned int>>::const_iterator it = this->edge.cbegin(); it != this->edge.cend(); ++it){ //把所有结点都放到vertex中. this->vertex.push(it->first);}std::cout<<"out"<<std::endl;
}
template<typename T>
void Graph<T>::primAlgorithm()
{T head;int total = 0;head = this->vertex.front(); //弹出栈内第一个元素. this->vertex.pop(); //删除该元素.this->memberFromQueue.insert(head);this->currentVertex.push_back(head);while( !this->vertex.empty() ){ //当给定的无向图不为空. int i=0;int j=0;int min=0;int flag =0;T start;T end;for(i=0; i<this->currentVertex.size(); ++i){ //当前顶点. for(j=0; i<this->edge[this->currentVertex[i]].size(); ++j){ //与当前顶点(head)相连接的有多少个顶点, 逐个访问这些与当前顶点相连接的结点. if(this->memberFromQueue.find(this->edge[this->currentVertex[i]][j]) == this->memberFromQueue.end()){ //查找当前顶点时候存在memberFromQueue中. if(flag == 0){ //如果给定的顶点是树中的第一个. 那么令min等于当前顶点与任意一边的加权值. min = this->graph[this->currentVertex[i]][this->edge[this->currentVertex[i]][j]]; //获得的是currentVertex[i] 和 edge[currentVertex[i]] [j] 这两个结点的加权值.flag = 1; }if(this->graph[this->currentVertex[i]][this->edge[this->currentVertex[i]][j]] <= min){min = this->graph[this->currentVertex[i][this->edge[this->currentVertex[i]][j]];start = this->currentVertex[i];end = this->edge[this->currentVertex[i]][j];}}}}std::cout<<start<<"----"<<min<<"-----"<<end<<std::endl;//输出边以及加权值.this->vertex.pop(); //删除栈内当前的顶点元素.this->memberFromQueue.insert(end);this->currentVertex.push_back(end); }}

转载于:https://my.oschina.net/SHIHUAMarryMe/blog/601102

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/543745.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

assoc_Ruby assoc()函数

assocRuby中的assoc()函数 (assoc() function in Ruby) We have studied functions to process single dimensional array so far but if we talk about assoc() function, it does not work for single dimensional arrays. assoc() function only works on Array of Arrays o…

前端接入HTTP协议浅析

【摘要】&#xff1a;本文整理并简要分析了HTTP协议的交互过程和内容格式&#xff0c;包括HTTP请求、HTTP应答的头域和实体内容&#xff0c;HTTP 1.0与HTTP 1.1的差异&#xff0c;并举例说明了Chunked编码的工作过程原理。1、HTTP协议简介浏览器和Web服务器之间一问一答的交互过…

互联网传真 传真指令_传真的完整形式是什么?

互联网传真 传真指令传真&#xff1a;传真 (FAX: Facsimile) FAX is an abbreviation of "Facsimile". 传真是“传真”的缩写 。 It is commonly written and spoken as FAX. It is a telephonic transmission of a scanned copy of text and images printed on a p…

C#使用七牛云存储上传下载文件、自定义回调

项目需要将音视频文件上传服务器&#xff0c;考虑并发要求高&#xff0c;通过七牛来实现。 做了一个简易的压力测试&#xff0c;同时上传多个文件&#xff0c;七牛自己应该有队列处理并发请求&#xff0c;我无论同时提交多少个文件&#xff0c;七牛是批量一个个排队处理了。 一…

netfilter que_QUE的完整形式是什么?

netfilter que问题&#xff1a;问题 (QUE: Questions) QUE is an abbreviation of "Questions". QUE是“ Questions”的缩写 。 It is an expression, which is commonly used in the Gmail platform. It is written in the body or the subject of the email to te…

mysqld_multi stop 不能停掉mysql

使用mysqld_multi start 启动了多个mysql&#xff0c;但是mysqld_multi stop 却不能停止原因&#xff1a;因为你还没有授权/usr/local/mysql/bin/mysqld_multi stop 但是默认是停不掉的&#xff0c;需要我们做一个授权grant shutdown on *.* to usernamelocalhost identified…

热冗余冷冗余_冗余支架

热冗余冷冗余Problem statement: 问题陈述&#xff1a; Given a string of balanced expression, find if it contains a redundant parenthesis or not. A set of parentheses is redundant if the same sub-expression is surrounded by unnecessary or multiple brackets. …

对称树

Problem statement: 问题陈述&#xff1a; Given a binary Tree, check whether the tree is symmetric or not. 给定二叉树 &#xff0c; 检查树是否对称 。 Input Example: 输入示例&#xff1a; For example1/ \2 2/ \ / \3 4 4 3The above tree is symmetric1/ \2 …

unity, undo

如果在操作一个Object之前调用Undo.RecordObject(Object)&#xff0c;且操作确实造成Object某些属性的改变&#xff0c;则会产生一个undo记录。 如果我们的架构不是直接操作Object&#xff0c;而是操作一个ui&#xff0c;并在某些时机通过ui.save(Object)将数据回写到Object&am…

ajax应用_AJAX的应用

ajax应用AJAX has several benefits that can be utilized inside a web application. In this article, well explore some advantages of AJAX and see some of its applications. AJAX具有可在Web应用程序内部使用的多个优点。 在本文中&#xff0c;我们将探讨AJAX的一些优势…

Linux下DRBD配置

一、什么是DRBD1、简介 Distributed Replicated Block Device(DRBD)是一个用软件实现的、无共享的、服务器之间镜像块设备内容的存储复制解决方案。数据镜像&#xff1a;实时、透明、同步&#xff08;所有服务器都成功后返回&#xff09;、异步&#xff08;本地服务器成功后返回…

键盘特殊_特殊键盘

键盘特殊Problem statement: 问题陈述&#xff1a; Imagine you have a special keyboard with four types of keys: 想象一下&#xff0c;您有一个特殊的键盘&#xff0c;其中包含四种类型的键&#xff1a; Key 1: Prints I on screen 按键1&#xff1a;在屏幕上打印“ I”…

【C++入门】简单的日期类操作

//--------------------------------------------------------------------------/***名称&#xff1a;日期的简单操作******类函数&#xff1a;构造函数&#xff0c;拷贝构造函数&#xff0c;析构函数&#xff0c;操作符重载函数****日期类操作函数&#xff1a; 1&#xff1a;…

Scala山脉

Scala Range Scala山脉 A Range is a bounded series with a uniform interval with an upper and lower limit. The range literal is a numerical sequence of number ranging with a certain limit. 范围是一个有上限且下限均匀的有界序列。 范围文字是具有一定限制的范围…

黑客经验谈:跳板攻击入侵技术实例解析

网络入侵&#xff0c;安全第一,一个高明的入侵者&#xff0c;不会冒然实行动. 他们在入侵时前会做足功课&#xff0c;入侵时会通过各种技术手段保护自己&#xff0c;以防被对方发现&#xff0c;引火烧身. 其中&#xff0c;跳板技术是攻击者通常采用的技术. 下面笔者结合实例&am…

dom属性和html属性_HTML属性

dom属性和html属性Attributes are used to provide additional information of a tag such as it’s alignments, color, size of the text and other. The attributes are given with the tag that is between the angular brackets after the tag name. The attributes have …

科普:UTF-8 GBK UTF8 GB2312 之间的区别和关系

UTF-8&#xff1a;Unicode TransformationFormat-8bit&#xff0c;允许含BOM&#xff0c;但通常不含BOM。是用以解决国际上字符的一种多字节编码&#xff0c;它对英文使用8位&#xff08;即一个字节&#xff09;&#xff0c;中文使用24为&#xff08;三个字节&#xff09;来编码…

vue3实现本地开发使用的px转换成vw,px转换成rem方法整理

前言&#xff1a; 项目中如果想本地开发使用px&#xff0c;但是界面上线以后界面是自适应的效果,可以有多种方式来实现效果。 一、px转成vw 1、安装&#xff0c;安装成功后&#xff0c;node_modules 会新增这两个插件包 npm i postcss-px-to-viewport-8-plugin 2、新增 post…

airplay2协议是什么_什么是AirPlay?

airplay2协议是什么AirPlay (AirPlay) AirPlay is released by Apple in the year 2004. It allows the easy exchange of audios without the use of any wired technique between the two devices. It was previously termed as AirTunes and later got its name changed to …

微信支付开发(5) 订单查询

本文介绍微信支付中订单查询功能的实现。 作者&#xff1a;方倍工作室 地址&#xff1a;http://www.cnblogs.com/txw1958/p/wxpay-order-query.html 一、订单查询 因为某一方技术的原因&#xff0c;可能导致商户在预期时间内都收不到最终支付通知&#xff0c;此时商户可以通过该…