矩阵乘法c语言最简单,C语言最短路径和矩阵乘法.

#include 

#include 

#include 

#include 

#include 

#include 

namespace{

enum:int{

MAXVALUE = 9999

};

}

template

class Node{

private:

T key_;

public:

template

Node(const Ty& key);

template

Node(const Node& otherNode_);

template

Node(Node&& otherNode_);

Node() = default;

~Node();

template

friend bool operator==(const Node& first_, const Node& second_);

template

friend bool operator& first_, const Node& second_);

const Node& operator=(const Node& otherNode_);//必须这么写 不然编译器还会合成.

template

friend std::ostream& operator<& node_);

template

void operator=(Node&& otherNode_);

};

template

template

Node::Node(const Ty& key)

:key_(key)

{

//std::cout<

}

template

template

Node::Node(const Node& otherNode_)

:key_(otherNode_.key_)

{

std::cout<

}

template

Node::~Node()

{

//

}

template

bool operator==(const Node& first_, const Node& second_)

{

return (first_.key_ == second_.key_) ? true : false;

}

template

const Node& Node::operator=(const Node& otherNode_) //必须这么写不然编译器自己合成.

{

this->key_ = otherNode_.key_;

std::cout<

}

template

bool operator& first_, const Node& second_)

{

std::cout<

return (first_.key_ 

}

template

std::ostream& operator<& node_)

{

os<

return os;

}

template

template

void Node::operator=(Node&& otherNode_)

{

std::cout<

this->key_ = otherNode_.key_;

}

template

class Map{

private:

class Compare{

public:

template

bool operator()(const Node& first_, const Node& second_);

};

//template

//typedef Graph std::map, std::map, int>>; //不能用typedef只能用using.

class Container{

public:

template

bool operator()(typename Map::cv_iter currentIter_, std::shared_ptr> currentNode_);

};

std::map, std::map, int>> graph_; //该边的加权值可以为负

std::map, std::vector>> adjList_;

std::map, int>> temp_graph_;

unsigned int nodeNumber_;

template

typename Map::Graph extend_shortest_paths(std::shared_ptr::Graph > g);

const int& min(const int& first_, const int& second_);

public:

template

using Graph = std::map, int>>;//类型别名.

template

using v_iter = typename std::vector>::const_iterator;//类型别名.

template

using  cv_iter = typename std::map, std::vector>>::const_iterator;

template

using cmm_iter = typename std::map, int>>::const_iterator;//类型别名.

template

Map(const Ty (&edge)[N][3]);

~Map();

Map()=default;

void slow_all_pairs_shortest_paths();

};

template

template

Map::Map(const Ty (&edges)[N][3])

:nodeNumber_(N)

{

if(N == 0){

throw std::runtime_error(std::string("there is nothing in graph"));

}

for(int i =0; i

Node first_(edges[i][0]);

Node second_(edges[i][2]);

this->graph_[first_][second_] = edges[i][1];

this->temp_graph_[first_][second_] = ::MAXVALUE;

this->adjList_[first_].push_back(second_); //邻接链表:跟结点A相连接的所有结点都被放在一个vector中.

}

}

template

template

typename Map::Graph Map::extend_shortest_paths(std::shared_ptr::Graph > g)

{

typename Map::Container jundge_;

typename Map::cv_iter first_ = this->adjList_.cbegin();

typename Map::v_iter second_ = this->adjList_[first_->first].cbegin();

typename Map::Graph temp_graph_;

for(; first_ != this->adjList_.cend(); ++first_){

for(; second_ != this->adjList_[first_->first].cend(); ++second_){

temp_graph_[first_->first][*second_] = ::MAXVALUE;

typename Map::v_iter third_ = this->adjList_[first_->first].cbegin();

for(; third_ != this->adjList_[first_->first].cend(); ++third_){

bool boolean = jundge(third_, std::make_shared> (*second_));

if(boolean == false){

continue;

}

temp_graph_[first_][second_] = this->min(temp_graph_[first_][second_], (*g)[first_][third_]+this->graph_[third_][second_]);

}

}

}

return (*g);

}

template

void Map::slow_all_pairs_shortest_paths()

{

Map::Graph L(this->graph_);

for(int i=1; inodeNumber_; ++i){

L = this->extended_shortest_paths(std::make_shared::Graph > (this->graph_));

}

}

template

template

bool Map::Compare::operator()(const Node& first_, const Node& second_)

{

return first_ 

}

template

const int& Map::min(const int& first_, const int& second_)

{

return (first_ 

}

template

template

bool Map::Container::operator()(typename Map::cv_iter currentIter_, std::shared_ptr> currentNode_)

{

if(Map::adjList_[*currentNode_].empty()){

return false;

}else{

typename Map::v_iter first_ = Map::adjList_[*currentNode_].cbegin();

typename Map::v_iter second_ = Map::adjList_[*currentNode_].cend();

typename Map::v_iter third_;

third_=std::find_if(first_, second_, [currentNode_](const Node& temp_)->bool { return (temp_ == *currentNode_) ? true : false; });

if(third_ == second_){

return false;

}else{

return true;

}

}

}

int main()

{

Node one_(20);

Node two_(30);

Node three_;

three_ = one_;

one_ = two_;

std::cout<

three_ = std::move(one_);

std::cout<<:boolalpha>

return 0;

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值