赞
踩
- void Graph::Dijkstra(size_t v) {
- verptr->dis[v] = 0;
- while (true) {
- size_t min = InFinity; //未访问的顶点到开始顶点的权值为∞
- for (int i = 0; i < verptr->vertexnumber; ++i) {
- if (verptr->visited[i] == false && min > verptr->dis[i]) { //选择离开始顶点权值最小的顶点
- min = verptr->dis[i];
- v = i;
- }
- }
- if (min == InFinity) //全部访问完跳出循环
- break;
- verptr->visited[v] = true; //对访问的进行标记
- auto beg = lists[v].begin();
- while (beg != lists[v].end()) {
- if (verptr->visited[*beg] == false)
- if (verptr->dis[v] + weight[v][*beg] < verptr->dis[*beg])
- verptr->dis[*beg] = verptr->dis[v] + weight[v][*beg]; //对dis进行更新
- ++beg;
- }
- }
- }
- #include<iostream>
- #include<vector>
- #include<list>
- #include<memory>
- const size_t InFinity = 999;
- struct Vertex {
- size_t vertexnumber;
- bool* visited;
- size_t* dis; //储存minweight
- };
- class Graph {
- public:
- Graph(const size_t _vertexnumber) :verptr(std::make_unique<Vertex>()), lists(new std::list<size_t>[_vertexnumber]()), weight(std::vector<std::vector<size_t>>(_vertexnumber, std::vector<size_t>(_vertexnumber, -1))) {
- verptr->vertexnumber = _vertexnumber;
- verptr->visited = new bool[_vertexnumber]();
- verptr->dis = new size_t[_vertexnumber]();
- for (int i = 0; i < verptr->vertexnumber; ++i)
- verptr->dis[i] = InFinity;
- }
- ~Graph() {
- if (verptr->visited && verptr->dis) {
- delete[] verptr->visited;
- delete[] verptr->dis;
- delete[] lists;
- }
- else
- throw std::out_of_range("Out of MemorySpace!!");
-
- }
- public:
- void AddEdge(const size_t v, const size_t w, const size_t _weight);
- void Dijkstra(size_t startv);
- void Display(const size_t v)const;
- private:
- std::unique_ptr<Vertex>verptr;
- std::list<size_t>*lists;
- std::vector<std::vector<size_t>>weight;
-
- };
-
- void Graph::AddEdge(const size_t v, const size_t w, const size_t _weight) {
- lists[v].push_back(w);
- weight[v][w] = _weight;
- }
-
- void Graph::Dijkstra(size_t v) {
- verptr->dis[v] = 0;
- while (true) {
- size_t min = InFinity;
- for (int i = 0; i < verptr->vertexnumber; ++i) {
- if (verptr->visited[i] == false && min > verptr->dis[i]) {
- min = verptr->dis[i];
- v = i;
- }
- }
- if (min == InFinity)
- break;
- verptr->visited[v] = true;
- auto beg = lists[v].begin();
- while (beg != lists[v].end()) {
- if (verptr->visited[*beg] == false)
- if (verptr->dis[v] + weight[v][*beg] < verptr->dis[*beg])
- verptr->dis[*beg] = verptr->dis[v] + weight[v][*beg];
- ++beg;
- }
- }
- }
-
- void Graph::Display(const size_t v)const {
- for (int i = 0; i < verptr->vertexnumber; ++i) {
- std::cout << v << " to " << i << " minweight: " << verptr->dis[i] << std::endl;
- }
- }
-
- int main(void)
- {
- const size_t vertexnumber = 7;
- Graph graph(vertexnumber);
- graph.AddEdge(0, 1, 2);
- graph.AddEdge(0, 2, 4);
- graph.AddEdge(0, 3, 1);
- graph.AddEdge(1, 3, 3);
- graph.AddEdge(1, 4, 10);
- graph.AddEdge(2, 5, 5);
- graph.AddEdge(3, 2, 2);
- graph.AddEdge(3, 5, 8);
- graph.AddEdge(3, 6, 4);
- graph.AddEdge(3, 4, 2);
- graph.AddEdge(4, 6, 6);
- graph.AddEdge(6, 5, 1);
- graph.Dijkstra(0);
- graph.Display(0);
- system("pause");
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。