生活随笔
收集整理的這篇文章主要介紹了
菊读图的dijkstra
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
我就憑這個數據結構拿到了深圳的offer。
其中做了點改動,rule_of_five中的四個被我private了,其實delete和private效果相同。為啥呢?想復制一張圖哪是這么簡單,本身就是一道leetcode中難度的題了,但是不寫還不行,所以我就直接把它們干掉了。那移動它怎么辦?放心,這又不是C,RVO和引用完全夠用了。
class Edge;
class Node
{public:int name_;int indegree_;int outdegree_;list<Node*>next_node_;list<Edge*>next_edge_;Node(int name):name_(name),indegree_(0),outdegree_(0){}
};class Edge{public:int weight_;Node* from_node_;Node* to_node_;Edge(int weight, Node* from_node, Node* to_node):weight_(weight),from_node_(from_node),to_node_(to_node){}
};class Graph
{public:list<Edge*>edge_;unordered_map<int, Node*>node_;Graph(const vector<vector<int>>& graph_vec){for(auto edge_vec : graph_vec){int from_name = edge_vec[0], to_name = edge_vec[1], weight = edge_vec[2];if(node_.find(from_name)==node_.end())node_.insert(pair<int,Node*>(from_name, new Node(from_name)));if(node_.find(to_name)==node_.end())node_.insert(pair<int,Node*>(to_name, new Node(to_name)));Node *fromNode = node_.find(from_name)->second, *toNode = node_.find(to_name)->second;Edge *edge = new Edge(weight, fromNode, toNode);fromNode->outdegree_++;toNode->indegree_++;fromNode->next_node_.push_back(toNode);fromNode->next_edge_.push_back(edge);edge_.push_back(edge);}}~Graph(){for(auto p_edge : edge_)delete(p_edge);for(auto pair_node : node_)delete(pair_node.second);}private:Graph(const Graph& other)=delete;Graph(const Graph&& other)=delete;Graph& operator=(Graph& other)=delete;Graph& operator=(Graph&& other)=delete;
};
總結
以上是生活随笔為你收集整理的菊读图的dijkstra的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。