DirectedWeightedGraph 发表于 2017-04-26 | 分类于 数据结构 DirectedWeightedGraph代码如下:123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141//// Created by mfzhu on 2017/4/19.//#ifndef ALGORITHM_DIJKSTRA_H#define ALGORITHM_DIJKSTRA_H#include <iostream>#include <vector>#include <map>using namespace std;class Edge;class directedGraphNode{public: int label; vector<Edge*> neighbors; directedGraphNode(int l): label(l) {};};class Edge{public: int weight; directedGraphNode* start; directedGraphNode* end; Edge(directedGraphNode* s, directedGraphNode* e, int w): weight(w), start(s), end(e){};};class directedWeightedGraph{public: directedWeightedGraph(); ~directedWeightedGraph(){};// void dfs();// void bfs();// void prime();// void kruskal(); void dijkstra(int start);// void Bellman_Ford(); void insertEdge(int start, int end, int weight); void insertNode(int val); void print();private: int nNode; int nEdge; map<int, directedGraphNode*> nodeTable; void insertNode(map<int, directedGraphNode*> &nodeTable, int val); void insertEdge(map<int, directedGraphNode*> &nodeTable, int start, int end, int val); void dijkstra(map<int, directedGraphNode*> nodeTable, int start);};directedWeightedGraph::directedWeightedGraph() { cout << "please input the number of vertex:" << endl; cin >> nNode; cout << "please input the number of edge:" << endl; cin >> nEdge; for (int i = 0; i < nNode; i++){ cout << "please input the label of vertex:" << endl; int label; cin >> label; directedGraphNode* node = new directedGraphNode(label); nodeTable.insert({label, node}); } for (int i = 0; i < nEdge; i++){ cout << "please input the info of Edge:" << endl; int start; int end; int weight; cin >> start >> end >> weight; Edge* edge = new Edge(nodeTable[start], nodeTable[end], weight); nodeTable[start]->neighbors.push_back(edge); }}void directedWeightedGraph::print() { auto it = nodeTable.begin(); while(it != nodeTable.end()){ cout << "the node label is :" << it->first << endl; cout << "the node info is:" << endl; for (auto i : it->second->neighbors){ cout << i->start->label << "->" << i->end->label << " weight:" << i->weight << endl; } it++; }}void directedWeightedGraph::insertNode(map<int, directedGraphNode *> &nodeTable, int val) { if (nodeTable.find(val) == nodeTable.end()){ directedGraphNode* node = new directedGraphNode(val); nodeTable.insert({val, node}); }else{ cout << "the node is existed" << endl; }}void directedWeightedGraph::insertNode(int node) { insertNode(nodeTable, node);}void directedWeightedGraph::insertEdge(map<int, directedGraphNode *> &nodeTable, int start, int end, int w) { Edge* e = new Edge(nodeTable[start], nodeTable[end], w); nodeTable[start]->neighbors.push_back(e);}void directedWeightedGraph::insertEdge(int start, int end, int weight) { insertEdge(nodeTable, start, end, weight);}void directedWeightedGraph::dijkstra(map<int, directedGraphNode *> nodeTable, int start) { map<int, int> visited{{start, 0}}; map<int, int> unvisted; for (auto i : nodeTable){ if (i.first != start) unvisted.insert({i.first, INT8_MAX}); } for(auto i: nodeTable[start]->neighbors){ unvisted[i->end->label] = i->weight; } while(!unvisted.empty()){ int cur_min_label = unvisted.begin()->first; int cur_min_weight = unvisted.begin()->second; for (auto it = unvisted.begin(); it != unvisted.end(); it++){ if (it->second < cur_min_weight){ cur_min_label = it->first; cur_min_weight = it->second; } } visited.insert({cur_min_label, cur_min_weight}); unvisted.erase(cur_min_label); for (auto it = unvisted.begin(); it != unvisted.end(); it++){ int cur_distance = it->second; for (auto edge: nodeTable[it->first]->neighbors){ if (edge->end->label == cur_min_label && (edge->weight + cur_min_weight) < cur_distance){ it->second = edge->weight + cur_min_weight; } } } } for(auto it = visited.begin(); it != visited.end(); it++){ cout << start << " " << it->first << ":" << it->second << endl; }}void directedWeightedGraph::dijkstra(int start) { dijkstra(nodeTable, start);}#endif //ALGORITHM_DIJKSTRA_H